2024-01-15 09:13:22 +00:00
|
|
|
// SDConfig represents service discovery config for hetzner cloud and hetzner robot.
|
|
|
|
package hetzner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2024-01-15 15:11:45 +00:00
|
|
|
)
|
2024-01-15 09:13:22 +00:00
|
|
|
|
|
|
|
// SDCheckInterval defines interval for targets refresh.
|
2024-01-20 14:52:41 +00:00
|
|
|
var SDCheckInterval = flag.Duration("promscrape.hetznerSDCheckInterval", time.Minute, "Interval for checking for changes in Hetzner API. "+
|
2024-01-15 09:13:22 +00:00
|
|
|
"This works only if hetzner_sd_configs is configured in '-promscrape.config' file. "+
|
|
|
|
"See https://docs.victoriametrics.com/sd_configs.html#hetzner_sd_configs for details")
|
|
|
|
|
2024-01-15 15:11:45 +00:00
|
|
|
// SDConfig represents service discovery config for Hetzner.
|
|
|
|
//
|
2024-01-15 09:13:22 +00:00
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#hetzner_sd_config
|
|
|
|
type SDConfig struct {
|
2024-01-20 14:52:41 +00:00
|
|
|
Role string `yaml:"role"`
|
2024-01-15 09:13:22 +00:00
|
|
|
Port *int `yaml:"port,omitempty"`
|
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
|
|
|
ProxyClientConfig promauth.ProxyClientConfig `yaml:",inline"`
|
|
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:52:41 +00:00
|
|
|
// GetLabels returns Hetzner target labels according to sdc.
|
2024-01-15 09:13:22 +00:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]*promutils.Labels, error) {
|
|
|
|
cfg, err := getAPIConfig(sdc, baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
|
|
|
}
|
|
|
|
switch sdc.Role {
|
|
|
|
case "robot":
|
|
|
|
return getRobotServerLabels(cfg)
|
|
|
|
case "hcloud":
|
2024-01-20 14:52:41 +00:00
|
|
|
return getHCloudServerLabels(cfg)
|
2024-01-15 09:13:22 +00:00
|
|
|
default:
|
2024-01-20 14:52:41 +00:00
|
|
|
// The sdc.Role must be already verified by getAPIConfig().
|
|
|
|
panic(fmt.Errorf("BUG: unexpected role=%q; must be one of `robot` or `hcloud`", sdc.Role))
|
2024-01-15 09:13:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
|
|
|
configMap.Delete(sdc)
|
|
|
|
}
|