-
-
Notifications
You must be signed in to change notification settings - Fork 188
chore(tracing): Store incoming baggage org ID #1209
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
Open
szokeasaurusrex
wants to merge
1
commit into
szokeasaurusrex/trace-propagation-context
from
szokeasaurusrex/org-id-baggage-parsing
+156
−3
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -3,17 +3,23 @@ | |
| use std::error::Error; | ||
| use std::fmt::{Display, Formatter, Result as FmtResult}; | ||
|
|
||
| use sentry_types::protocol::v7::OrganizationId; | ||
|
|
||
| use crate::protocol::{SpanId, TraceId}; | ||
|
|
||
| /// A key-value header pair. | ||
| type Header<'h> = (&'h str, &'h str); | ||
|
|
||
| /// The baggage key for the Sentry org ID. | ||
| const SENTRY_ORG_ID: &str = "sentry-org_id"; | ||
|
|
||
| /// The [trace propagation] context. | ||
| /// | ||
| /// Contains the information necessary for propagating Sentry traces and continuing traces from | ||
| /// incoming requests. | ||
| /// | ||
| /// The data stored in this struct can be parsed from and transmitted as `sentry-trace` headers. | ||
| /// The data stored in this struct can be parsed from and transmitted as `sentry-trace` and Sentry | ||
| /// baggage headers. | ||
| /// | ||
| /// Note that the Rust SDK only partially supports trace propagation, certain features such as | ||
| /// [dynamic sampling] may be missing or incomplete. | ||
|
|
@@ -25,6 +31,7 @@ pub struct TracePropagationContext { | |
| pub(crate) trace_id: TraceId, | ||
| pub(crate) span_id: SpanId, | ||
| pub(super) sampled: Option<bool>, | ||
| pub(super) org_id: Option<OrganizationId>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
|
|
@@ -53,6 +60,7 @@ impl TracePropagationContext { | |
| trace_id, | ||
| span_id, | ||
| sampled: None, | ||
| org_id: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -68,6 +76,7 @@ impl TracePropagationContext { | |
| trace_id, | ||
| span_id, | ||
| sampled, | ||
| org_id: _, | ||
| } = self; | ||
|
|
||
| let sampled_suffix = sampled | ||
|
|
@@ -85,6 +94,7 @@ impl TracePropagationContext { | |
| I: IntoIterator<Item = Header<'a>>, | ||
| { | ||
| let mut context_result = Err(HeaderParseError::Missing); | ||
| let mut baggage = SentryBaggage::default(); | ||
|
|
||
| for (header, value) in headers { | ||
| if header.eq_ignore_ascii_case("sentry-trace") { | ||
|
|
@@ -93,10 +103,15 @@ impl TracePropagationContext { | |
| context_result = TracePropagationContext::from_sentry_trace(value) | ||
| .map_or(context_result, Ok) | ||
| .map_err(|_| HeaderParseError::Invalid); | ||
| } else if header.eq_ignore_ascii_case("baggage") { | ||
| baggage.update_from_header(value); | ||
| } | ||
| } | ||
|
|
||
| context_result | ||
| let context = context_result?; | ||
|
|
||
| let SentryBaggage { org_id } = baggage; | ||
| Ok(TracePropagationContext { org_id, ..context }) | ||
| } | ||
|
|
||
| /// Attempts to construct a [`TracePropagationContext`] from the given Sentry trace header. | ||
|
|
@@ -118,6 +133,7 @@ impl TracePropagationContext { | |
| trace_id, | ||
| span_id, | ||
| sampled, | ||
| org_id: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -134,6 +150,7 @@ where | |
| trace_id, | ||
| span_id, | ||
| sampled, | ||
| org_id: _, | ||
| } = TracePropagationContext::try_from_headers(headers).ok()?; | ||
|
|
||
| Some(SentryTrace { | ||
|
|
@@ -175,6 +192,7 @@ impl From<SentryTrace> for TracePropagationContext { | |
| trace_id: trace.trace_id, | ||
| span_id: trace.span_id, | ||
| sampled: trace.sampled, | ||
| org_id: None, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -190,6 +208,42 @@ impl std::fmt::Display for SentryTrace { | |
| } | ||
| } | ||
|
|
||
| /// A struct containing known Sentry baggage values. | ||
| /// | ||
| /// For now, this only includes the `org_id`, but we can add more values as we support them. | ||
| #[derive(Debug, Default)] | ||
| struct SentryBaggage { | ||
| org_id: Option<OrganizationId>, | ||
| } | ||
|
|
||
| impl SentryBaggage { | ||
| /// Update `self` with the known Sentry baggage values in the provided [baggage header]. | ||
| /// | ||
| /// The header is parsed according to the W3C baggage format: entries are separated by | ||
| /// commas, each entry is a key-value pair separated by `=`, and optional properties after | ||
| /// a semicolon are ignored. | ||
| /// | ||
| /// [baggage header]: https://www.w3.org/TR/baggage/ | ||
| fn update_from_header(&mut self, value: &str) { | ||
| value | ||
| .split(',') | ||
| .flat_map(|s| s.split_once('=')) | ||
| // Discard optional values after semicolon. | ||
| .map(|(key, value)| (key, value.split_once(';').map_or(value, |(v, _)| v))) | ||
| .map(|(key, value)| (key.trim(), value.trim())) | ||
| .for_each(|(key, value)| self.update_from_value(key, value)) | ||
| } | ||
|
|
||
| /// Update `self` with a key-value pair from the baggage header. | ||
| /// | ||
| /// The value is only updated if it is valid, otherwise the old value is kept. | ||
| fn update_from_value(&mut self, key: &str, value: &str) { | ||
| if key == SENTRY_ORG_ID { | ||
| self.org_id = value.parse().ok().or(self.org_id); | ||
| } | ||
| } | ||
|
Comment on lines
+227
to
+244
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. nit: I think we can just have a |
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
@@ -210,6 +264,7 @@ mod tests { | |
| trace_id, | ||
| span_id: parent_trace_id, | ||
| sampled: Some(false), | ||
| org_id: None, | ||
| } | ||
| ); | ||
|
|
||
|
|
@@ -221,4 +276,63 @@ mod tests { | |
| .expect("should parse successfully"); | ||
| assert_eq!(parsed, trace); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parses_baggage_org_id() { | ||
| let trace = TracePropagationContext::try_from_headers([ | ||
| ( | ||
| "sentry-trace", | ||
| "09e04486820349518ac7b5d2adbf6ba5-9cf635fa5b870b3a-0", | ||
| ), | ||
| ("baggage", "sentry-org_id=123"), | ||
| ]) | ||
| .expect("should parse successfully"); | ||
|
|
||
| assert_eq!(trace.org_id, Some("123".parse().unwrap())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parses_baggage_org_id_with_unrelated_fields() { | ||
| let trace = TracePropagationContext::try_from_headers([ | ||
| ( | ||
| "sentry-trace", | ||
| "09e04486820349518ac7b5d2adbf6ba5-9cf635fa5b870b3a-0", | ||
| ), | ||
| ( | ||
| "baggage", | ||
| "other=value, sentry-org_id=123, another=value;property", | ||
| ), | ||
| ]) | ||
| .expect("should parse successfully"); | ||
|
|
||
| assert_eq!(trace.org_id, Some("123".parse().unwrap())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn accepts_mixed_case_baggage_header_name() { | ||
| let trace = TracePropagationContext::try_from_headers([ | ||
| ( | ||
| "sentry-trace", | ||
| "09e04486820349518ac7b5d2adbf6ba5-9cf635fa5b870b3a-0", | ||
| ), | ||
| ("BagGaGe", "sentry-org_id=123"), | ||
| ]) | ||
| .expect("should parse successfully"); | ||
|
|
||
| assert_eq!(trace.org_id, Some("123".parse().unwrap())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn treats_malformed_baggage_org_id_as_absent() { | ||
| let trace = TracePropagationContext::try_from_headers([ | ||
| ( | ||
| "sentry-trace", | ||
| "09e04486820349518ac7b5d2adbf6ba5-9cf635fa5b870b3a-0", | ||
| ), | ||
| ("baggage", "sentry-org_id=not-an-org-id"), | ||
| ]) | ||
| .expect("should parse successfully"); | ||
|
|
||
| assert_eq!(trace.org_id, None); | ||
| } | ||
| } | ||
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
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.
Maybe we can also add
sentry-traceandbaggageas constants for consistency