2021-06-22 12:33:37 +02:00
package http
import (
"flag"
"fmt"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
)
// SDCheckInterval defines interval for targets refresh.
var SDCheckInterval = flag . Duration ( "promscrape.httpSDCheckInterval" , time . Minute , "Interval for checking for changes in http endpoint service discovery. " +
"This works only if http_sd_configs is configured in '-promscrape.config' file. " +
2022-08-15 00:40:20 +02:00
"See https://docs.victoriametrics.com/sd_configs.html#http_sd_configs for details" )
2021-06-22 12:33:37 +02:00
// SDConfig represents service discovery config for http.
//
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#http_sd_config
type SDConfig struct {
URL string ` yaml:"url" `
HTTPClientConfig promauth . HTTPClientConfig ` yaml:",inline" `
2021-10-26 20:21:08 +02:00
ProxyURL * proxy . URL ` yaml:"proxy_url,omitempty" `
2021-06-22 12:33:37 +02:00
ProxyClientConfig promauth . ProxyClientConfig ` yaml:",inline" `
}
// GetLabels returns http service discovery labels according to sdc.
func ( sdc * SDConfig ) GetLabels ( baseDir string ) ( [ ] map [ string ] string , error ) {
cfg , err := getAPIConfig ( sdc , baseDir )
if err != nil {
return nil , fmt . Errorf ( "cannot get API config: %w" , err )
}
hts , err := getHTTPTargets ( cfg )
if err != nil {
return nil , err
}
2021-06-22 12:40:33 +02:00
return addHTTPTargetLabels ( hts , sdc . URL ) , nil
2021-06-22 12:33:37 +02:00
}
2021-06-22 12:40:33 +02:00
func addHTTPTargetLabels ( src [ ] httpGroupTarget , sourceURL string ) [ ] map [ string ] string {
2021-06-22 12:33:37 +02:00
ms := make ( [ ] map [ string ] string , 0 , len ( src ) )
for _ , targetGroup := range src {
labels := targetGroup . Labels
for _ , target := range targetGroup . Targets {
m := make ( map [ string ] string , len ( labels ) )
for k , v := range labels {
m [ k ] = v
}
m [ "__address__" ] = target
2021-06-22 12:40:33 +02:00
m [ "__meta_url" ] = sourceURL
2021-06-22 12:33:37 +02:00
ms = append ( ms , m )
}
}
return ms
}
2021-06-25 10:39:18 +02:00
// MustStop stops further usage for sdc.
func ( sdc * SDConfig ) MustStop ( ) {
configMap . Delete ( sdc )
}