2020-06-28 13:26:22 +02:00
package remotewrite
import (
"context"
"flag"
"fmt"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
2022-08-11 09:56:40 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
2024-02-08 15:52:00 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httputils"
2024-07-15 23:00:14 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
2020-06-28 13:26:22 +02:00
)
var (
2021-04-20 10:42:17 +02:00
addr = flag . String ( "remoteWrite.url" , "" , "Optional URL to VictoriaMetrics or vminsert where to persist alerts state " +
2024-05-22 10:52:51 +02:00
"and recording rules results in form of timeseries. " +
2024-08-16 11:32:04 +02:00
"Supports address in the form of IP address with a port (e.g., http://127.0.0.1:8428) or DNS SRV record. " +
2024-05-22 10:52:51 +02:00
"For example, if -remoteWrite.url=http://127.0.0.1:8428 is specified, " +
2022-08-11 13:30:32 +02:00
"then the alerts state will be written to http://127.0.0.1:8428/api/v1/write . See also -remoteWrite.disablePathAppend, '-remoteWrite.showURL'." )
2022-08-11 09:56:40 +02:00
showRemoteWriteURL = flag . Bool ( "remoteWrite.showURL" , false , "Whether to show -remoteWrite.url in the exported metrics. " +
"It is hidden by default, since it can contain sensitive info such as auth key" )
2022-03-10 12:09:12 +01:00
2022-07-21 13:57:53 +02:00
headers = flag . String ( "remoteWrite.headers" , "" , "Optional HTTP headers to send with each request to the corresponding -remoteWrite.url. " +
"For example, -remoteWrite.headers='My-Auth:foobar' would send 'My-Auth: foobar' HTTP header with every request to the corresponding -remoteWrite.url. " +
"Multiple headers must be delimited by '^^': -remoteWrite.headers='header1:value1^^header2:value2'" )
2021-09-14 13:32:06 +02: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 12:09:12 +01: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 13:26:22 +02:00
2024-05-30 17:54:42 +02:00
idleConnectionTimeout = flag . Duration ( "remoteWrite.idleConnTimeout" , 50 * time . Second , ` Defines a duration for idle (keep-alive connections) to exist. Consider settings this value less to the value of "-http.idleConnTimeout". It must prevent possible "write: broken pipe" and "read: connection reset by peer" errors. ` )
2024-10-22 14:43:55 +02:00
maxQueueSize = flag . Int ( "remoteWrite.maxQueueSize" , defaultMaxQueueSize , "Defines the max number of pending datapoints to remote write endpoint" )
maxBatchSize = flag . Int ( "remoteWrite.maxBatchSize" , defaultMaxBatchSize , "Defines max number of timeseries to be flushed at once" )
concurrency = flag . Int ( "remoteWrite.concurrency" , defaultConcurrency , "Defines number of writers for concurrent writing into remote write endpoint" )
flushInterval = flag . Duration ( "remoteWrite.flushInterval" , defaultFlushInterval , "Defines interval of flushes to remote write endpoint" )
2020-06-28 13:26:22 +02:00
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. " +
2023-05-10 09:50:41 +02:00
"By default, system CA is used" )
2020-06-28 13:26:22 +02:00
tlsServerName = flag . String ( "remoteWrite.tlsServerName" , "" , "Optional TLS server name to use for connections to -remoteWrite.url. " +
2023-05-10 09:50:41 +02:00
"By default, the server name from -remoteWrite.url is used" )
2022-03-10 12:09:12 +01:00
2023-12-20 20:35:16 +01: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" )
oauth2EndpointParams = flag . String ( "remoteWrite.oauth2.endpointParams" , "" , "Optional OAuth2 endpoint parameters to use for -remoteWrite.url . " +
` The endpoint parameters must be set in JSON format: { "param1":"value1",...,"paramN":"valueN"} ` )
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 ';'." )
2020-06-28 13:26:22 +02:00
)
2022-08-11 09:56:40 +02:00
// InitSecretFlags must be called after flag.Parse and before any logging
func InitSecretFlags ( ) {
if ! * showRemoteWriteURL {
flagutil . RegisterSecretFlag ( "remoteWrite.url" )
}
}
2020-06-28 13:26:22 +02: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
}
2024-02-08 15:52:00 +01:00
t , err := httputils . Transport ( * addr , * tlsCertFile , * tlsKeyFile , * tlsCAFile , * tlsServerName , * tlsInsecureSkipVerify )
2020-06-28 13:26:22 +02:00
if err != nil {
2024-08-16 11:32:04 +02:00
return nil , fmt . Errorf ( "failed to create transport for -remoteWrite.url=%q: %w" , * addr , err )
2020-06-28 13:26:22 +02:00
}
2024-05-30 17:54:42 +02:00
t . IdleConnTimeout = * idleConnectionTimeout
2024-07-15 23:00:14 +02:00
t . DialContext = netutil . NewStatDialFunc ( "vmalert_remotewrite" )
2020-06-28 13:26:22 +02:00
2023-12-20 20:35:16 +01:00
endpointParams , err := flagutil . ParseJSONMap ( * oauth2EndpointParams )
if err != nil {
return nil , fmt . Errorf ( "cannot parse JSON for -remoteWrite.oauth2.endpointParams=%s: %w" , * oauth2EndpointParams , err )
}
2022-03-10 12:09:12 +01:00
authCfg , err := utils . AuthConfig (
utils . WithBasicAuth ( * basicAuthUsername , * basicAuthPassword , * basicAuthPasswordFile ) ,
utils . WithBearer ( * bearerToken , * bearerTokenFile ) ,
2023-12-20 20:35:16 +01:00
utils . WithOAuth ( * oauth2ClientID , * oauth2ClientSecret , * oauth2ClientSecretFile , * oauth2TokenURL , * oauth2Scopes , endpointParams ) ,
2022-07-21 13:57:53 +02:00
utils . WithHeaders ( * headers ) )
2021-09-14 13:32:06 +02:00
if err != nil {
return nil , fmt . Errorf ( "failed to configure auth: %w" , err )
}
2020-06-28 13:26:22 +02:00
return NewClient ( ctx , Config {
2022-12-01 09:57:19 +01:00
Addr : * addr ,
AuthCfg : authCfg ,
Concurrency : * concurrency ,
MaxQueueSize : * maxQueueSize ,
MaxBatchSize : * maxBatchSize ,
FlushInterval : * flushInterval ,
Transport : t ,
2020-06-28 13:26:22 +02:00
} )
}