Skip to content

Commit

Permalink
Support URL-encoded values for OTEL_EXPORTER_OTLP_HEADERS
Browse files Browse the repository at this point in the history
  • Loading branch information
pyohannes committed Feb 26, 2024
1 parent ab9415a commit 1e57366
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ tracing = { version = "0.1", default-features = false }
tracing-core = { version = "0.1", default-features = false }
tracing-subscriber = { version = "0.3", default-features = false }
url = { version = "2.2", default-features = false }
urlencoding = { version = "2.1.3" }
1 change: 1 addition & 0 deletions opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ reqwest = { workspace = true, optional = true }
http = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
thiserror = { workspace = true }
urlencoding = { workspace = true }

[dev-dependencies]
tokio-stream = { workspace = true, features = ["net"] }
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ fn add_header_from_string(input: &str, headers: &mut HashMap<HeaderName, HeaderV
headers.extend(parse_header_string(input).filter_map(|(key, value)| {
Some((
HeaderName::from_str(key).ok()?,
HeaderValue::from_str(value).ok()?,
HeaderValue::from_str(&value).ok()?,
))
}));
}
Expand Down
31 changes: 26 additions & 5 deletions opentelemetry-otlp/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,25 @@ impl<B: HasExportConfig> WithExportConfig for B {
}

#[cfg(any(feature = "grpc-tonic", feature = "http-proto"))]
fn parse_header_string(value: &str) -> impl Iterator<Item = (&str, &str)> {
fn parse_header_string(value: &str) -> impl Iterator<Item = (&str, String)> {
value
.split_terminator(',')
.map(str::trim)
.filter_map(parse_header_key_value_string)
}

#[cfg(any(feature = "grpc-tonic", feature = "http-proto"))]
fn parse_header_key_value_string(key_value_string: &str) -> Option<(&str, &str)> {
fn parse_header_key_value_string(key_value_string: &str) -> Option<(&str, String)> {
key_value_string
.split_once('=')
.map(|(key, value)| (key.trim(), value.trim()))
.map(|(key, value)| {
(
key.trim(),
urlencoding::decode(value.trim())
.unwrap_or_default()
.into_owned(),
)
})
.filter(|(key, value)| !key.is_empty() && !value.is_empty())
}

Expand Down Expand Up @@ -280,7 +287,10 @@ mod tests {
for (input_str, expected_headers) in test_cases {
assert_eq!(
super::parse_header_string(input_str).collect::<Vec<_>>(),
expected_headers,
expected_headers
.into_iter()
.map(|(k, v)| (k, v.to_string()))
.collect::<Vec<_>>(),
)
}
}
Expand All @@ -290,6 +300,14 @@ mod tests {
let test_cases = vec![
// Format: (input_str, expected_header)
("k1=v1", Some(("k1", "v1"))),
(
"Authentication=Basic AAA",
Some(("Authentication", "Basic AAA")),
),
(
"Authentication=Basic%20AAA",
Some(("Authentication", "Basic AAA")),
),
("", None),
("=v1", None),
("k1=", None),
Expand All @@ -298,7 +316,10 @@ mod tests {
for (input_str, expected_headers) in test_cases {
assert_eq!(
super::parse_header_key_value_string(input_str),
expected_headers,
match expected_headers {

Check failure on line 319 in opentelemetry-otlp/src/exporter/mod.rs

View workflow job for this annotation

GitHub Actions / lint

manual implementation of `Option::map`
Some((k, v)) => Some((k, v.to_string())),
None => None,
}
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/src/exporter/tonic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ fn parse_headers_from_env(signal_headers_var: &str) -> HeaderMap {
.filter_map(|(key, value)| {
Some((
HeaderName::from_str(key).ok()?,
HeaderValue::from_str(value).ok()?,
HeaderValue::from_str(&value).ok()?,
))
})
.collect::<HeaderMap>()
Expand Down

0 comments on commit 1e57366

Please sign in to comment.