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

[swarm] tag container metrics with swarm service if available #3182

Merged
merged 1 commit into from
Feb 10, 2017
Merged
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
10 changes: 9 additions & 1 deletion checks.d/docker_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
'image_tag'
]

DEFAULT_LABELS_AS_TAGS = [
DockerUtil.SWARM_SVC_LABEL
]


TAG_EXTRACTORS = {
"docker_image": lambda c: [c["Image"]],
Expand Down Expand Up @@ -187,7 +191,7 @@ def init(self):

# Set tagging options
self.custom_tags = instance.get("tags", [])
self.collect_labels_as_tags = instance.get("collect_labels_as_tags", [])
self.collect_labels_as_tags = instance.get("collect_labels_as_tags", DEFAULT_LABELS_AS_TAGS)
self.kube_labels = {}

self.use_histogram = _is_affirmative(instance.get('use_histogram', False))
Expand Down Expand Up @@ -399,6 +403,10 @@ def _get_tags(self, entity=None, tag_type=None):
tags.append("kube_replication_controller:%s" % replication_controller)
tags.append("pod_name:%s" % pod_name)

elif k == DockerUtil.SWARM_SVC_LABEL and Platform.is_swarm():
if v:
tags.append("swarm_service:%s" % v)

elif not v:
tags.append(k)

Expand Down
20 changes: 20 additions & 0 deletions utils/dockerutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from utils.platform import Platform
from utils.singleton import Singleton

SWARM_SVC_LABEL = 'com.docker.swarm.service.name'
DATADOG_ID = 'com.datadoghq.sd.check.id'


Expand Down Expand Up @@ -60,6 +61,9 @@ def __init__(self, **kwargs):
# At first run we'll just collect the events from the latest 60 secs
self._latest_event_collection_ts = int(time.time()) - 60

# Try to detect if we are on Swarm
self.fetch_swarm_state()

# Try to detect if we are on ECS
self._is_ecs = False
try:
Expand Down Expand Up @@ -120,6 +124,22 @@ def get_check_config(self):
def is_ecs(self):
return self._is_ecs

def is_swarm(self):
if self.swarm_node_state == 'pending':
self.fetch_swarm_state()
if self.swarm_node_state == 'active':
return True
else:
return False

def fetch_swarm_state(self):
self.swarm_node_state = None
try:
info = DockerUtil().client.info()
self.swarm_node_state = info.get('Swarm', {}).get('LocalNodeState')
except Exception:
pass

def get_events(self):
self.events = []
changed_container_ids = set()
Expand Down
5 changes: 5 additions & 0 deletions utils/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ def is_containerized():
@staticmethod
def is_k8s():
return 'KUBERNETES_PORT' in os.environ

@staticmethod
def is_swarm():
from utils.dockerutil import DockerUtil
return DockerUtil().is_swarm()
8 changes: 8 additions & 0 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'

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -260,6 +261,13 @@ def get_tags(self, state, c_id):
# get kubernetes namespace
tags.append('kube_namespace:%s' % pod_metadata.get('namespace'))

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


return tags

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