From 419e1733d50ca2e2a6afcd2a577a7ed7f0b98724 Mon Sep 17 00:00:00 2001 From: Xavier Vello Date: Thu, 11 Oct 2018 13:57:50 +0200 Subject: [PATCH] Fix hostname override and type for status_report.count metrics (#2372) --- .../datadog_checks/base/checks/openmetrics/mixins.py | 6 ++++++ .../datadog_checks/kubernetes_state/kubernetes_state.py | 3 ++- kubernetes_state/metadata.csv | 4 ++-- kubernetes_state/tests/fixtures/prometheus.txt | 1 + kubernetes_state/tests/test_kubernetes_state.py | 4 +++- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/datadog_checks_base/datadog_checks/base/checks/openmetrics/mixins.py b/datadog_checks_base/datadog_checks/base/checks/openmetrics/mixins.py index 5b34f9e46fe94..4de33d14d4d83 100644 --- a/datadog_checks_base/datadog_checks/base/checks/openmetrics/mixins.py +++ b/datadog_checks_base/datadog_checks/base/checks/openmetrics/mixins.py @@ -446,6 +446,12 @@ def send_request(self, endpoint, scraper_config, headers=None): return requests.get(endpoint, headers=headers, stream=True, timeout=scraper_config['prometheus_timeout'], cert=cert, verify=verify, auth=auth) + def get_hostname_for_sample(self, sample, scraper_config): + """ + Expose the label_to_hostname mapping logic to custom handler methods + """ + return self._get_hostname(None, sample, scraper_config) + def _submit(self, metric_name, metric, scraper_config, hostname=None): """ For each sample in the metric, report it as a gauge with all labels as tags diff --git a/kubernetes_state/datadog_checks/kubernetes_state/kubernetes_state.py b/kubernetes_state/datadog_checks/kubernetes_state/kubernetes_state.py index 468a479131915..40a05f3aa8bc7 100644 --- a/kubernetes_state/datadog_checks/kubernetes_state/kubernetes_state.py +++ b/kubernetes_state/datadog_checks/kubernetes_state/kubernetes_state.py @@ -450,7 +450,8 @@ def _submit_metric_kube_pod_container_status_reason(self, metric, metric_suffix, if 'pod' in sample[self.SAMPLE_LABELS]: tags.append(self._format_tag('pod', sample[self.SAMPLE_LABELS]['pod'], scraper_config)) - self.count(metric_name, sample[self.SAMPLE_VALUE], tags + scraper_config['custom_tags']) + self.gauge(metric_name, sample[self.SAMPLE_VALUE], tags + scraper_config['custom_tags'], + hostname=self.get_hostname_for_sample(sample, scraper_config)) def kube_pod_container_status_waiting_reason(self, metric, scraper_config): self._submit_metric_kube_pod_container_status_reason(metric, '.container.status_report.count.waiting', diff --git a/kubernetes_state/metadata.csv b/kubernetes_state/metadata.csv index ac9e6783b5098..b0d868db7ccc5 100644 --- a/kubernetes_state/metadata.csv +++ b/kubernetes_state/metadata.csv @@ -2,9 +2,9 @@ metric_name,metric_type,interval,unit_name,per_unit_name,description,orientation kubernetes_state.container.ready,gauge,,,,Whether the containers readiness check succeeded,0,kubernetes,k8s_state.container.rdy kubernetes_state.container.running,gauge,,,,Whether the container is currently in running state,0,kubernetes,k8s_state.container.running kubernetes_state.container.terminated,gauge,,,,Whether the container is currently in terminated state,0,kubernetes,k8s_state.container.term -kubernetes_state.container.status_report.count.terminated,count,,,,Count of the containers currently reporting a in terminated state with the reason as a tag,-1,kubernetes,k8s_state.container.status_report.count.term +kubernetes_state.container.status_report.count.terminated,gauge,,,,Count of the containers currently reporting a in terminated state with the reason as a tag,-1,kubernetes,k8s_state.container.status_report.count.term kubernetes_state.container.waiting,gauge,,,,Whether the container is currently in waiting state,0,kubernetes,k8s_state.container.wait -kubernetes_state.container.status_report.count.waiting,count,,,,Count of the containers currently reporting a in waiting state with the reason as a tag,-1,kubernetes,k8s_state.container.status_report.count.wait +kubernetes_state.container.status_report.count.waiting,gauge,,,,Count of the containers currently reporting a in waiting state with the reason as a tag,-1,kubernetes,k8s_state.container.status_report.count.wait kubernetes_state.container.gpu.request,gauge,,,,The number of requested gpu devices by a container,0,kubernetes,k8s_state.container.gpu.request kubernetes_state.container.gpu.limit,gauge,,,,The limit on gpu devices to be used by a container,0,kubernetes,k8s_state.container.gpu.limit kubernetes_state.container.restarts,gauge,,,,The number of restarts per container,-1,kubernetes,k8s_state.container.restarts diff --git a/kubernetes_state/tests/fixtures/prometheus.txt b/kubernetes_state/tests/fixtures/prometheus.txt index 1992d477caf0e..6aba4f487a238 100644 --- a/kubernetes_state/tests/fixtures/prometheus.txt +++ b/kubernetes_state/tests/fixtures/prometheus.txt @@ -506,6 +506,7 @@ kube_pod_info{created_by_kind="ReplicaSet",created_by_name="kube-dns-1326421443" kube_pod_info{created_by_kind="ReplicaSet",created_by_name="tiller-deploy-1651615695",host_ip="192.168.99.100",namespace="kube-system",node="minikube",pod="tiller-deploy-1651615695-dcphn",pod_ip="172.17.0.6"} 1 kube_pod_info{created_by_kind="ReplicationController",created_by_name="kubernetes-dashboard",host_ip="192.168.99.100",namespace="kube-system",node="minikube",pod="kubernetes-dashboard-zhtjl",pod_ip="172.17.0.2"} 1 kube_pod_info{created_by_kind="ReplicationController",created_by_name="registry-creds",host_ip="192.168.99.100",namespace="kube-system",node="minikube",pod="registry-creds-hq249",pod_ip=""} 1 +kube_pod_info{created_by_kind="Job",created_by_name="hello-1509998400",host_ip="192.168.99.100",node="minikube",namespace="ns2",pod="pod2",pod_ip=""} 1 # HELP kube_pod_labels Kubernetes labels converted to Prometheus labels. # TYPE kube_pod_labels gauge kube_pod_labels{namespace="default",pod="nxf-126"} 1 diff --git a/kubernetes_state/tests/test_kubernetes_state.py b/kubernetes_state/tests/test_kubernetes_state.py index 5cef01378e665..b5b1b44411544 100644 --- a/kubernetes_state/tests/test_kubernetes_state.py +++ b/kubernetes_state/tests/test_kubernetes_state.py @@ -159,7 +159,9 @@ HOSTNAMES = { NAMESPACE + '.pod.ready': 'minikube', - NAMESPACE + '.pod.scheduled': 'minikube' + NAMESPACE + '.pod.scheduled': 'minikube', + NAMESPACE + '.container.status_report.count.waiting': 'minikube', + NAMESPACE + '.container.status_report.count.terminated': 'minikube' } ZERO_METRICS = [