2021-06-22 12:33:37 +02:00
package http
import (
"flag"
"fmt"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
2022-11-30 06:22:12 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
2021-06-22 12:33:37 +02: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-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.
2022-11-30 06:22:12 +01:00
func ( sdc * SDConfig ) GetLabels ( baseDir string ) ( [ ] * promutils . Labels , error ) {
2021-06-22 12:33:37 +02: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 12:40:33 +02:00
return addHTTPTargetLabels ( hts , sdc . URL ) , nil
2021-06-22 12:33:37 +02:00
}
2022-11-30 06:22:12 +01:00
func addHTTPTargetLabels ( src [ ] httpGroupTarget , sourceURL string ) [ ] * promutils . Labels {
ms := make ( [ ] * promutils . Labels , 0 , len ( src ) )
2021-06-22 12:33:37 +02:00
for _ , targetGroup := range src {
labels := targetGroup . Labels
for _ , target := range targetGroup . Targets {
2022-11-30 06:22:12 +01:00
m := promutils . NewLabels ( 2 + labels . Len ( ) )
m . AddFrom ( labels )
m . Add ( "__address__" , target )
m . Add ( "__meta_url" , sourceURL )
2023-02-23 02:01:01 +01:00
// Remove possible duplicate labels, which can appear after AddFrom() call
2022-11-30 06:22:12 +01:00
m . RemoveDuplicates ( )
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 ( ) {
2023-07-27 23:47:53 +02:00
v := configMap . Delete ( sdc )
if v != nil {
cfg := v . ( * apiConfig )
cfg . client . Stop ( )
}
2021-06-25 10:39:18 +02:00
}