Skip to content

Commit 3856798

Browse files
committed
Adding ability to specify docker labels as tags in service discovery
1 parent 1445662 commit 3856798

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

utils/service_discovery/sd_docker_backend.py

Lines changed: 16 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,20 @@ 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+
# Collect any specified docker labels as tags
252+
c_labels = state.inspect_container(c_id).get('Config', {}).get('Labels', {})
253+
for k in self.collect_labels_as_tags:
254+
if k in c_labels.keys():
255+
v = c_labels[k]
256+
if k == SWARM_SVC_LABEL and Platform.is_swarm():
257+
if v:
258+
tags.append("swarm_service:%s" % v)
259+
elif not v:
260+
tags.append(k)
261+
else:
262+
tags.append("%s:%s" % (k,v))
263+
248264
if Platform.is_k8s():
249265
pod_metadata = state.get_kube_config(c_id, 'metadata')
250266

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

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-
293303
return tags
294304

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

0 commit comments

Comments
 (0)