2020-05-04 17:48:02 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-12-24 08:56:10 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-05-04 17:48:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SDConfig represents service discovery config for Consul.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config
|
|
|
|
type SDConfig struct {
|
2021-06-22 09:49:44 +00:00
|
|
|
Server string `yaml:"server,omitempty"`
|
|
|
|
Token *string `yaml:"token"`
|
|
|
|
Datacenter string `yaml:"datacenter"`
|
|
|
|
// Namespace only supported at enterprise consul.
|
|
|
|
// https://www.consul.io/docs/enterprise/namespaces
|
|
|
|
Namespace string `yaml:"namespace,omitempty"`
|
2021-04-03 21:40:08 +00:00
|
|
|
Scheme string `yaml:"scheme,omitempty"`
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password string `yaml:"password"`
|
2021-06-22 10:17:42 +00:00
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2021-10-26 18:21:08 +00:00
|
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
2021-04-03 21:40:08 +00: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 17:48:02 +00: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 13:53:42 +00:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]map[string]string, error) {
|
2020-05-04 17:48:02 +00:00
|
|
|
cfg, err := getAPIConfig(sdc, baseDir)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
2020-12-03 17:50:50 +00:00
|
|
|
ms := getServiceNodesLabels(cfg)
|
2020-05-04 17:48:02 +00:00
|
|
|
return ms, nil
|
|
|
|
}
|
2021-03-01 12:13:56 +00: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()
|
|
|
|
}
|
|
|
|
}
|