-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3159: feat(telemetry): extract otel context & associate with HTTP request spans r=fnichol a=fnichol <img src="https://media2.giphy.com/media/gLiNeyEhLoLlshprKm/giphy.gif"/> Co-authored-by: Fletcher Nichol <[email protected]>
- Loading branch information
Showing
8 changed files
with
151 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use http::HeaderMap; | ||
use telemetry::opentelemetry::{global, Context}; | ||
|
||
/// Extracts an OpenTelemetry [`Context`] from a [`HeaderMap`]. | ||
pub fn extract_opentelemetry_context(headers: &HeaderMap) -> Context { | ||
let extractor = self::headers::HeaderExtractor(headers); | ||
global::get_text_map_propagator(|propagator| propagator.extract(&extractor)) | ||
} | ||
|
||
/// Injects an OpenTelemetry [`Context`] into a [`HeaderMap`]. | ||
pub fn inject_opentelemetry_context(ctx: &Context, headers: &mut HeaderMap) { | ||
let mut injector = self::headers::HeaderInjector(headers); | ||
global::get_text_map_propagator(|propagator| propagator.inject_context(ctx, &mut injector)); | ||
} | ||
|
||
// Implementation vendored from `opentelemetry-http` crate, released under the Apache 2.0 license | ||
// | ||
// https://github.com/open-telemetry/opentelemetry-rust/blob/47881b20a2b8e94d8e1cdbd4877852dd74cc07de/opentelemetry-http/src/lib.rs#L13-L41 | ||
mod headers { | ||
use telemetry::opentelemetry::propagation::{Extractor, Injector}; | ||
|
||
pub struct HeaderInjector<'a>(pub &'a mut http::HeaderMap); | ||
|
||
impl<'a> Injector for HeaderInjector<'a> { | ||
/// Set a key and value in the HeaderMap. Does nothing if the key or value are not valid inputs. | ||
fn set(&mut self, key: &str, value: String) { | ||
if let Ok(name) = http::header::HeaderName::from_bytes(key.as_bytes()) { | ||
if let Ok(val) = http::header::HeaderValue::from_str(&value) { | ||
self.0.insert(name, val); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct HeaderExtractor<'a>(pub &'a http::HeaderMap); | ||
|
||
impl<'a> Extractor for HeaderExtractor<'a> { | ||
/// Get a value for a key from the HeaderMap. If the value is not valid ASCII, returns None. | ||
fn get(&self, key: &str) -> Option<&str> { | ||
self.0.get(key).and_then(|value| value.to_str().ok()) | ||
} | ||
|
||
/// Collect all the keys from the HeaderMap. | ||
fn keys(&self) -> Vec<&str> { | ||
self.0 | ||
.keys() | ||
.map(|value| value.as_str()) | ||
.collect::<Vec<_>>() | ||
} | ||
} | ||
} |
Oops, something went wrong.