2022-07-21 18:49:52 +02:00
package pushmetrics
import (
"flag"
"strings"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/appmetrics"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
2022-07-22 12:35:58 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
2022-07-21 18:49:52 +02:00
"github.com/VictoriaMetrics/metrics"
)
var (
pushURL = flagutil . NewArray ( "pushmetrics.url" , "Optional URL to push metrics exposed at /metrics page. See https://docs.victoriametrics.com/#push-metrics . " +
"By default metrics exposed at /metrics page aren't pushed to any remote storage" )
2022-07-26 19:40:19 +02:00
pushInterval = flag . Duration ( "pushmetrics.interval" , 10 * time . Second , "Interval for pushing metrics to -pushmetrics.url" )
2022-07-26 18:24:24 +02:00
pushExtraLabel = flagutil . NewArray ( "pushmetrics.extraLabel" , "Optional labels to add to metrics pushed to -pushmetrics.url . " +
` For example, -pushmetrics.extraLabel='instance="foo"' adds instance="foo" label to all the metrics pushed to -pushmetrics.url ` )
2022-07-21 18:49:52 +02:00
)
func init ( ) {
// The -pushmetrics.url flag can contain basic auth creds, so it mustn't be visible when exposing the flags.
flagutil . RegisterSecretFlag ( "pushmetrics.url" )
}
2022-07-22 12:35:58 +02:00
// Init must be called after logger.Init
2022-07-21 18:49:52 +02:00
func Init ( ) {
2022-07-26 18:24:24 +02:00
extraLabels := strings . Join ( * pushExtraLabel , "," )
2022-07-21 18:49:52 +02:00
for _ , pu := range * pushURL {
2022-07-22 12:35:58 +02:00
if err := metrics . InitPushExt ( pu , * pushInterval , extraLabels , appmetrics . WritePrometheusMetrics ) ; err != nil {
logger . Fatalf ( "cannot initialize pushmetrics: %s" , err )
}
2022-07-21 18:49:52 +02:00
}
}