Skip to content

Commit 47d6bea

Browse files
committed
Adding container_name tag and ability to specify docker labels as tags in service discovery
1 parent 1445662 commit 47d6bea

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

packaging/supervisor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ priority=998
4949
user=dd-agent
5050

5151
[program:jmxfetch]
52-
command=/opt/datadog-agent/embedded/bin/python /opt/datadog-agent/agent/jmxfetch.py
52+
command=bash -c 'sleep 5 && /opt/datadog-agent/embedded/bin/python /opt/datadog-agent/agent/jmxfetch.py'
5353
stdout_logfile=NONE
5454
stderr_logfile=NONE
5555
redirect_stderr=true

utils/service_discovery/sd_docker_backend.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from utils.service_discovery.config_stores import get_config_store
1818

1919
DATADOG_ID = 'com.datadoghq.sd.check.id'
20+
DEFAULT_COLLECT_LABELS_AS_TAGS = ""
2021

2122
log = logging.getLogger(__name__)
2223

@@ -73,6 +74,7 @@ class SDDockerBackend(AbstractSDBackend):
7374

7475
def __init__(self, agentConfig):
7576
try:
77+
self.collect_labels_as_tags = agentConfig.get('sd_docker_collect_labels_as_tags', DEFAULT_COLLECT_LABELS_AS_TAGS).split(",")
7678
self.config_store = get_config_store(agentConfig=agentConfig)
7779
except Exception as e:
7880
log.error('Failed to instantiate the config store client. '
@@ -245,6 +247,26 @@ def _extract_port_from_list(self, ports, tpl_var):
245247
def get_tags(self, state, c_id):
246248
"""Extract useful tags from docker or platform APIs. These are collected by default."""
247249
tags = []
250+
251+
container = state.inspect_container(c_id)
252+
253+
# Get some standard tags, like container name
254+
container_name = DockerUtil.container_name_extractor(container)[0]
255+
tags.append("%s:%s" % ("container_name", container_name))
256+
257+
# Collect any specified docker labels as tags
258+
c_labels = container.get('Config', {}).get('Labels', {})
259+
for k in self.collect_labels_as_tags:
260+
if k in c_labels.keys():
261+
v = c_labels[k]
262+
if k == SWARM_SVC_LABEL and Platform.is_swarm():
263+
if v:
264+
tags.append("swarm_service:%s" % v)
265+
elif not v:
266+
tags.append(k)
267+
else:
268+
tags.append("%s:%s" % (k,v))
269+
248270
if Platform.is_k8s():
249271
pod_metadata = state.get_kube_config(c_id, 'metadata')
250272

@@ -284,12 +306,6 @@ def get_tags(self, state, c_id):
284306
# For deployment we only need to do it if the pod creator is a ReplicaSet.
285307
# Details: https://kubernetes.io/docs/user-guide/deployments/#selector
286308

287-
elif Platform.is_swarm():
288-
c_labels = state.inspect_container(c_id).get('Config', {}).get('Labels', {})
289-
swarm_svc = c_labels.get(SWARM_SVC_LABEL)
290-
if swarm_svc:
291-
tags.append('swarm_service:%s' % swarm_svc)
292-
293309
return tags
294310

295311
def _get_additional_tags(self, state, c_id, *args):

0 commit comments

Comments
 (0)