-
Notifications
You must be signed in to change notification settings - Fork 847
Decode the OTEL_RESOURCE_ATTRIBUTES environment variable according to the spec #6461
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
b05c573
9e35708
aef230e
e244ed1
b59b52c
985617b
9191ac2
febb6c2
5263bb3
b55fea3
a9fbfc2
9624ed9
4bb160d
7c30944
7656241
2636bd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||
// Copyright The OpenTelemetry Authors | ||||||||||
// SPDX-License-Identifier: Apache-2.0 | ||||||||||
|
||||||||||
using System.Text; | ||||||||||
using Microsoft.Extensions.Configuration; | ||||||||||
|
||||||||||
namespace OpenTelemetry.Resources; | ||||||||||
|
@@ -38,15 +39,74 @@ private static List<KeyValuePair<string, object>> ParseResourceAttributes(string | |||||||||
string[] rawAttributes = resourceAttributes.Split(AttributeListSplitter); | ||||||||||
foreach (string rawKeyValuePair in rawAttributes) | ||||||||||
{ | ||||||||||
string[] keyValuePair = rawKeyValuePair.Split(AttributeKeyValueSplitter); | ||||||||||
if (keyValuePair.Length != 2) | ||||||||||
#if NETSTANDARD2_1 || NET8_0_OR_GREATER | ||||||||||
hannahhaering marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
var indexOfFirstEquals = rawKeyValuePair.IndexOf(AttributeKeyValueSplitter.ToString(), StringComparison.Ordinal); | ||||||||||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
#else | ||||||||||
var indexOfFirstEquals = rawKeyValuePair.IndexOf(AttributeKeyValueSplitter); | ||||||||||
#endif | ||||||||||
if (indexOfFirstEquals == -1) | ||||||||||
{ | ||||||||||
continue; | ||||||||||
} | ||||||||||
|
||||||||||
attributes.Add(new KeyValuePair<string, object>(keyValuePair[0].Trim(), keyValuePair[1].Trim())); | ||||||||||
var key = rawKeyValuePair.Substring(0, indexOfFirstEquals).Trim(); | ||||||||||
var value = rawKeyValuePair.Substring(indexOfFirstEquals + 1).Trim(); | ||||||||||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
var key = rawKeyValuePair.Substring(0, indexOfFirstEquals).Trim(); | |
var value = rawKeyValuePair.Substring(indexOfFirstEquals + 1).Trim(); | |
var key = rawKeyValuePair.AsSpan(0, indexOfFirstEquals).Trim().ToString(); | |
var value = rawKeyValuePair.AsSpan(indexOfFirstEquals + 1).Trim().ToString(); |
Outdated
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 we use WebUtility.UrlDecode()
here, rather than hand-roll our own?
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.
We can, but there is one difference. The UrlDecode method would replace + in the value string with space in the decoded string. This behavior is not part of the specification which uses only percent encoding (unlike application/x-www-form-urlencoded data, where + should be decoded into space). The current specification links to this.
However, client libraries in other languages seem to mostly use UrlDecode. If you prefer to follow their (incorrect 😄) approach I can change it.
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 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.
@martincostello, I would consider this as a bug. See: #5689
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 go with the simpler option first (i.e. the slightly non-compliant WebUtility.UrlDecode()
for consistency), then in a follow-up we can add a compliant implementation (maybe using the optimised/tested code from this method itself and amending as appropriate) as shared code, then update all the appropriate places at once to use it and fix the spec-drift all together?
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'm fine with both approaches. I don't feel qualified to decide this 😄 Could you tell me which decoding to use? @Kielek @martincostello
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.
@hannahhaering, any option that you can start with bugfixing baggage propagator with proper de/encoding? Based on this we can adjust this PR.
I would like to avoid introducing intentional bug.
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 can try to do this. I can start doing this in a few days (maybe earlier).
I will mark this PR as draft in the meantime.
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 implemented a new helper class for percent encoding. I used this to encode and decode the baggage in the propagator and in the resource detector.
I also added some unit tests. I added a test for non-ascii character decoding, this makes a the linter script fail. Any way around 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.
@Kielek I updated the tests and the linter doesn't complain anymore. I think this PR is ready. Do you have any other remarks/comments?
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.
These
string[]
andstring
allocations could be removed with span operations.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 considered using spans but decided against it since this method is only used for config parsing. Supporting mixed .NET targets (some without full span support) would require two versions, adding unnecessary complexity and reducing readability. I don’t think the trade-off is worth it here.
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.
Are you saying this is the only place in the whole codebase that splits strings and shouldn't?
Polyfills can be easily created for targets that miss some features. I've done that more than once.