Skip to content

Commit 2b3edf2

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

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-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:nginx']}))),
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:nginx']}))),
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:nginx']}))),
125125
}
126126

127127
# raw templates coming straight from the config store

utils/service_discovery/sd_docker_backend.py

Lines changed: 25 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,29 @@ 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 = container.get("Name", container.get("Id"))
255+
if container_name.count('/') <= 1:
256+
container_name = str(container_name).lstrip('/')
257+
258+
tags.append("%s:%s" % ("container_name", container_name))
259+
260+
# Collect any specified docker labels as tags
261+
c_labels = container.get('Config', {}).get('Labels', {})
262+
for k in self.collect_labels_as_tags:
263+
if k in c_labels.keys():
264+
v = c_labels[k]
265+
if k == SWARM_SVC_LABEL and Platform.is_swarm():
266+
if v:
267+
tags.append("swarm_service:%s" % v)
268+
elif not v:
269+
tags.append(k)
270+
else:
271+
tags.append("%s:%s" % (k,v))
272+
248273
if Platform.is_k8s():
249274
pod_metadata = state.get_kube_config(c_id, 'metadata')
250275

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

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-
293312
return tags
294313

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

0 commit comments

Comments
 (0)