From 47d6beac8c03dba653cd339b0dfc571f7e47f13d Mon Sep 17 00:00:00 2001 From: Scott Rankin Date: Fri, 24 Mar 2017 11:48:47 -0400 Subject: [PATCH] Adding container_name tag and ability to specify docker labels as tags in service discovery --- packaging/supervisor.conf | 2 +- utils/service_discovery/sd_docker_backend.py | 28 +++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packaging/supervisor.conf b/packaging/supervisor.conf index e57491c92c..c1bed6af5d 100644 --- a/packaging/supervisor.conf +++ b/packaging/supervisor.conf @@ -49,7 +49,7 @@ priority=998 user=dd-agent [program:jmxfetch] -command=/opt/datadog-agent/embedded/bin/python /opt/datadog-agent/agent/jmxfetch.py +command=bash -c 'sleep 5 && /opt/datadog-agent/embedded/bin/python /opt/datadog-agent/agent/jmxfetch.py' stdout_logfile=NONE stderr_logfile=NONE redirect_stderr=true diff --git a/utils/service_discovery/sd_docker_backend.py b/utils/service_discovery/sd_docker_backend.py index e54816ce0d..87db4c3109 100644 --- a/utils/service_discovery/sd_docker_backend.py +++ b/utils/service_discovery/sd_docker_backend.py @@ -17,6 +17,7 @@ from utils.service_discovery.config_stores import get_config_store DATADOG_ID = 'com.datadoghq.sd.check.id' +DEFAULT_COLLECT_LABELS_AS_TAGS = "" log = logging.getLogger(__name__) @@ -73,6 +74,7 @@ class SDDockerBackend(AbstractSDBackend): def __init__(self, agentConfig): try: + self.collect_labels_as_tags = agentConfig.get('sd_docker_collect_labels_as_tags', DEFAULT_COLLECT_LABELS_AS_TAGS).split(",") self.config_store = get_config_store(agentConfig=agentConfig) except Exception as e: log.error('Failed to instantiate the config store client. ' @@ -245,6 +247,26 @@ def _extract_port_from_list(self, ports, tpl_var): def get_tags(self, state, c_id): """Extract useful tags from docker or platform APIs. These are collected by default.""" tags = [] + + container = state.inspect_container(c_id) + + # Get some standard tags, like container name + container_name = DockerUtil.container_name_extractor(container)[0] + tags.append("%s:%s" % ("container_name", container_name)) + + # Collect any specified docker labels as tags + c_labels = container.get('Config', {}).get('Labels', {}) + for k in self.collect_labels_as_tags: + if k in c_labels.keys(): + v = c_labels[k] + if k == SWARM_SVC_LABEL and Platform.is_swarm(): + if v: + tags.append("swarm_service:%s" % v) + elif not v: + tags.append(k) + else: + tags.append("%s:%s" % (k,v)) + if Platform.is_k8s(): pod_metadata = state.get_kube_config(c_id, 'metadata') @@ -284,12 +306,6 @@ def get_tags(self, state, c_id): # For deployment we only need to do it if the pod creator is a ReplicaSet. # Details: https://kubernetes.io/docs/user-guide/deployments/#selector - elif Platform.is_swarm(): - c_labels = state.inspect_container(c_id).get('Config', {}).get('Labels', {}) - swarm_svc = c_labels.get(SWARM_SVC_LABEL) - if swarm_svc: - tags.append('swarm_service:%s' % swarm_svc) - return tags def _get_additional_tags(self, state, c_id, *args):