2021-06-22 10:33:37 +00:00
package http
import (
"flag"
"fmt"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
2022-11-30 05:22:12 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
2021-06-22 10:33:37 +00:00
"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-14 22:40:20 +00:00
"See https://docs.victoriametrics.com/sd_configs.html#http_sd_configs for details" )
2021-06-22 10:33:37 +00: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 18:21:08 +00:00
ProxyURL * proxy . URL ` yaml:"proxy_url,omitempty" `
2021-06-22 10:33:37 +00:00
ProxyClientConfig promauth . ProxyClientConfig ` yaml:",inline" `
}
// GetLabels returns http service discovery labels according to sdc.
2022-11-30 05:22:12 +00:00
func ( sdc * SDConfig ) GetLabels ( baseDir string ) ( [ ] * promutils . Labels , error ) {
2021-06-22 10:33:37 +00:00
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 10:40:33 +00:00
return addHTTPTargetLabels ( hts , sdc . URL ) , nil
2021-06-22 10:33:37 +00:00
}
2022-11-30 05:22:12 +00:00
func addHTTPTargetLabels ( src [ ] httpGroupTarget , sourceURL string ) [ ] * promutils . Labels {
ms := make ( [ ] * promutils . Labels , 0 , len ( src ) )
2021-06-22 10:33:37 +00:00
for _ , targetGroup := range src {
labels := targetGroup . Labels
for _ , target := range targetGroup . Targets {
2022-11-30 05:22:12 +00:00
m := promutils . NewLabels ( 2 + labels . Len ( ) )
m . AddFrom ( labels )
m . Add ( "__address__" , target )
m . Add ( "__meta_url" , sourceURL )
2023-02-23 01:01:01 +00:00
// Remove possible duplicate labels, which can appear after AddFrom() call
2022-11-30 05:22:12 +00:00
m . RemoveDuplicates ( )
2021-06-22 10:33:37 +00:00
ms = append ( ms , m )
}
}
return ms
}
2021-06-25 08:39:18 +00:00
// MustStop stops further usage for sdc.
func ( sdc * SDConfig ) MustStop ( ) {
configMap . Delete ( sdc )
}