Skip to content

Commit c424a15

Browse files
committed
Fix tests
1 parent e3d0fa6 commit c424a15

File tree

4 files changed

+56
-82
lines changed

4 files changed

+56
-82
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 & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,6 @@ def compile_filter_rules(rules):
134134

135135
return patterns, tag_names
136136

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

156138
class DockerDaemon(AgentCheck):
157139
"""Collect metrics and events from Docker API and cgroups."""
@@ -219,10 +201,7 @@ def init(self):
219201
self.collect_image_stats = _is_affirmative(instance.get('collect_images_stats', False))
220202
self.collect_container_size = _is_affirmative(instance.get('collect_container_size', False))
221203
self.collect_container_count = _is_affirmative(instance.get('collect_container_count', False))
222-
self.collect_dead_container_count = _is_affirmative(instance.get('collect_dead_container_count', False))
223-
self.collect_exited_container_count = _is_affirmative(instance.get('collect_exited_container_count', False))
224204
self.collect_volume_count = _is_affirmative(instance.get('collect_volume_count', False))
225-
self.collect_dangling_volume_count = _is_affirmative(instance.get('collect_dangling_volume_count', False))
226205
self.collect_events = _is_affirmative(instance.get('collect_events', True))
227206
self.collect_image_size = _is_affirmative(instance.get('collect_image_size', False))
228207
self.collect_disk_stats = _is_affirmative(instance.get('collect_disk_stats', False))
@@ -279,20 +258,11 @@ def check(self, instance):
279258
self._report_container_size(containers_by_id)
280259

281260
if self.collect_container_count:
282-
self._report_container_count_by_state(containers_by_id)
283-
284-
if self.collect_dead_container_count:
285-
self._report_container_count_by_state(containers_by_id, state="Dead")
286-
287-
if self.collect_exited_container_count:
288-
self._report_container_count_by_state(containers_by_id, state="Exited")
261+
self._report_container_count(containers_by_id)
289262

290263
if self.collect_volume_count:
291264
self._report_volume_count()
292265

293-
if self.collect_dangling_volume_count:
294-
self._report_volume_count(filters={'dangling': True})
295-
296266
# Collect disk stats from Docker info command
297267
if self.collect_disk_stats:
298268
self._report_disk_stats()
@@ -369,7 +339,7 @@ def _get_and_count_containers(self, custom_cgroups=False, healthchecks=False):
369339
except Exception as e:
370340
self.log.debug("Unable to inspect Docker container: %s", e)
371341

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

@@ -561,25 +531,32 @@ def _submit_healthcheck_sc(self, container):
561531
tags = self._get_tags(container, CONTAINER)
562532
self.service_check(HEALTHCHECK_SERVICE_CHECK_NAME, status, tags=tags)
563533

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

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

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

584561
def _report_image_size(self, images):
585562
for image in images:
@@ -599,6 +576,7 @@ def _report_performance_metrics(self, containers_by_id):
599576
continue
600577

601578
tags = self._get_tags(container, PERFORMANCE)
579+
602580
self._report_cgroup_metrics(container, tags)
603581
if "_proc_root" not in container:
604582
containers_without_proc_root.append(DockerUtil.container_name_extractor(container)[0])

conf.d/docker_daemon.yaml.example

Lines changed: 8 additions & 23 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
@@ -58,48 +58,33 @@ instances:
5858
# versions will result in an UNKNOWN state for the service check.
5959
#
6060
# You should whitelist the container images you wish to submit health service checks for.
61-
# Example: ["tomcat", "nginx", "etcd"]
61+
# Example: ["image_name:tomcat", "image_name:nginx", "image_name:etcd"]
6262
#
6363
# health_service_checks: false
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: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ def mock_get_info_no_data(self):
7373
def mock_get_info_invalid_values(self):
7474
return {
7575
'DriverStatus': [
76-
['Metadata Space Available', '9 MB'],
77-
['Metadata Space Total', '10 MB'],
78-
['Metadata Space Used', '11 MB'],
76+
['Metadata Space Available', '0 MB'],
77+
['Metadata Space Total', '0 MB'],
78+
['Metadata Space Used', '0 MB'],
7979
]
8080
}
8181

@@ -132,9 +132,9 @@ def test_devicemapper_invalid_values(self, mock_info):
132132

133133
self.run_check(MOCK_CONFIG, force_reload=True)
134134
metric_names = [metric[0] for metric in self.metrics]
135-
self.assertMetric('docker.metadata.free', value=9e6)
136-
self.assertMetric('docker.metadata.used', value=11e6)
137-
self.assertMetric('docker.metadata.total', value=10e6)
135+
self.assertMetric('docker.metadata.free', value=0)
136+
self.assertMetric('docker.metadata.used', value=0)
137+
self.assertMetric('docker.metadata.total', value=0)
138138
self.assertNotIn('docker.metadata.percent', metric_names)
139139

140140
@mock.patch('docker.Client.info')
@@ -292,7 +292,8 @@ def test_exclude_filter(self):
292292
},
293293
],
294294
}
295-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
295+
DockerUtil._drop()
296+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
296297

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

@@ -347,7 +348,8 @@ def test_include_filter(self):
347348
},
348349
],
349350
}
350-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
351+
DockerUtil._drop()
352+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
351353

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

@@ -409,7 +411,8 @@ def test_tags_options(self):
409411
},
410412
],
411413
}
412-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
414+
DockerUtil._drop()
415+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
413416

414417
self.run_check_twice(config, force_reload=True)
415418
for mname, tags in expected_metrics:
@@ -474,7 +477,9 @@ def test_labels_collection(self):
474477
},
475478
],
476479
}
477-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
480+
DockerUtil._drop()
481+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
482+
478483
self.run_check(config, force_reload=True)
479484
for mname, tags in expected_metrics:
480485
self.assertMetric(mname, tags=tags, count=1, at_least=1)
@@ -515,7 +520,8 @@ def test_histogram(self):
515520
},
516521
],
517522
}
518-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
523+
DockerUtil._drop()
524+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
519525

520526
self.run_check(config, force_reload=True)
521527
for mname, tags in expected_metrics:
@@ -547,11 +553,13 @@ def test_healthcheck(self):
547553
"url": "unix://var/run/docker.sock",
548554
"collect_images_stats": True,
549555
"health_service_checks": True,
556+
"health_service_check_whitelist": ["image_name:.*"]
550557
},
551558
],
552559
}
553560

554-
DockerUtil().set_docker_settings(config['init_config'], config['instances'][0])
561+
DockerUtil._drop()
562+
DockerUtil(init_config=config['init_config'], instance=config['instances'][0])
555563

556564
self.run_check(config, force_reload=True)
557565
self.assertServiceCheck('docker.container_health', at_least=2)

0 commit comments

Comments
 (0)