-
Notifications
You must be signed in to change notification settings - Fork 683
fix(resource): percent-decode attribute values #3586
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 all commits
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| //! variables. | ||
| use crate::resource::{Resource, ResourceDetector}; | ||
| use opentelemetry::{Key, KeyValue, Value}; | ||
| use percent_encoding::percent_decode_str; | ||
| use std::env; | ||
|
|
||
| const OTEL_RESOURCE_ATTRIBUTES: &str = "OTEL_RESOURCE_ATTRIBUTES"; | ||
|
|
@@ -50,9 +51,13 @@ fn construct_otel_resources(s: String) -> Resource { | |
| None => return None, | ||
| }; | ||
| let key = parts.0.trim(); | ||
| let value = parts.1.trim(); | ||
| // Trim OWS *before* percent-decoding so that any encoded whitespace | ||
| // (e.g. `%20`) in the value is preserved. Per the Resource SDK spec | ||
| // the value MUST be percent-decoded; keys are baggage tokens and are | ||
| // not encoded. | ||
| let value = percent_decode_str(parts.1.trim()).decode_utf8_lossy(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| Some(KeyValue::new(key.to_owned(), value.to_owned())) | ||
| Some(KeyValue::new(key.to_owned(), value.into_owned())) | ||
| })) | ||
| .build() | ||
| } | ||
|
|
@@ -124,7 +129,7 @@ mod tests { | |
| [ | ||
| ( | ||
| "OTEL_RESOURCE_ATTRIBUTES", | ||
| Some("key=value, k = v , a= x, a=z,base64=SGVsbG8sIFdvcmxkIQ=="), | ||
| Some("key=value, k = v , a= x, a=z,base64=SGVsbG8sIFdvcmxkIQ==,encoded=user%20id%3D42%2C%20name%3D%22foo%22"), | ||
| ), | ||
| ("IRRELEVANT", Some("20200810")), | ||
| ], | ||
|
|
@@ -140,6 +145,8 @@ mod tests { | |
| KeyValue::new("a", "x"), | ||
| KeyValue::new("a", "z"), | ||
| KeyValue::new("base64", "SGVsbG8sIFdvcmxkIQ=="), // base64('Hello, World!') | ||
| // Values are percent-decoded per the Resource SDK spec. | ||
| KeyValue::new("encoded", "user id=42, name=\"foo\""), | ||
| ]) | ||
| .build() | ||
| ); | ||
|
|
||
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 believe we should also percent-decode
keys. As per specs:The OTEL_RESOURCE_ATTRIBUTES environment variable will contain of a list of key value pairs, represented as key1=value1,key2=value2. All attribute values MUST be considered strings. The , and = characters in keys and values MUST be percent encoded. Other characters MAY be [percent-encoded](https://datatracker.ietf.org/doc/html/rfc3986#section-2.1), e.g. values outside the ANSI characters set.