|
17 | 17 | from utils.service_discovery.config_stores import get_config_store |
18 | 18 |
|
19 | 19 | DATADOG_ID = 'com.datadoghq.sd.check.id' |
| 20 | +DEFAULT_COLLECT_LABELS_AS_TAGS = "" |
20 | 21 |
|
21 | 22 | log = logging.getLogger(__name__) |
22 | 23 |
|
@@ -73,6 +74,7 @@ class SDDockerBackend(AbstractSDBackend): |
73 | 74 |
|
74 | 75 | def __init__(self, agentConfig): |
75 | 76 | try: |
| 77 | + self.collect_labels_as_tags = agentConfig.get('sd_docker_collect_labels_as_tags', DEFAULT_COLLECT_LABELS_AS_TAGS).split(",") |
76 | 78 | self.config_store = get_config_store(agentConfig=agentConfig) |
77 | 79 | except Exception as e: |
78 | 80 | log.error('Failed to instantiate the config store client. ' |
@@ -245,6 +247,20 @@ def _extract_port_from_list(self, ports, tpl_var): |
245 | 247 | def get_tags(self, state, c_id): |
246 | 248 | """Extract useful tags from docker or platform APIs. These are collected by default.""" |
247 | 249 | 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 | + |
248 | 264 | if Platform.is_k8s(): |
249 | 265 | pod_metadata = state.get_kube_config(c_id, 'metadata') |
250 | 266 |
|
@@ -284,12 +300,6 @@ def get_tags(self, state, c_id): |
284 | 300 | # For deployment we only need to do it if the pod creator is a ReplicaSet. |
285 | 301 | # Details: https://kubernetes.io/docs/user-guide/deployments/#selector |
286 | 302 |
|
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 | | - |
293 | 303 | return tags |
294 | 304 |
|
295 | 305 | def _get_additional_tags(self, state, c_id, *args): |
|
0 commit comments