-
Notifications
You must be signed in to change notification settings - Fork 20
feature(bottlecap): add trace context extractor #401
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
Merged
astuyve
merged 9 commits into
jordan.gonzalez/bottlecap/universal-instrumentation
from
jordan.gonzalez/bottlecap/add-trace-propagator
Oct 9, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a03aa6e
add `thiserror` and `lazystatic`
duncanista 4f7947f
add Span/Trace `context`
duncanista 48dcac3
update `mod.rs`
duncanista a46f6a2
add `propagation` module
duncanista fe353ca
add `propagation::Error`
duncanista 964d663
add interface for `carrier` and `HashMap` implementation
duncanista 59fda72
add `text_map_propagator`
duncanista d7859a6
update `LICENSE-3rdparty.yml`
duncanista 801e5a8
Merge branch 'jordan.gonzalez/bottlecap/universal-instrumentation' in…
duncanista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| #[derive(Copy, Clone, Default, Debug)] | ||
| pub struct Sampling { | ||
| pub priority: Option<i8>, | ||
| pub mechanism: Option<u8>, | ||
| } | ||
|
|
||
| #[derive(Clone, Default, Debug)] | ||
| #[allow(clippy::module_name_repetitions)] | ||
| pub struct SpanContext { | ||
| pub trace_id: u64, | ||
| pub span_id: u64, | ||
| pub sampling: Option<Sampling>, | ||
| pub origin: Option<String>, | ||
| pub tags: HashMap<String, String>, | ||
| } |
This file contains hidden or 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 hidden or 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,68 @@ | ||
| /// Code inspired, and copied, by OpenTelemetry Rust project. | ||
| /// <https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry/src/propagation/mod.rs> | ||
| /// | ||
| use std::collections::HashMap; | ||
|
|
||
| /// Injector provides an interface for a carrier to be used | ||
| /// with a Propagator to inject a Context into the carrier. | ||
| /// | ||
| pub trait Injector { | ||
| /// Set a value in the carrier. | ||
| fn set(&mut self, key: &str, value: String); | ||
| } | ||
|
|
||
| pub trait Extractor { | ||
| /// Get a value from the carrier. | ||
| fn get(&self, key: &str) -> Option<&str>; | ||
|
|
||
| /// Get all keys from the carrier. | ||
| fn keys(&self) -> Vec<&str>; | ||
| } | ||
|
|
||
| impl<S: std::hash::BuildHasher> Injector for HashMap<String, String, S> { | ||
| /// Set a key and value in the `HashMap`. | ||
| fn set(&mut self, key: &str, value: String) { | ||
| self.insert(key.to_lowercase(), value); | ||
| } | ||
| } | ||
|
|
||
| impl<S: std::hash::BuildHasher> Extractor for HashMap<String, String, S> { | ||
| /// Get a value for a key from the `HashMap`. | ||
| fn get(&self, key: &str) -> Option<&str> { | ||
| self.get(&key.to_lowercase()).map(String::as_str) | ||
| } | ||
|
|
||
| /// Collect all the keys from the `HashMap`. | ||
| fn keys(&self) -> Vec<&str> { | ||
| self.keys().map(String::as_str).collect::<Vec<_>>() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn hash_map_get() { | ||
| let mut carrier = HashMap::new(); | ||
| carrier.set("headerName", "value".to_string()); | ||
|
|
||
| assert_eq!( | ||
| Extractor::get(&carrier, "HEADERNAME"), | ||
| Some("value"), | ||
| "case insensitive extraction" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn hash_map_keys() { | ||
| let mut carrier = HashMap::new(); | ||
| carrier.set("headerName1", "value1".to_string()); | ||
| carrier.set("headerName2", "value2".to_string()); | ||
|
|
||
| let got = Extractor::keys(&carrier); | ||
| assert_eq!(got.len(), 2); | ||
| assert!(got.contains(&"headername1")); | ||
| assert!(got.contains(&"headername2")); | ||
| } | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Error, Debug, Copy, Clone)] | ||
| #[error("Cannot {} from {}, {}", operation, message, propagator_name)] | ||
| pub struct Error { | ||
| message: &'static str, | ||
| // which propagator this error comes from | ||
| propagator_name: &'static str, | ||
| // what operation was attempted | ||
| operation: &'static str, | ||
| } | ||
|
|
||
| impl Error { | ||
| /// Error when extracting a value from a carrier | ||
| #[must_use] | ||
| pub fn extract(message: &'static str, propagator_name: &'static str) -> Self { | ||
| Self { | ||
| message, | ||
| propagator_name, | ||
| operation: "extract", | ||
| } | ||
| } | ||
|
|
||
| /// Error when injecting a value into a carrier | ||
| #[allow(clippy::must_use_candidate)] | ||
| pub fn inject(message: &'static str, propagator_name: &'static str) -> Self { | ||
| Self { | ||
| message, | ||
| propagator_name, | ||
| operation: "inject", | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,3 @@ | ||
| pub mod carrier; | ||
| pub mod error; | ||
| pub mod text_map_propagator; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Same for these 2. Can we avoid/postpone creating extra traits?
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.
This allows us to directly implement this traits for carriers which we might not have control of, for example, here I tend to convert
hyper::HeaderMapinto aHashMapso I can use theextractandinjectmethods on the struct right away.At the top of the file I explain that I directly copied this from the OTel rust implementation for extractor/injectors.
I think this would help us whenever we have to expand, and its also very generic, what do you think of this?