Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not tag container restarts/state metrics by container_id anymore #3424

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions kubelet/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ def get_conn_info():
return {'url': 'http://127.0.0.1:10255'}


def get_tags(entity, high_card):
tag_store = {}
return tag_store.get(entity, [])


kubeutil = type(sys)('kubeutil')
kubeutil.get_connection_info = get_conn_info
sys.modules['kubeutil'] = kubeutil

tagger = type(sys)('tagger')
tagger.get_tags = get_tags
sys.modules['tagger'] = tagger
10 changes: 5 additions & 5 deletions kubelet/datadog_checks/kubelet/cadvisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import requests

from .common import tags_for_docker, tags_for_pod, is_static_pending_pod, get_pod_by_uid
from tagger import get_tags
from datadog_checks.base.utils.tagging import tagger

NAMESPACE = "kubernetes"
DEFAULT_MAX_DEPTH = 10
Expand Down Expand Up @@ -162,12 +162,12 @@ def _update_container_metrics(self, instance, subcontainer, pod_list, pod_list_u

# Let's see who we have here
if is_pod:
tags = tags_for_pod(pod_uid, True)
tags = tags_for_pod(pod_uid, tagger.HIGH)
elif in_static_pod and k_container_name:
# FIXME static pods don't have container statuses so we can't
# get the container id with the scheme, assuming docker here
tags = tags_for_docker(subcontainer_id, True)
tags += tags_for_pod(pod_uid, True)
tags = tags_for_docker(subcontainer_id, tagger.HIGH)
tags += tags_for_pod(pod_uid, tagger.HIGH)
tags.append("kube_container_name:%s" % k_container_name)
else: # Standard container
cid = pod_list_utils.get_cid_by_name_tuple(
Expand All @@ -176,7 +176,7 @@ def _update_container_metrics(self, instance, subcontainer, pod_list, pod_list_u
if pod_list_utils.is_excluded(cid):
self.log.debug("Filtering out " + cid)
return
tags = get_tags(cid, True)
tags = tagger.tag(cid, tagger.HIGH)

if not tags:
self.log.debug("Subcontainer {} doesn't have tags, skipping.".format(subcontainer_id))
Expand Down
6 changes: 3 additions & 3 deletions kubelet/datadog_checks/kubelet/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

from tagger import get_tags
from datadog_checks.base.utils.tagging import tagger

try:
from containers import is_excluded
Expand All @@ -25,15 +25,15 @@ def tags_for_pod(pod_id, cardinality):
Queries the tagger for a given pod uid
:return: string array, empty if pod not found
"""
return get_tags('kubernetes_pod://%s' % pod_id, cardinality)
return tagger.tag('kubernetes_pod://%s' % pod_id, cardinality)


def tags_for_docker(cid, cardinality):
"""
Queries the tagger for a given container id
:return: string array, empty if container not found
"""
return get_tags('docker://%s' % cid, cardinality)
return tagger.tag('docker://%s' % cid, cardinality)


def get_pod_by_uid(uid, podlist):
Expand Down
10 changes: 5 additions & 5 deletions kubelet/datadog_checks/kubelet/kubelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import requests

from datadog_checks.base.utils.date import parse_rfc3339, UTC
from datadog_checks.base.utils.tagging import tagger
from datadog_checks.checks import AgentCheck
from datadog_checks.checks.openmetrics import OpenMetricsBaseCheck
from datadog_checks.errors import CheckException
from kubeutil import get_connection_info
from six import iteritems
from six.moves.urllib.parse import urljoin
from tagger import get_tags

from .common import CADVISOR_DEFAULT_PORT, PodListUtils, KubeletCredentials
from .cadvisor import CadvisorScraper
Expand Down Expand Up @@ -367,7 +367,7 @@ def _report_pods_running(self, pods, instance_tags):
if "running" not in container.get('state', {}):
continue
has_container_running = True
tags = get_tags(container_id, False) or None
tags = tagger.tag(container_id, tagger.LOW) or None
if not tags:
continue
tags += instance_tags
Expand All @@ -380,7 +380,7 @@ def _report_pods_running(self, pods, instance_tags):
if not pod_id:
self.log.debug('skipping pod with no uid')
continue
tags = get_tags('kubernetes_pod://%s' % pod_id, False) or None
tags = tagger.tag('kubernetes_pod://%s' % pod_id, tagger.LOW) or None
if not tags:
continue
tags += instance_tags
Expand Down Expand Up @@ -417,7 +417,7 @@ def _report_container_spec_metrics(self, pod_list, instance_tags):
if self.pod_list_utils.is_excluded(cid, pod_uid):
continue

tags = get_tags('%s' % cid, True) + instance_tags
tags = tagger.tag('%s' % cid, tagger.HIGH) + instance_tags

try:
for resource, value_str in iteritems(ctr.get('resources', {}).get('requests', {})):
Expand Down Expand Up @@ -454,7 +454,7 @@ def _report_container_state_metrics(self, pod_list, instance_tags):
if self.pod_list_utils.is_excluded(cid, pod_uid):
continue

tags = get_tags('%s' % cid, True) + instance_tags
tags = tagger.tag('%s' % cid, tagger.ORCHESTRATOR) + instance_tags

restart_count = ctr_status.get('restartCount', 0)
self.gauge(self.NAMESPACE + '.containers.restarts', restart_count, tags)
Expand Down
14 changes: 7 additions & 7 deletions kubelet/datadog_checks/kubelet/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from six import iteritems

from datadog_checks.checks.openmetrics import OpenMetricsBaseCheck
from tagger import get_tags
from datadog_checks.base.utils.tagging import tagger

from .common import is_static_pending_pod, get_pod_by_uid

Expand Down Expand Up @@ -278,14 +278,14 @@ def _process_container_metric(self, type, metric_name, metric, scraper_config):
if self.pod_list_utils.is_excluded(c_id, pod_uid):
continue

tags = get_tags(c_id, True)
tags = tagger.tag(c_id, tagger.HIGH)
tags += scraper_config['custom_tags']

# FIXME we are forced to do that because the Kubelet PodList isn't updated
# for static pods, see https://github.com/kubernetes/kubernetes/pull/59948
pod = self._get_pod_by_metric_label(sample[self.SAMPLE_LABELS])
if pod is not None and is_static_pending_pod(pod):
tags += get_tags('kubernetes_pod://%s' % pod["metadata"]["uid"], True)
tags += tagger.tag('kubernetes_pod://%s' % pod["metadata"]["uid"], tagger.HIGH)
tags += self._get_kube_container_name(sample[self.SAMPLE_LABELS])
tags = list(set(tags))

Expand All @@ -309,7 +309,7 @@ def _process_pod_rate(self, metric_name, metric, scraper_config):
for pod_uid, sample in iteritems(samples):
if '.network.' in metric_name and self._is_pod_host_networked(pod_uid):
continue
tags = get_tags('kubernetes_pod://%s' % pod_uid, True)
tags = tagger.tag('kubernetes_pod://%s' % pod_uid, tagger.HIGH)
tags += scraper_config['custom_tags']
val = sample[self.SAMPLE_VALUE]
self.rate(metric_name, val, tags)
Expand All @@ -332,14 +332,14 @@ def _process_usage_metric(self, m_name, metric, cache, scraper_config):
if self.pod_list_utils.is_excluded(c_id, pod_uid):
continue

tags = get_tags(c_id, True)
tags = tagger.tag(c_id, tagger.HIGH)
tags += scraper_config['custom_tags']

# FIXME we are forced to do that because the Kubelet PodList isn't updated
# for static pods, see https://github.com/kubernetes/kubernetes/pull/59948
pod = self._get_pod_by_metric_label(sample[self.SAMPLE_LABELS])
if pod is not None and is_static_pending_pod(pod):
tags += get_tags('kubernetes_pod://%s' % pod["metadata"]["uid"], True)
tags += tagger.tag('kubernetes_pod://%s' % pod["metadata"]["uid"], tagger.HIGH)
tags += self._get_kube_container_name(sample[self.SAMPLE_LABELS])
tags = list(set(tags))

Expand All @@ -366,7 +366,7 @@ def _process_limit_metric(self, m_name, metric, cache, scraper_config, pct_m_nam
if self.pod_list_utils.is_excluded(c_id, pod_uid):
continue

tags = get_tags(c_id, True)
tags = tagger.tag(c_id, tagger.HIGH)
tags += scraper_config['custom_tags']

if m_name:
Expand Down
2 changes: 1 addition & 1 deletion kubelet/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_requirements(fpath):
return f.readlines()


CHECKS_BASE_REQ = 'datadog_checks_base'
CHECKS_BASE_REQ = 'datadog_checks_base >= 6.5.0'

setup(
name='datadog-kubelet',
Expand Down
17 changes: 11 additions & 6 deletions kubelet/tests/test_cadvisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def mock_request():
yield m


@pytest.fixture
def tagger():
from datadog_checks.base.stubs import tagger
tagger.reset()
# We filter out slices unknown by the tagger, mock a non-empty taglist
tagger.set_default_tags(["foo:bar"])
return tagger


def test_detect_cadvisor_nominal(mock_request):
mock_request.head('http://kubelet:4192/api/v1.3/subcontainers/', text='{}')
url = KubeletCheck.detect_cadvisor("http://kubelet:10250", 4192)
Expand All @@ -46,7 +55,7 @@ def test_detect_cadvisor_port_zero():
assert url == ""


def test_kubelet_check_cadvisor(monkeypatch, aggregator):
def test_kubelet_check_cadvisor(monkeypatch, aggregator, tagger):
instance_with_tag = {"tags": ["instance:tag"], "cadvisor_port": 4194}
cadvisor_url = "http://valid:port/url"
check = KubeletCheck('kubelet', None, {}, [instance_with_tag])
Expand All @@ -58,11 +67,6 @@ def test_kubelet_check_cadvisor(monkeypatch, aggregator):
mock.Mock(return_value=json.loads(mock_from_file('cadvisor_1.2.json'))))
monkeypatch.setattr(check, 'detect_cadvisor', mock.Mock(return_value=cadvisor_url))
monkeypatch.setattr(check, 'process', mock.Mock(return_value=None))
# We filter out slices unknown by the tagger, mock a non-empty taglist
monkeypatch.setattr('datadog_checks.kubelet.cadvisor.get_tags',
mock.Mock(return_value=["foo:bar"]))
monkeypatch.setattr('datadog_checks.kubelet.cadvisor.tags_for_pod',
mock.Mock(return_value=["foo:bar"]))

check.check(instance_with_tag)
assert check.cadvisor_legacy_url == cadvisor_url
Expand All @@ -79,4 +83,5 @@ def test_kubelet_check_cadvisor(monkeypatch, aggregator):
for metric in EXPECTED_METRICS_CADVISOR:
aggregator.assert_metric(metric)
aggregator.assert_metric_has_tag(metric, "instance:tag")

assert aggregator.metrics_asserted_pct == 100.0
Loading