Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions opentelemetry-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
.map(|value| value.as_str())
.collect::<Vec<_>>()
}

/// Get all the values for a key from the HeaderMap
fn get_all(&self, key: &str) -> Option<Vec<&str>> {
let all_iter = self.0.get_all(key).iter();
if let (0, Some(0)) = all_iter.size_hint() {
return None;
}

Some(all_iter.filter_map(|value| value.to_str().ok()).collect())
}
}

pub type HttpError = Box<dyn std::error::Error + Send + Sync + 'static>;
Expand Down Expand Up @@ -236,6 +246,8 @@

#[cfg(test)]
mod tests {
use http::HeaderValue;

use super::*;

#[test]
Expand All @@ -250,6 +262,32 @@
)
}

#[test]
fn http_headers_get_all() {
let mut carrier = http::HeaderMap::new();
carrier.append("headerName", HeaderValue::from_static("value"));
carrier.append("headerName", HeaderValue::from_static("value2"));
carrier.append("headerName", HeaderValue::from_static("value3"));

assert_eq!(
HeaderExtractor(&carrier).get_all("HEADERNAME"),
Some(vec!["value", "value2", "value3"]),
"all values from a key extraction"

Check warning on line 275 in opentelemetry-http/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-http/src/lib.rs#L275

Added line #L275 was not covered by tests
)
}

#[test]
fn http_headers_get_all_missing_key() {
let mut carrier = http::HeaderMap::new();
carrier.append("headerName", HeaderValue::from_static("value"));

assert_eq!(
HeaderExtractor(&carrier).get_all("not_existing"),
None,
"all values from a missing key extraction"

Check warning on line 287 in opentelemetry-http/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-http/src/lib.rs#L287

Added line #L287 was not covered by tests
)
}

#[test]
fn http_headers_keys() {
let mut carrier = http::HeaderMap::new();
Expand Down
29 changes: 29 additions & 0 deletions opentelemetry/src/propagation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@

/// Collect all the keys from the underlying data.
fn keys(&self) -> Vec<&str>;

/// Get all values from a key from the underlying data.
fn get_all(&self, key: &str) -> Option<Vec<&str>> {
self.get(key).map(|value| vec![value])
}
}

impl<S: std::hash::BuildHasher> Injector for HashMap<String, String, S> {
Expand Down Expand Up @@ -77,6 +82,30 @@
);
}

#[test]
fn hash_map_get_all() {
let mut carrier = HashMap::new();
carrier.set("headerName", "value".to_string());

assert_eq!(
Extractor::get_all(&carrier, "HEADERNAME"),
Some(vec!["value"]),
"case insensitive get_all extraction"

Check warning on line 93 in opentelemetry/src/propagation/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry/src/propagation/mod.rs#L93

Added line #L93 was not covered by tests
);
}

#[test]
fn hash_map_get_all_missing_key() {
let mut carrier = HashMap::new();
carrier.set("headerName", "value".to_string());

assert_eq!(
Extractor::get_all(&carrier, "missing_key"),
None,
"case insensitive get_all extraction"

Check warning on line 105 in opentelemetry/src/propagation/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry/src/propagation/mod.rs#L105

Added line #L105 was not covered by tests
);
}

#[test]
fn hash_map_keys() {
let mut carrier = HashMap::new();
Expand Down