-
Notifications
You must be signed in to change notification settings - Fork 2k
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
cli-plugins: merge OTEL_RESOURCE_ATTRIBUTES environment variable #5842
base: master
Are you sure you want to change the base?
cli-plugins: merge OTEL_RESOURCE_ATTRIBUTES environment variable #5842
Conversation
Merge `OTEL_RESOURCE_ATTRIBUTES` when there is one already in the environment. This allows user-specified resource attributes to be passed on to CLI plugins while still allowing the extra attributes added for telemetry information. This was the original intended use-case but it seems to have never made it in. The reason `OTEL_RESOURCE_ATTRIBUTES` was used is because we could combine it with user-centric ones. Signed-off-by: Jonathan A. Sternberg <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5842 +/- ##
==========================================
+ Coverage 59.16% 59.22% +0.05%
==========================================
Files 352 352
Lines 29567 29607 +40
==========================================
+ Hits 17493 17534 +41
+ Misses 11092 11087 -5
- Partials 982 986 +4 |
1cad61f
to
ac5757f
Compare
cli-plugins/manager/cobra.go
Outdated
// Construct baggage members for each of the attributes. | ||
// Ignore any failures as these aren't significant and | ||
// represent an internal issue. | ||
var b baggage.Baggage |
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.
wouldn't b
be nil on the first iteration?
Why not have a slice of members and then add it to the baggage afterwards.
var members []baggage.Member
for iter := attrs.Iter(); iter.Next(); {
m, err := baggage.NewMemberRaw(string(attr.Key), attr.Value.AsString())
if err != nil {
continue
}
members = append(members, m)
}
b := baggage.New(members...)
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.
Baggage
operates similar to a slice so it being nil isn't really a problem. SetMember
is similar to calling append
on a slice. Looking at the implementation, there is a possibility there's a slight performance improvement for New
if the number of baggage members are above 1, but this will only ever be 1. I can change it though since it's not a big deal.
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.
Thank you! it was just quite difficult to read, since we were assigning to a variable in the outer-scope on every iteration - just looked strange.
for //
newB, err := b.SetMember(m)
// handle err
b = newB
Remove the `docker.cli` prefixed attributes from `OTEL_RESOURCE_ATTRIBUTES` after the telemetry provider has been created within a plugin. This prevents accidentally sending the attributes to something downstream for the user. This also fixes an issue with compose where the self-injected `OTEL_RESOURCE_ATTRIBUTES` would override an existing attribute in the environment file because the "user environment" overrode the environment file, but the "user environment" was created by the `docker` tool rather than by the user's environment. When `OTEL_RESOURCE_ATTRIBUTES` is empty after pruning, the environment variable is unset. Signed-off-by: Jonathan A. Sternberg <[email protected]>
ac5757f
to
8890a1c
Compare
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.
LGTM
Sorry, away from keyboard for some hours; is any of these depending on generics or otherwise that would require a "//go:build" hack? cc @vvoland we probably should run the go module check to be safe |
(I tentatively added this to the v28.0 milestone, but wasn't sure how critical it was) |
@thaJeztah no generics or any |
- What I did
Merge
OTEL_RESOURCE_ATTRIBUTES
when there is one already in the environment. This allows user-specified resource attributes to be passed on to CLI plugins while still allowing the extra attributes added for telemetry information.This was the original intended use-case but it seems to have never made it in. The reason
OTEL_RESOURCE_ATTRIBUTES
was used is because we could combine it with user-centric ones.Additionally, remove the
docker.cli
prefixed attributes fromOTEL_RESOURCE_ATTRIBUTES
after the telemetry provider has been created within a plugin. This prevents accidentally sending the attributes to something downstream for the user.This also fixes an issue with compose where the self-injected
OTEL_RESOURCE_ATTRIBUTES
would override an existing attribute in the environment file because the "user environment" overrode the environment file, but the "user environment" was created by thedocker
tool rather than by the user's environment.When
OTEL_RESOURCE_ATTRIBUTES
is empty after pruning, the environmentvariable is unset.
Fixes #4958.
- How I did it
Check the environment variable and combine it with a comma to ensure all attributes are present in the final environment variable. The injected attributes take priority but they are namespaced and shouldn't conflict with user-specified ones.
For sanitizing the environment variable, I used the initialize method from the plugin to look for
docker.cli
attributes in the environment and remove them.- How to verify it
Should be able to verify by compiling compose with the change and running this with this
compose.yml
.And this
.env
file.If you see
a.b.c=foobar
in the output it's working. You can also run the above with settingOTEL_RESOURCE_ATTRIBUTES=a.b.d=foobar docker compose up
and if you seea.b.d
then that shows the first part is working. When both are together, onlya.b.d=foobar
will be present. When only the CLI is fixed and compose is still without the change, you'll seea.b.d=foobar
with the one the CLI injects.- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)