[docker_daemon] Add 5 opt-in checks for container & volume counts#2740
[docker_daemon] Add 5 opt-in checks for container & volume counts#2740parkr wants to merge 3 commits intoDataDog:masterfrom parkr:docker-daemon-track-containers-volumes
Conversation
|
/cc @ross |
|
Thanks @parkr! We'll review your changes in details soon. No problem at all about the monolithic commit, it looks clean enough as it is 😃 We might want to add some tests on this in test_docker_daemon.py, we'll keep you updated |
|
@olivielpeau Thanks! It's been a while since I have done any Python work so pardon the lack of idiomatic Python. I tried to fix it up by using |
| m_func = FUNC_MAP[GAUGE][self.use_histogram] | ||
| # Report docker.container.count if state is "Any", otherwise | ||
| # report docker.container.state_STATE.count | ||
| suffix = ".state_{}".format(state.lower()) if state is not "Any" else "" |
There was a problem hiding this comment.
any reason not to use a tag instead for the state ?
There was a problem hiding this comment.
Hey @remh! No particular reason! I am used to a much more basic version of graphite and tags are new. How would you like to proceed here?
There was a problem hiding this comment.
Hi @parkr
The way we usually do this is to report docker.containers.count several times, each time with a different state tag. This could look like:
# const
STATES = ['created', 'restarting', 'running', 'paused', 'exited', 'dead']
...
def _report_container_count_by_state(self, containers_by_id):
tags = self._get_tags(filtered[0], PERFORMANCE)
m_func = FUNC_MAP[GAUGE][self.use_histogram]
for state in STATES:
# the filter changes for each state
filterlambda = lambda x: not self._is_container_excluded(x) and container["State"] is state
filtered = list(filter(filterlambda, containers_by_id))
# add the state tag
tags.append('state:{}'.format(state)
m_func(self, 'docker.container.count'.format(suffix), len(filtered), tags=tags)This way you can sum the reported values of docker.container.count on a graph to get the total count, or split the graph over the state tag and have the detailed count.
Let me know if it's still unclear.
(this snippet is not tested and could be optimized)
|
Thanks a lot @parkr ! That's pretty cool! Could you also add tests for the metrics you are collecting please ? |
I just went looking through other PR's to see how this is best accomplished and didn't find recent examples of PR's which add functionality to this check and add tests. I'll take a stab but it might be terrible. |
- collect_exited_container_count: gauge of how many containers have State=Exited - collect_dead_container_count: gauge of how many containers have State=Dead - collect_container_count: gauge of how many containers there are - collect_volume_count: gauge of how many volumes there are - collect_dangling_volume_count: gauge of how many dangling volumes there are
| tags=tags) | ||
|
|
||
| def _report_container_count_by_state(self, containers_by_id, state="Any"): | ||
| count = 0 |
There was a problem hiding this comment.
looks like it's not used, you can remove it safely
|
Hi @parkr |
I got stuck trying to write tests for this. Couldn't seem to mock the response from the docker API. |
…iners-volumes [docker] container & volume metrics (wraps #2740)
|
#3077, which supersedes this PR, was merged. |
What does this PR do?
This pull request adds 5 new metrics:
docker.containers.count– equivalent todocker ps -a | wc -ldocker.containers.state_dead.count– count of all dead containersdocker.containers.state_exited.count– count of all exited containersdocker.volumes.count– count of all volumesdocker.volumes.dangling.count– count of all dangling volumesEach metric has its own toggle in the settings so you can opt in to each on its own.
Motivation
We've been battling a bit of weirdness with containers and volumes lying around. We use ephemeral docker containers for some operations but found that volumes would be lying around in the "dangling" state, and that some containers weren't properly cleaned up.
Testing Guidelines
An overview on testing
is available in our contribution guidelines.
Additional Notes
I went with one monolithic commit here because the code was too close together for Git to play nicely with cherry-picking the commits. Let me know if that wasn't the right way to go and I can make 5 distinct commits.