2020-05-04 17:48:02 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
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-11-05 12:41:14 +00:00
|
|
|
Server string `yaml:"server,omitempty"`
|
|
|
|
Token *promauth.Secret `yaml:"token"`
|
|
|
|
Datacenter string `yaml:"datacenter"`
|
2022-11-07 13:25:09 +00:00
|
|
|
|
2021-06-22 09:49:44 +00:00
|
|
|
// Namespace only supported at enterprise consul.
|
|
|
|
// https://www.consul.io/docs/enterprise/namespaces
|
2022-11-07 13:25:09 +00:00
|
|
|
Namespace string `yaml:"namespace,omitempty"`
|
2024-04-16 17:34:18 +00:00
|
|
|
// Partition only supported at enterprise consul.
|
2022-11-07 13:25:09 +00:00
|
|
|
// https://developer.hashicorp.com/consul/docs/enterprise/admin-partitions
|
|
|
|
Partition string `yaml:"partition,omitempty"`
|
|
|
|
|
2021-04-03 21:40:08 +00:00
|
|
|
Scheme string `yaml:"scheme,omitempty"`
|
|
|
|
Username string `yaml:"username"`
|
2021-11-05 12:41:14 +00:00
|
|
|
Password *promauth.Secret `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"`
|
2022-08-05 11:41:37 +00:00
|
|
|
AllowStale *bool `yaml:"allow_stale,omitempty"`
|
2023-04-26 17:16:27 +00:00
|
|
|
// See https://developer.hashicorp.com/consul/api-docs/features/filtering
|
|
|
|
// list of supported filters https://developer.hashicorp.com/consul/api-docs/catalog#filtering-1
|
|
|
|
Filter string `yaml:"filter,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.
|
2022-11-30 05:22:12 +00:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]*promutils.Labels, 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()
|
|
|
|
}
|
|
|
|
}
|