Skip to content

Commit f51af49

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

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

tests/core/test_service_discovery.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ class TestServiceDiscovery(unittest.TestCase):
115115
# image_name: ([(source, (check_name, init_tpl, instance_tpl, variables))], (expected_config_template))
116116
'image_0': (
117117
[('template', ('check_0', {}, {'host': '%%host%%'}, ['host']))],
118-
('template', ('check_0', {}, {'host': '127.0.0.1'}))),
118+
('template', ('check_0', {}, {'host': '127.0.0.1', 'tags': ['container_name:69ff25598b23']}))),
119119
'image_1': (
120120
[('template', ('check_1', {}, {'port': '%%port%%'}, ['port']))],
121-
('template', ('check_1', {}, {'port': '1337'}))),
121+
('template', ('check_1', {}, {'port': '1337', 'tags': ['container_name:69ff25598b23']}))),
122122
'image_2': (
123123
[('template', ('check_2', {}, {'host': '%%host%%', 'port': '%%port%%'}, ['host', 'port']))],
124-
('template', ('check_2', {}, {'host': '127.0.0.1', 'port': '1337'}))),
124+
('template', ('check_2', {}, {'host': '127.0.0.1', 'port': '1337', 'tags': ['container_name:69ff25598b23']}))),
125125
}
126126

127127
# raw templates coming straight from the config store

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)