2020-05-04 19:48:02 +02:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-12-24 09:56:10 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-05-04 19:48:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// SDConfig represents service discovery config for Consul.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config
|
|
|
|
type SDConfig struct {
|
2021-11-05 13:41:14 +01:00
|
|
|
Server string `yaml:"server,omitempty"`
|
|
|
|
Token *promauth.Secret `yaml:"token"`
|
|
|
|
Datacenter string `yaml:"datacenter"`
|
2021-06-22 11:49:44 +02:00
|
|
|
// Namespace only supported at enterprise consul.
|
|
|
|
// https://www.consul.io/docs/enterprise/namespaces
|
|
|
|
Namespace string `yaml:"namespace,omitempty"`
|
2021-04-03 23:40:08 +02:00
|
|
|
Scheme string `yaml:"scheme,omitempty"`
|
|
|
|
Username string `yaml:"username"`
|
2021-11-05 13:41:14 +01:00
|
|
|
Password *promauth.Secret `yaml:"password"`
|
2021-06-22 12:17:42 +02:00
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2021-10-26 20:21:08 +02:00
|
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
2021-04-03 23:40:08 +02:00
|
|
|
ProxyClientConfig promauth.ProxyClientConfig `yaml:",inline"`
|
|
|
|
Services []string `yaml:"services,omitempty"`
|
|
|
|
Tags []string `yaml:"tags,omitempty"`
|
|
|
|
NodeMeta map[string]string `yaml:"node_meta,omitempty"`
|
|
|
|
TagSeparator *string `yaml:"tag_separator,omitempty"`
|
|
|
|
AllowStale bool `yaml:"allow_stale,omitempty"`
|
2020-05-04 19:48:02 +02:00
|
|
|
// RefreshInterval time.Duration `yaml:"refresh_interval"`
|
|
|
|
// refresh_interval is obtained from `-promscrape.consulSDCheckInterval` command-line option.
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabels returns Consul labels according to sdc.
|
2021-02-26 14:53:42 +01:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]map[string]string, error) {
|
2020-05-04 19:48:02 +02:00
|
|
|
cfg, err := getAPIConfig(sdc, baseDir)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
2020-05-04 19:48:02 +02:00
|
|
|
}
|
2020-12-03 18:50:50 +01:00
|
|
|
ms := getServiceNodesLabels(cfg)
|
2020-05-04 19:48:02 +02:00
|
|
|
return ms, nil
|
|
|
|
}
|
2021-03-01 13:13:56 +01:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
|
|
|
v := configMap.Delete(sdc)
|
|
|
|
if v != nil {
|
|
|
|
// v can be nil if GetLabels wasn't called yet.
|
|
|
|
cfg := v.(*apiConfig)
|
|
|
|
cfg.mustStop()
|
|
|
|
}
|
|
|
|
}
|