Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[service_discovery] Adding container_name tag, ability to specify docker labels as tags #3282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/core/test_service_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ class TestServiceDiscovery(unittest.TestCase):
# image_name: ([(source, (check_name, init_tpl, instance_tpl, variables))], (expected_config_template))
'image_0': (
[('template', ('check_0', {}, {'host': '%%host%%'}, ['host']))],
('template', ('check_0', {}, {'host': '127.0.0.1'}))),
('template', ('check_0', {}, {'host': '127.0.0.1', 'tags': ['container_name:nginx']}))),
'image_1': (
[('template', ('check_1', {}, {'port': '%%port%%'}, ['port']))],
('template', ('check_1', {}, {'port': '1337'}))),
('template', ('check_1', {}, {'port': '1337', 'tags': ['container_name:nginx']}))),
'image_2': (
[('template', ('check_2', {}, {'host': '%%host%%', 'port': '%%port%%'}, ['host', 'port']))],
('template', ('check_2', {}, {'host': '127.0.0.1', 'port': '1337'}))),
('template', ('check_2', {}, {'host': '127.0.0.1', 'port': '1337', 'tags': ['container_name:nginx']}))),
}

# raw templates coming straight from the config store
Expand Down
31 changes: 25 additions & 6 deletions utils/service_discovery/sd_docker_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from utils.service_discovery.config_stores import get_config_store

DATADOG_ID = 'com.datadoghq.sd.check.id'
DEFAULT_COLLECT_LABELS_AS_TAGS = ""

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,6 +74,7 @@ class SDDockerBackend(AbstractSDBackend):

def __init__(self, agentConfig):
try:
self.collect_labels_as_tags = agentConfig.get('sd_docker_collect_labels_as_tags', DEFAULT_COLLECT_LABELS_AS_TAGS).split(",")
self.config_store = get_config_store(agentConfig=agentConfig)
except Exception as e:
log.error('Failed to instantiate the config store client. '
Expand Down Expand Up @@ -245,6 +247,29 @@ def _extract_port_from_list(self, ports, tpl_var):
def get_tags(self, state, c_id):
"""Extract useful tags from docker or platform APIs. These are collected by default."""
tags = []

container = state.inspect_container(c_id)

# Get some standard tags, like container name
container_name = container.get("Name", container.get("Id"))
if container_name.count('/') <= 1:
container_name = str(container_name).lstrip('/')

tags.append("%s:%s" % ("container_name", container_name))

# Collect any specified docker labels as tags
c_labels = container.get('Config', {}).get('Labels', {})
for k in self.collect_labels_as_tags:
if k in c_labels.keys():
v = c_labels[k]
if k == SWARM_SVC_LABEL and Platform.is_swarm():
if v:
tags.append("swarm_service:%s" % v)
elif not v:
tags.append(k)
else:
tags.append("%s:%s" % (k,v))

if Platform.is_k8s():
pod_metadata = state.get_kube_config(c_id, 'metadata')

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

elif Platform.is_swarm():
c_labels = state.inspect_container(c_id).get('Config', {}).get('Labels', {})
swarm_svc = c_labels.get(SWARM_SVC_LABEL)
if swarm_svc:
tags.append('swarm_service:%s' % swarm_svc)

return tags

def _get_additional_tags(self, state, c_id, *args):
Expand Down