From abf7e4e72f2abfc46513ccd2c867977befe52be8 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 7 Nov 2022 15:25:09 +0200 Subject: [PATCH] lib/promscrape/discovery/consul: add `__meta_consul_partition` label in the same way as Prometheus does See https://github.com/prometheus/prometheus/pull/11482 --- docs/CHANGELOG.md | 2 ++ docs/sd_configs.md | 8 ++++++++ lib/promscrape/discovery/consul/consul.go | 7 ++++++- lib/promscrape/discovery/consul/service_node.go | 2 ++ lib/promscrape/discovery/consul/service_node_test.go | 4 +++- lib/promscrape/discovery/consul/watch.go | 5 ++++- 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3aef080f2..ef37279e0 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -15,6 +15,8 @@ The following tip changes can be tested by building VictoriaMetrics components f ## tip +* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): expose `__meta_consul_partition` label for targets discovered via [consul_sd_configs](https://docs.victoriametrics.com/sd_configs.html#consul_sd_configs) in the same way as [Prometheus 2.40 does](https://github.com/prometheus/prometheus/pull/11482). + * BUGFIX: properly register new time series in per-day inverted index if they were ingested during the last 10 seconds of the day. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3309). Thanks to @lmarszal for the bugreport and for the [initial fix](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/3320). diff --git a/docs/sd_configs.md b/docs/sd_configs.md index bbe14d061..2562c6b1b 100644 --- a/docs/sd_configs.md +++ b/docs/sd_configs.md @@ -112,9 +112,15 @@ scrape_configs: # datacenter: "..." # namespace is an optional Consul namespace. + # See https://developer.hashicorp.com/consul/docs/enterprise/namespaces # If the namespace isn't specified, then it is read from CONSUL_NAMESPACE environment var. # namespace: "..." + # partition is an optional Consul partition. + # See https://developer.hashicorp.com/consul/docs/enterprise/admin-partitions + # If partition isn't specified, then the default partition is used. + # partition: "..." + # scheme is an optional scheme (http or https) to use for connecting to Consul server. # By default http scheme is used. # scheme: "..." @@ -151,7 +157,9 @@ The following meta labels are available on discovered targets during [relabeling * `__meta_consul_dc`: the datacenter name for the target * `__meta_consul_health`: the health status of the service * `__meta_consul_metadata_`: each node metadata key value of the target +* `__meta_consul_namespace`: namespace of the service - see [namespace docs](https://developer.hashicorp.com/consul/docs/enterprise/namespaces) * `__meta_consul_node`: the node name defined for the target +* `__meta_consul_partition`: partition of the service - see [partition docs](https://developer.hashicorp.com/consul/docs/enterprise/admin-partitions) * `__meta_consul_service_address`: the service address of the target * `__meta_consul_service_id`: the service ID of the target * `__meta_consul_service_metadata_`: each service metadata key value of the target diff --git a/lib/promscrape/discovery/consul/consul.go b/lib/promscrape/discovery/consul/consul.go index c2ce8658d..aa4779d5f 100644 --- a/lib/promscrape/discovery/consul/consul.go +++ b/lib/promscrape/discovery/consul/consul.go @@ -14,9 +14,14 @@ type SDConfig struct { Server string `yaml:"server,omitempty"` Token *promauth.Secret `yaml:"token"` Datacenter string `yaml:"datacenter"` + // Namespace only supported at enterprise consul. // https://www.consul.io/docs/enterprise/namespaces - Namespace string `yaml:"namespace,omitempty"` + Namespace string `yaml:"namespace,omitempty"` + // Partition only supported at enteprise consul. + // https://developer.hashicorp.com/consul/docs/enterprise/admin-partitions + Partition string `yaml:"partition,omitempty"` + Scheme string `yaml:"scheme,omitempty"` Username string `yaml:"username"` Password *promauth.Secret `yaml:"password"` diff --git a/lib/promscrape/discovery/consul/service_node.go b/lib/promscrape/discovery/consul/service_node.go index 23f3a0003..60a198b1a 100644 --- a/lib/promscrape/discovery/consul/service_node.go +++ b/lib/promscrape/discovery/consul/service_node.go @@ -38,6 +38,7 @@ type Service struct { Service string Address string Namespace string + Partition string Port int Tags []string Meta map[string]string @@ -83,6 +84,7 @@ func (sn *ServiceNode) appendTargetLabels(ms []map[string]string, serviceName, t "__meta_consul_dc": sn.Node.Datacenter, "__meta_consul_health": aggregatedStatus(sn.Checks), "__meta_consul_namespace": sn.Service.Namespace, + "__meta_consul_partition": sn.Service.Partition, "__meta_consul_node": sn.Node.Node, "__meta_consul_service": serviceName, "__meta_consul_service_address": sn.Service.Address, diff --git a/lib/promscrape/discovery/consul/service_node_test.go b/lib/promscrape/discovery/consul/service_node_test.go index 68ad5db41..848181c26 100644 --- a/lib/promscrape/discovery/consul/service_node_test.go +++ b/lib/promscrape/discovery/consul/service_node_test.go @@ -64,7 +64,8 @@ func TestParseServiceNodesSuccess(t *testing.T) { "Passing": 10, "Warning": 1 }, - "Namespace": "ns-dev" + "Namespace": "ns-dev", + "Partition": "part-foobar" }, "Checks": [ { @@ -120,6 +121,7 @@ func TestParseServiceNodesSuccess(t *testing.T) { "__meta_consul_metadata_instance_type": "t2.medium", "__meta_consul_namespace": "ns-dev", "__meta_consul_node": "foobar", + "__meta_consul_partition": "part-foobar", "__meta_consul_service": "redis", "__meta_consul_service_address": "10.1.10.12", "__meta_consul_service_id": "redis", diff --git a/lib/promscrape/discovery/consul/watch.go b/lib/promscrape/discovery/consul/watch.go index 0804a0468..4f10e0fdc 100644 --- a/lib/promscrape/discovery/consul/watch.go +++ b/lib/promscrape/discovery/consul/watch.go @@ -49,7 +49,10 @@ func newConsulWatcher(client *discoveryutils.Client, sdc *SDConfig, datacenter, baseQueryArgs += "&stale" } if namespace != "" { - baseQueryArgs += "&ns=" + namespace + baseQueryArgs += "&ns=" + url.QueryEscape(namespace) + } + if sdc.Partition != "" { + baseQueryArgs += "&partition=" + url.QueryEscape(sdc.Partition) } for k, v := range sdc.NodeMeta { baseQueryArgs += "&node-meta=" + url.QueryEscape(k+":"+v)