Skip to content

Commit 6923524

Browse files
committed
Fix tests, refactor how we collect container and volume states
1 parent fea04b5 commit 6923524

File tree

4 files changed

+48
-76
lines changed

4 files changed

+48
-76
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ env:
3535
- TRAVIS_FLAVOR=cassandra FLAVOR_VERSION=2.0.13
3636
- TRAVIS_FLAVOR=cassandra FLAVOR_VERSION=2.1.3
3737
- TRAVIS_FLAVOR=couchdb
38+
# FIXME: cannot enable docker on Travis
39+
# because it needs docker and we run tests in a container
40+
# - TRAVIS_FLAVOR=docker_daemon
3841
- TRAVIS_FLAVOR=elasticsearch FLAVOR_VERSION=0.90.13
3942
- TRAVIS_FLAVOR=elasticsearch FLAVOR_VERSION=1.0.3
4043
- TRAVIS_FLAVOR=elasticsearch FLAVOR_VERSION=1.1.2

checks.d/docker_daemon.py

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,6 @@ def compile_filter_rules(rules):
133133

134134
return patterns, tag_names
135135

136-
def get_filters(include, exclude):
137-
# The reasoning is to check exclude first, so we can skip if there is no exclude
138-
if not exclude:
139-
return
140-
141-
filtered_tag_names = []
142-
exclude_patterns = []
143-
include_patterns = []
144-
145-
# Compile regex
146-
exclude_patterns, tag_names = compile_filter_rules(exclude)
147-
filtered_tag_names.extend(tag_names)
148-
149-
include_patterns, tag_names = compile_filter_rules(include)
150-
filtered_tag_names.extend(tag_names)
151-
152-
return set(exclude_patterns), set(include_patterns), set(filtered_tag_names)
153-
154136

155137
class DockerDaemon(AgentCheck):
156138
"""Collect metrics and events from Docker API and cgroups."""
@@ -217,10 +199,7 @@ def init(self):
217199
self.collect_image_stats = _is_affirmative(instance.get('collect_images_stats', False))
218200
self.collect_container_size = _is_affirmative(instance.get('collect_container_size', False))
219201
self.collect_container_count = _is_affirmative(instance.get('collect_container_count', False))
220-
self.collect_dead_container_count = _is_affirmative(instance.get('collect_dead_container_count', False))
221-
self.collect_exited_container_count = _is_affirmative(instance.get('collect_exited_container_count', False))
222202
self.collect_volume_count = _is_affirmative(instance.get('collect_volume_count', False))
223-
self.collect_dangling_volume_count = _is_affirmative(instance.get('collect_dangling_volume_count', False))
224203
self.collect_events = _is_affirmative(instance.get('collect_events', True))
225204
self.collect_image_size = _is_affirmative(instance.get('collect_image_size', False))
226205
self.collect_disk_stats = _is_affirmative(instance.get('collect_disk_stats', False))
@@ -277,20 +256,11 @@ def check(self, instance):
277256
self._report_container_size(containers_by_id)
278257

279258
if self.collect_container_count:
280-
self._report_container_count_by_state(containers_by_id)
281-
282-
if self.collect_dead_container_count:
283-
self._report_container_count_by_state(containers_by_id, state="Dead")
284-
285-
if self.collect_exited_container_count:
286-
self._report_container_count_by_state(containers_by_id, state="Exited")
259+
self._report_container_count(containers_by_id)
287260

288261
if self.collect_volume_count:
289262
self._report_volume_count()
290263

291-
if self.collect_dangling_volume_count:
292-
self._report_volume_count(filters={'dangling': True})
293-
294264
# Collect disk stats from Docker info command
295265
if self.collect_disk_stats:
296266
self._report_disk_stats()
@@ -367,7 +337,7 @@ def _get_and_count_containers(self, custom_cgroups=False, healthchecks=False):
367337
except Exception as e:
368338
self.log.debug("Unable to inspect Docker container: %s", e)
369339

370-
340+
# TODO: deprecate these 2, they should be replaced by _report_container_count
371341
for tags, count in running_containers_count.iteritems():
372342
self.gauge("docker.containers.running", count, tags=list(tags))
373343

@@ -559,26 +529,32 @@ def _submit_healthcheck_sc(self, container):
559529
tags = self._get_tags(container, CONTAINER)
560530
self.service_check(HEALTHCHECK_SERVICE_CHECK_NAME, status, tags=tags)
561531

562-
def _report_container_count_by_state(self, containers_by_id, state="Any"):
563-
count = 0
564-
tags = {}
565-
filterlambda = lambda x: not self._is_container_excluded(x) and state is "Any" or container["State"] is state
566-
filtered = list(filter(filterlambda, containers_by_id))
567-
tags = self._get_tags(filtered[0], PERFORMANCE)
568-
532+
def _report_container_count(self, containers_by_id):
533+
"""Report container count per state"""
569534
m_func = FUNC_MAP[GAUGE][self.use_histogram]
570-
# Report docker.container.count if state is "Any", otherwise
571-
# report docker.container.state_STATE.count
572-
suffix = ".state_{}".format(state.lower()) if state is not "Any" else ""
573-
m_func(self, 'docker.container{}.count'.format(suffix), len(filtered), tags=tags)
574535

575-
def _report_volume_count(self, filters={}):
576-
volumes = self.docker_client.volumes(filters=filters)
577-
count = len(volumes['Volumes'])
536+
per_state_count = defaultdict(int)
537+
538+
filterlambda = lambda ctr: not self._is_container_excluded(ctr)
539+
containers = list(filter(filterlambda, containers_by_id.values()))
540+
541+
for ctr in containers:
542+
per_state_count[ctr.get('State', '')] += 1
578543

544+
for state in per_state_count:
545+
if state:
546+
m_func(self, 'docker.container.count', per_state_count[state], tags=['container_state:%s' % state.lower()])
547+
548+
def _report_volume_count(self):
549+
"""Report volume count per state (dangling or not)"""
579550
m_func = FUNC_MAP[GAUGE][self.use_histogram]
580-
suffix = '.' + '-'.join(sorted(filters.keys())) if len(filters) is not 0 else ''
581-
m_func(self, 'docker.volumes{}.count'.format(suffix), count)
551+
552+
attached_volumes = self.docker_client.volumes(filters={'dangling': False})
553+
dangling_volumes = self.docker_client.volumes(filters={'dangling': True})
554+
attached_count = len(attached_volumes['Volumes'])
555+
dangling_count = len(dangling_volumes['Volumes'])
556+
m_func(self, 'docker.volume.count', attached_count, tags=['volume_state:attached'])
557+
m_func(self, 'docker.volume.count', dangling_count, tags=['volume_state:dangling'])
582558

583559
def _report_image_size(self, images):
584560
for image in images:
@@ -598,6 +574,7 @@ def _report_performance_metrics(self, containers_by_id):
598574
continue
599575

600576
tags = self._get_tags(container, PERFORMANCE)
577+
601578
self._report_cgroup_metrics(container, tags)
602579
if "_proc_root" not in container:
603580
containers_without_proc_root.append(DockerUtil.container_name_extractor(container)[0])

conf.d/docker_daemon.yaml.example

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ instances:
4343
# ensure that `docker ps -a -q` run fast before enabling it.
4444
# Defaults to false.
4545
#
46-
# collect_container_size: false
46+
# collect_container_size: true
4747

4848
# Do you use custom cgroups for this particular instance?
4949
# Note: enabling this option modifies the way in which we inspect the containers and causes
@@ -63,43 +63,28 @@ instances:
6363
#
6464
# health_service_check_whitelist: []
6565

66-
# Collect the total container count with the docker.containers.count metric.
66+
# Collect the container count tagged by state (running, paused, exited, dead)
6767
# Defaults to false.
6868
#
69-
# collect_container_count: false
69+
# collect_container_count: true
7070

71-
# Collect the count of all containers in Dead state with the docker.container.state_dead.count metric.
71+
# Collect the volume count for attached and dangling volumes.
7272
# Defaults to false.
7373
#
74-
# collect_dead_container_count: false
75-
76-
# Collect the count of all containers in Exited state with the docker.container.state_exited.count metric.
77-
# Defaults to false.
78-
#
79-
# collect_exited_container_count: false
80-
81-
# Collect the total volume count with the docker.volumes.count metric.
82-
# Defaults to false.
83-
#
84-
# collect_volume_count: false
85-
86-
# Collect the count of all dangling volumes with the docker.volumes.dangling.count metric.
87-
# Defaults to false.
88-
#
89-
# collect_dangling_volume_count: false
74+
# collect_volume_count: true
9075

9176
# Collect images stats
9277
# Number of available active images and intermediate images as gauges.
9378
# Defaults to false.
9479
#
95-
# collect_images_stats: false
80+
# collect_images_stats: true
9681

9782
# Collect disk usage per image with docker.image.size and docker.image.virtual_size metrics.
9883
# The check gets this size with the `docker images` command.
9984
# Requires collect_images_stats to be enabled.
10085
# Defaults to false.
10186
#
102-
# collect_image_size: false
87+
# collect_image_size: true
10388

10489
# Collect disk metrics (total, used, free) through the docker info command for data and metadata.
10590
# This is useful when these values can't be obtained by the disk check.

tests/checks/integration/test_docker_daemon.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ def test_exclude_filter(self):
291291
},
292292
],
293293
}
294-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
294+
DockerUtil._drop()
295+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
295296

296297
self.run_check_twice(config, force_reload=True)
297298

@@ -346,7 +347,8 @@ def test_include_filter(self):
346347
},
347348
],
348349
}
349-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
350+
DockerUtil._drop()
351+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
350352

351353
self.run_check_twice(config, force_reload=True)
352354

@@ -408,7 +410,8 @@ def test_tags_options(self):
408410
},
409411
],
410412
}
411-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
413+
DockerUtil._drop()
414+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
412415

413416
self.run_check_twice(config, force_reload=True)
414417
for mname, tags in expected_metrics:
@@ -473,7 +476,9 @@ def test_labels_collection(self):
473476
},
474477
],
475478
}
476-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
479+
DockerUtil._drop()
480+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
481+
477482
self.run_check(config, force_reload=True)
478483
for mname, tags in expected_metrics:
479484
self.assertMetric(mname, tags=tags, count=1, at_least=1)
@@ -514,7 +519,8 @@ def test_histogram(self):
514519
},
515520
],
516521
}
517-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
522+
DockerUtil._drop()
523+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
518524

519525
self.run_check(config, force_reload=True)
520526
for mname, tags in expected_metrics:
@@ -563,7 +569,8 @@ def test_healthcheck(self):
563569
],
564570
}
565571

566-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
572+
DockerUtil._drop()
573+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
567574

568575
self.run_check(config, force_reload=True)
569576
self.assertServiceCheck('docker.container_health', count=0)

0 commit comments

Comments
 (0)