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

Use consistent container name in docker input plugin #8703

Merged
merged 2 commits into from
Mar 2, 2021

Conversation

pkoenig10
Copy link
Contributor

@pkoenig10 pkoenig10 commented Jan 15, 2021

Required for all PRs:

  • Associated README.md updated.
  • Has appropriate unit tests.

Before this PR

I have noticed that the docker input plugin occasionally gathers metrics for containers with a container name that does not match the configured filter. For example, I have a container named influxdb and have configured the docker input plugin with container_name_include = ["influxdb"], but the docker input plugin sometimes produces metrics with a container_name tag of something like 689a5b5f3679_influxdb.

Upon further investigation, I discovered that this happens when the metrics are gathered while docker-compose is recreating the container. When docker-compose recreates the container, it temporarily renames the original container to {id}_{name} before removing it.

The docker input plugin uses the name from the ListContainer call when checking the filter, but uses the name from the ContainerStats call when setting the container_name tag. If the container is renamed between these calls, it will result in the confusing behavior described above.

After this PR

The docker input plugin uses the name from the ListContainer call both when checking the filter and when setting the container_name tag.

Because we want the container_name tag to be the canonical container name, this PR removes support for matching based on all container names and now only matches on the default container name. Other container names come from links (see moby/moby#14128), which is a legacy feature that has been deprecated for a while. See https://docs.docker.com/network/links/.

This implementation now matches the behavior of docker ps (when used without --no-trunc). You can see the current implementation here:
https://github.com/docker/cli/blob/cde469bf1aad14146d57710205399899e465aadc/cli/command/formatter/container.go#L124-L138

This behavior in the docker CLI was first introduced over 5 years ago in moby/moby#8505.

I can't think of a good reason to allow matching on names from links, especially given that they are deprecated, so it seemed reasonable to make this change.

Copy link
Contributor

@telegraf-tiger telegraf-tiger bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤝 ✅ CLA has been signed. Thank you!

@pkoenig10
Copy link
Contributor Author

@danielnelson I'd really appreciate a review of this if you have the time!

@sjwang90 sjwang90 added area/docker feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin labels Feb 3, 2021
@pkoenig10
Copy link
Contributor Author

pkoenig10 commented Feb 7, 2021

@ssoroka @sjwang90 I'd really appreciate a review of this if you have the time!

@pkoenig10
Copy link
Contributor Author

@ssoroka @sjwang90 Another ping for a review if you have the time! This should be a relatively simple fix.

Copy link
Contributor

@ivorybilled ivorybilled left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@pkoenig10
Copy link
Contributor Author

@jagularr Any other blockers to getting this merged?

Copy link
Contributor

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks also good to me.

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Mar 1, 2021
plugins/inputs/docker/docker.go Outdated Show resolved Hide resolved
@@ -434,7 +434,7 @@ func (d *Docker) gatherContainer(
var cname string
for _, name := range container.Names {
trimmedName := strings.TrimPrefix(name, "/")
if len(strings.Split(trimmedName, "/")) == 1 {
if !strings.Contains(trimmedName, "/") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about /name/ should that match? Maybe it would be better to trim both leading and trailing slashed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you can get a name like that.

I'd prefer to keep this implementation as similar as possible to the Docker CLI implementation. I imagine that users expect the implementation of the name filter to be consistent with the output of docker ps.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok than that's fine with me.

@ssoroka ssoroka merged commit 6bc731b into influxdata:master Mar 2, 2021
@pkoenig10 pkoenig10 deleted the containerName branch April 20, 2021 06:29
arstercz pushed a commit to arstercz/telegraf that referenced this pull request Mar 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docker feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants