mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-23 12:31:07 +01:00
adds consul enterprise namespace support (#1400)
* adds consul enterprise namespace support * Update lib/promscrape/discovery/consul/consul.go Co-authored-by: Aliaksandr Valialkin <valyala@gmail.com>
This commit is contained in:
parent
3bc83d3b17
commit
58a2989fe7
@ -82,7 +82,13 @@ func newAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cw := newConsulWatcher(client, sdc, dc)
|
||||
namespace := sdc.Namespace
|
||||
// default namespace can be detected from env var.
|
||||
if namespace == "" {
|
||||
namespace = os.Getenv("CONSUL_NAMESPACE")
|
||||
}
|
||||
|
||||
cw := newConsulWatcher(client, sdc, dc, sdc.Namespace)
|
||||
cfg := &apiConfig{
|
||||
tagSeparator: tagSeparator,
|
||||
consulWatcher: cw,
|
||||
|
@ -11,9 +11,12 @@ import (
|
||||
//
|
||||
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config
|
||||
type SDConfig struct {
|
||||
Server string `yaml:"server,omitempty"`
|
||||
Token *string `yaml:"token"`
|
||||
Datacenter string `yaml:"datacenter"`
|
||||
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"`
|
||||
Scheme string `yaml:"scheme,omitempty"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
|
@ -34,12 +34,13 @@ type ServiceNode struct {
|
||||
//
|
||||
// See https://www.consul.io/api/health.html#list-nodes-for-service
|
||||
type Service struct {
|
||||
ID string
|
||||
Service string
|
||||
Address string
|
||||
Port int
|
||||
Tags []string
|
||||
Meta map[string]string
|
||||
ID string
|
||||
Service string
|
||||
Address string
|
||||
Namespace string
|
||||
Port int
|
||||
Tags []string
|
||||
Meta map[string]string
|
||||
}
|
||||
|
||||
// Node is Consul node.
|
||||
@ -81,6 +82,7 @@ func (sn *ServiceNode) appendTargetLabels(ms []map[string]string, serviceName, t
|
||||
"__meta_consul_address": sn.Node.Address,
|
||||
"__meta_consul_dc": sn.Node.Datacenter,
|
||||
"__meta_consul_health": aggregatedStatus(sn.Checks),
|
||||
"__meta_consul_namespace": sn.Service.Namespace,
|
||||
"__meta_consul_node": sn.Node.Node,
|
||||
"__meta_consul_service": serviceName,
|
||||
"__meta_consul_service_address": sn.Service.Address,
|
||||
|
@ -64,7 +64,7 @@ func TestParseServiceNodesSuccess(t *testing.T) {
|
||||
"Passing": 10,
|
||||
"Warning": 1
|
||||
},
|
||||
"Namespace": "default"
|
||||
"Namespace": "ns-dev"
|
||||
},
|
||||
"Checks": [
|
||||
{
|
||||
@ -118,6 +118,7 @@ func TestParseServiceNodesSuccess(t *testing.T) {
|
||||
"__meta_consul_dc": "dc1",
|
||||
"__meta_consul_health": "passing",
|
||||
"__meta_consul_metadata_instance_type": "t2.medium",
|
||||
"__meta_consul_namespace": "ns-dev",
|
||||
"__meta_consul_node": "foobar",
|
||||
"__meta_consul_service": "redis",
|
||||
"__meta_consul_service_address": "10.1.10.12",
|
||||
|
@ -42,11 +42,14 @@ type serviceWatcher struct {
|
||||
}
|
||||
|
||||
// newConsulWatcher creates new watcher and start background service discovery for Consul.
|
||||
func newConsulWatcher(client *discoveryutils.Client, sdc *SDConfig, datacenter string) *consulWatcher {
|
||||
func newConsulWatcher(client *discoveryutils.Client, sdc *SDConfig, datacenter, namespace string) *consulWatcher {
|
||||
baseQueryArgs := "?dc=" + url.QueryEscape(datacenter)
|
||||
if sdc.AllowStale {
|
||||
baseQueryArgs += "&stale"
|
||||
}
|
||||
if namespace != "" {
|
||||
baseQueryArgs += "&ns=" + namespace
|
||||
}
|
||||
for k, v := range sdc.NodeMeta {
|
||||
baseQueryArgs += "&node-meta=" + url.QueryEscape(k+":"+v)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user