lib/promscrape/discovery/consul: add __meta_consul_partition label in the same way as Prometheus does

See https://github.com/prometheus/prometheus/pull/11482
This commit is contained in:
Aliaksandr Valialkin 2022-11-07 15:25:09 +02:00
parent 9108a1d33f
commit abf7e4e72f
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
6 changed files with 25 additions and 3 deletions

View file

@ -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).

View file

@ -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_<key>`: 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_<key>`: each service metadata key value of the target

View file

@ -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"`

View file

@ -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,

View file

@ -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",

View file

@ -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)