feat (inputs/prometheus): add setting to set custom namespace label name to avoid conflicts (#11538)
This commit is contained in:
parent
9cfd7d185c
commit
f4e76893d1
|
|
@ -53,6 +53,9 @@ in Prometheus format.
|
||||||
## Restricts Kubernetes monitoring to a single namespace
|
## Restricts Kubernetes monitoring to a single namespace
|
||||||
## ex: monitor_kubernetes_pods_namespace = "default"
|
## ex: monitor_kubernetes_pods_namespace = "default"
|
||||||
# monitor_kubernetes_pods_namespace = ""
|
# monitor_kubernetes_pods_namespace = ""
|
||||||
|
## The name of the label for the pod that is being scraped.
|
||||||
|
## Default is 'namespace' but this can conflict with metrics that have the label 'namespace'
|
||||||
|
# pod_namespace_label_name = "namespace"
|
||||||
# label selector to target pods which have the label
|
# label selector to target pods which have the label
|
||||||
# kubernetes_label_selector = "env=dev,app=nginx"
|
# kubernetes_label_selector = "env=dev,app=nginx"
|
||||||
# field selector to target pods
|
# field selector to target pods
|
||||||
|
|
@ -159,6 +162,10 @@ the following annotation are supported:
|
||||||
Using the `monitor_kubernetes_pods_namespace` option allows you to limit which
|
Using the `monitor_kubernetes_pods_namespace` option allows you to limit which
|
||||||
pods you are scraping.
|
pods you are scraping.
|
||||||
|
|
||||||
|
The setting `pod_namespace_label_name` allows you to change the label name for
|
||||||
|
the namespace of the pod you are scraping. The default is `namespace`, but this
|
||||||
|
will overwrite a label with the name `namespace` from a metric scraped.
|
||||||
|
|
||||||
Using `pod_scrape_scope = "node"` allows more scalable scraping for pods which
|
Using `pod_scrape_scope = "node"` allows more scalable scraping for pods which
|
||||||
will scrape pods only in the node that telegraf is running. It will fetch the
|
will scrape pods only in the node that telegraf is running. It will fetch the
|
||||||
pod list locally from the node's kubelet. This will require running Telegraf in
|
pod list locally from the node's kubelet. This will require running Telegraf in
|
||||||
|
|
|
||||||
|
|
@ -373,8 +373,14 @@ func registerPod(pod *corev1.Pod, p *Prometheus) {
|
||||||
if tags == nil {
|
if tags == nil {
|
||||||
tags = map[string]string{}
|
tags = map[string]string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags["pod_name"] = pod.Name
|
tags["pod_name"] = pod.Name
|
||||||
tags["namespace"] = pod.Namespace
|
podNamespace := "namespace"
|
||||||
|
if p.PodNamespaceLabelName != "" {
|
||||||
|
podNamespace = p.PodNamespaceLabelName
|
||||||
|
}
|
||||||
|
tags[podNamespace] = pod.Namespace
|
||||||
|
|
||||||
// add labels as metrics tags
|
// add labels as metrics tags
|
||||||
for k, v := range pod.Labels {
|
for k, v := range pod.Labels {
|
||||||
tags[k] = v
|
tags[k] = v
|
||||||
|
|
|
||||||
|
|
@ -119,6 +119,27 @@ func TestDeletePods(t *testing.T) {
|
||||||
require.Equal(t, 0, len(prom.kubernetesPods))
|
require.Equal(t, 0, len(prom.kubernetesPods))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKeepDefaultNamespaceLabelName(t *testing.T) {
|
||||||
|
prom := &Prometheus{Log: testutil.Logger{}}
|
||||||
|
|
||||||
|
p := pod()
|
||||||
|
p.Annotations = map[string]string{"prometheus.io/scrape": "true"}
|
||||||
|
registerPod(p, prom)
|
||||||
|
tags := prom.kubernetesPods["http://127.0.0.1:9102/metrics"].Tags
|
||||||
|
require.Equal(t, "default", tags["namespace"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChangeNamespaceLabelName(t *testing.T) {
|
||||||
|
prom := &Prometheus{Log: testutil.Logger{}, PodNamespaceLabelName: "pod_namespace"}
|
||||||
|
|
||||||
|
p := pod()
|
||||||
|
p.Annotations = map[string]string{"prometheus.io/scrape": "true"}
|
||||||
|
registerPod(p, prom)
|
||||||
|
tags := prom.kubernetesPods["http://127.0.0.1:9102/metrics"].Tags
|
||||||
|
require.Equal(t, "default", tags["pod_namespace"])
|
||||||
|
require.Equal(t, "", tags["namespace"])
|
||||||
|
}
|
||||||
|
|
||||||
func TestPodHasMatchingNamespace(t *testing.T) {
|
func TestPodHasMatchingNamespace(t *testing.T) {
|
||||||
prom := &Prometheus{Log: testutil.Logger{}, PodNamespace: "default"}
|
prom := &Prometheus{Log: testutil.Logger{}, PodNamespace: "default"}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,15 +75,16 @@ type Prometheus struct {
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
|
|
||||||
// Should we scrape Kubernetes services for prometheus annotations
|
// Should we scrape Kubernetes services for prometheus annotations
|
||||||
MonitorPods bool `toml:"monitor_kubernetes_pods"`
|
MonitorPods bool `toml:"monitor_kubernetes_pods"`
|
||||||
PodScrapeScope string `toml:"pod_scrape_scope"`
|
PodScrapeScope string `toml:"pod_scrape_scope"`
|
||||||
NodeIP string `toml:"node_ip"`
|
NodeIP string `toml:"node_ip"`
|
||||||
PodScrapeInterval int `toml:"pod_scrape_interval"`
|
PodScrapeInterval int `toml:"pod_scrape_interval"`
|
||||||
PodNamespace string `toml:"monitor_kubernetes_pods_namespace"`
|
PodNamespace string `toml:"monitor_kubernetes_pods_namespace"`
|
||||||
lock sync.Mutex
|
PodNamespaceLabelName string `toml:"pod_namespace_label_name"`
|
||||||
kubernetesPods map[string]URLAndAddress
|
lock sync.Mutex
|
||||||
cancel context.CancelFunc
|
kubernetesPods map[string]URLAndAddress
|
||||||
wg sync.WaitGroup
|
cancel context.CancelFunc
|
||||||
|
wg sync.WaitGroup
|
||||||
|
|
||||||
// Only for monitor_kubernetes_pods=true and pod_scrape_scope="node"
|
// Only for monitor_kubernetes_pods=true and pod_scrape_scope="node"
|
||||||
podLabelSelector labels.Selector
|
podLabelSelector labels.Selector
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,9 @@
|
||||||
## Restricts Kubernetes monitoring to a single namespace
|
## Restricts Kubernetes monitoring to a single namespace
|
||||||
## ex: monitor_kubernetes_pods_namespace = "default"
|
## ex: monitor_kubernetes_pods_namespace = "default"
|
||||||
# monitor_kubernetes_pods_namespace = ""
|
# monitor_kubernetes_pods_namespace = ""
|
||||||
|
## The name of the label for the pod that is being scraped.
|
||||||
|
## Default is 'namespace' but this can conflict with metrics that have the label 'namespace'
|
||||||
|
# pod_namespace_label_name = "namespace"
|
||||||
# label selector to target pods which have the label
|
# label selector to target pods which have the label
|
||||||
# kubernetes_label_selector = "env=dev,app=nginx"
|
# kubernetes_label_selector = "env=dev,app=nginx"
|
||||||
# field selector to target pods
|
# field selector to target pods
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue