-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add Docker container environment variables as tags. Only whitelisted #2580 #2581
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a few minor changes needed.
plugins/inputs/docker/docker.go
Outdated
@@ -98,6 +111,8 @@ var sampleConfig = ` | |||
perdevice = true | |||
## Whether to report for each container total blkio and network stats or not | |||
total = false | |||
## Which environment variables should we use as a tag | |||
gather_environment = ["ENV", "DC"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment this line so its not on by default, change the names to something a little more standard so its extra clear these are environment variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/inputs/docker/docker.go
Outdated
if len(d.GatherEnvironment) > 0 { | ||
info, err := inspectWrapper(d.client, ctx, container.ID) | ||
if err != nil { | ||
return fmt.Errorf("Error getting docker container inspect: %s", err.Error()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wording here is confusing, I suggest "Error inspecting docker container: %s"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/inputs/docker/docker.go
Outdated
|
||
for _, envvar := range info.Config.Env { | ||
kvs := strings.SplitN(envvar, "=", 2) | ||
if !sliceContains(kvs[0], d.GatherEnvironment) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use this function since it is private to the libraries docker package, its just a coincidence that we are also in a docker package. I think this loop would be better if we ranged over d.GatherEnvironment and split only if the prefix matches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/inputs/docker/docker.go
Outdated
if !sliceContains(kvs[0], d.GatherEnvironment) { | ||
continue | ||
} | ||
tags[kvs[0]] = kvs[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets be extra careful and check the len of kvs, since SplitN could return a slice of length 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/inputs/docker/docker_test.go
Outdated
testing: true, | ||
client: nil, | ||
testing: true, | ||
GatherEnvironment: []string{"ENVVAR1", "ENVVAR2"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for envvars that are not found in the container, envvars with zero length values, multiple '=' signs, and any other edge cases you can think of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added mentioned negative edge cases.
Please update the docker README.md too. |
Review changes are incorporated. Please have a look |
There were some conflicts due to Docker: optionally add labels as tags (#2425) , |
plugins/inputs/docker/README.md
Outdated
@@ -30,12 +30,13 @@ for the stat structure can be found | |||
perdevice = true | |||
## Whether to report for each container total blkio and network stats or not | |||
total = false | |||
## Which environment variables should we use as a tag | |||
gather_environment = ["JAVA_HOME", "HEAP_SIZE"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should rename this option to tag_env
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/inputs/docker/docker.go
Outdated
} | ||
for _, envvar := range info.Config.Env { | ||
for _, prefix := range d.GatherEnvironment { | ||
if strings.Count(envvar, "=") > 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should't skip environment variables like: FOO=BAR=BAZ
, also add a testcase for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, added the testcase
plugins/inputs/docker/docker.go
Outdated
if strings.Count(envvar, "=") > 1 { | ||
continue | ||
} | ||
if strings.HasPrefix(envvar, prefix) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think items with a common prefix will get picked up. Try adding a test case for if there are environment variables FOO
and FOOBAR
, and specify FOO
in the config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added an testcase for this as well
I have incorporated review changes. Testing failed due to Test killed with quit: ran too long |
plugins/inputs/docker/docker_test.go
Outdated
testing: true, | ||
client: nil, | ||
testing: true, | ||
TagEnvironment: []string{"ENVVAR1", "ENVVAR2", "ENVVAR3", "ENVVAR4", "ENVVAR5", "ENVVAR6", "ENVVAR7", "ENVVAR8", "ENVVAR9"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please line wrap this after each element, not a hard rule but try to stick to about 80 chars per line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/inputs/docker/docker_test.go
Outdated
"ENVVAR2": "dolorsitamet", | ||
"ENVVAR3": "ubuntu:10.04", | ||
"ENVVAR6": " ", | ||
"ENVVAR7": "ENVVAR9", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Value should be ENVVAR8=ENVVAR9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, didn't get this comment
say ENVVAR7=ENVVAR8=ENVVAR9
then tags would be
ENVVAR7=ENVVAR9
ENVVAR8=ENVVAR9
please correct me if my understanding is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tagkey should be ENVVAR7
and the tagvalue should be ENVVAR8=ENVVAR9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/inputs/docker/docker_test.go
Outdated
"ENVVAR3": "ubuntu:10.04", | ||
"ENVVAR6": " ", | ||
"ENVVAR7": "ENVVAR9", | ||
"ENVVAR8": "ENVVAR9", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There shouldn't be an ENVVAR8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, didn't get this comment
say ENVVAR7=ENVVAR8=ENVVAR9
then tags would be
ENVVAR7=ENVVAR9
ENVVAR8=ENVVAR9
please correct me if my understanding is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@rsingh2411 Can you add an entry to the 1.4 feature section of the CHANGELOG? @nhaugo Did you want to review? |
Add Docker container environment variables as tags. Only whitelisted #2580
PR for the above issue.
Required for all PRs: