2020-06-28 11:26:22 +00:00
package remotewrite
import (
"context"
"flag"
"fmt"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
)
var (
2021-04-20 08:42:17 +00:00
addr = flag . String ( "remoteWrite.url" , "" , "Optional URL to VictoriaMetrics or vminsert where to persist alerts state " +
2021-08-16 12:20:22 +00:00
"and recording rules results in form of timeseries. For example, if -remoteWrite.url=http://127.0.0.1:8428 is specified, " +
"then the alerts state will be written to http://127.0.0.1:8428/api/v1/write . See also -remoteWrite.disablePathAppend" )
2022-03-10 11:09:12 +00:00
2021-09-14 11:32:06 +00:00
basicAuthUsername = flag . String ( "remoteWrite.basicAuth.username" , "" , "Optional basic auth username for -remoteWrite.url" )
basicAuthPassword = flag . String ( "remoteWrite.basicAuth.password" , "" , "Optional basic auth password for -remoteWrite.url" )
basicAuthPasswordFile = flag . String ( "remoteWrite.basicAuth.passwordFile" , "" , "Optional path to basic auth password to use for -remoteWrite.url" )
2022-03-10 11:09:12 +00:00
bearerToken = flag . String ( "remoteWrite.bearerToken" , "" , "Optional bearer auth token to use for -remoteWrite.url." )
bearerTokenFile = flag . String ( "remoteWrite.bearerTokenFile" , "" , "Optional path to bearer token file to use for -remoteWrite.url." )
2020-06-28 11:26:22 +00:00
maxQueueSize = flag . Int ( "remoteWrite.maxQueueSize" , 1e5 , "Defines the max number of pending datapoints to remote write endpoint" )
maxBatchSize = flag . Int ( "remoteWrite.maxBatchSize" , 1e3 , "Defines defines max number of timeseries to be flushed at once" )
concurrency = flag . Int ( "remoteWrite.concurrency" , 1 , "Defines number of writers for concurrent writing into remote querier" )
flushInterval = flag . Duration ( "remoteWrite.flushInterval" , 5 * time . Second , "Defines interval of flushes to remote write endpoint" )
tlsInsecureSkipVerify = flag . Bool ( "remoteWrite.tlsInsecureSkipVerify" , false , "Whether to skip tls verification when connecting to -remoteWrite.url" )
tlsCertFile = flag . String ( "remoteWrite.tlsCertFile" , "" , "Optional path to client-side TLS certificate file to use when connecting to -remoteWrite.url" )
tlsKeyFile = flag . String ( "remoteWrite.tlsKeyFile" , "" , "Optional path to client-side TLS certificate key to use when connecting to -remoteWrite.url" )
tlsCAFile = flag . String ( "remoteWrite.tlsCAFile" , "" , "Optional path to TLS CA file to use for verifying connections to -remoteWrite.url. " +
"By default system CA is used" )
tlsServerName = flag . String ( "remoteWrite.tlsServerName" , "" , "Optional TLS server name to use for connections to -remoteWrite.url. " +
"By default the server name from -remoteWrite.url is used" )
2022-03-10 11:09:12 +00:00
oauth2ClientID = flag . String ( "remoteWrite.oauth2.clientID" , "" , "Optional OAuth2 clientID to use for -remoteWrite.url." )
oauth2ClientSecret = flag . String ( "remoteWrite.oauth2.clientSecret" , "" , "Optional OAuth2 clientSecret to use for -remoteWrite.url." )
oauth2ClientSecretFile = flag . String ( "remoteWrite.oauth2.clientSecretFile" , "" , "Optional OAuth2 clientSecretFile to use for -remoteWrite.url." )
oauth2TokenURL = flag . String ( "remoteWrite.oauth2.tokenUrl" , "" , "Optional OAuth2 tokenURL to use for -notifier.url." )
oauth2Scopes = flag . String ( "remoteWrite.oauth2.scopes" , "" , "Optional OAuth2 scopes to use for -notifier.url. Scopes must be delimited by ';'." )
2021-08-16 11:20:57 +00:00
disablePathAppend = flag . Bool ( "remoteWrite.disablePathAppend" , false , "Whether to disable automatic appending of '/api/v1/write' path to the configured -remoteWrite.url." )
2020-06-28 11:26:22 +00:00
)
// Init creates Client object from given flags.
// Returns nil if addr flag wasn't set.
func Init ( ctx context . Context ) ( * Client , error ) {
if * addr == "" {
return nil , nil
}
t , err := utils . Transport ( * addr , * tlsCertFile , * tlsKeyFile , * tlsCAFile , * tlsServerName , * tlsInsecureSkipVerify )
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "failed to create transport: %w" , err )
2020-06-28 11:26:22 +00:00
}
2022-03-10 11:09:12 +00:00
authCfg , err := utils . AuthConfig (
utils . WithBasicAuth ( * basicAuthUsername , * basicAuthPassword , * basicAuthPasswordFile ) ,
utils . WithBearer ( * bearerToken , * bearerTokenFile ) ,
utils . WithOAuth ( * oauth2ClientID , * oauth2ClientSecret , * oauth2ClientSecretFile , * oauth2TokenURL , * oauth2Scopes ) )
2021-09-14 11:32:06 +00:00
if err != nil {
return nil , fmt . Errorf ( "failed to configure auth: %w" , err )
}
2020-06-28 11:26:22 +00:00
return NewClient ( ctx , Config {
2021-08-16 11:20:57 +00:00
Addr : * addr ,
2021-09-14 11:32:06 +00:00
AuthCfg : authCfg ,
2021-08-16 11:20:57 +00:00
Concurrency : * concurrency ,
MaxQueueSize : * maxQueueSize ,
MaxBatchSize : * maxBatchSize ,
FlushInterval : * flushInterval ,
DisablePathAppend : * disablePathAppend ,
Transport : t ,
2020-06-28 11:26:22 +00:00
} )
}