-
Notifications
You must be signed in to change notification settings - Fork 683
feat(api): add experimental env var propagators #3574
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
pellared
wants to merge
14
commits into
open-telemetry:main
Choose a base branch
from
pellared:pellared-env-var-propagation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d5fdcd0
feat(api): add experimental env var propagators
pellared 718f9c2
fix(api): make env carrier snapshots explicit
pellared 084b340
docs(example): clarify env var propagation flow
pellared 99352a3
refactor(api): deduplicate env carrier entry collection
pellared eee36a9
refactor(api): simplify env carrier entry collection
pellared cc10893
refactor(api): streamline env var injector API
pellared 16d2ec1
refactor(api): simplify env var injector
pellared 8a185f0
docs(api): explain snapshoting in EnvVarExtractor
pellared 55a5af6
feat(api): add targeted env var extraction
pellared f1df492
document case sensitivity
pellared 05a39d5
fix(api): filter non-normalized env carrier entries
pellared 06fc2ec
remove redundant assertion
pellared 7fe831d
then instead of if-else
pellared 8abb673
Merge branch 'main' into pellared-env-var-propagation
pellared 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [package] | ||
| name = "env-var-propagation" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| license = "Apache-2.0" | ||
| rust-version = "1.75.0" | ||
| publish = false | ||
| autobenches = false | ||
|
|
||
| [[bin]] | ||
| name = "env-var-propagation" | ||
| path = "src/main.rs" | ||
| doc = false | ||
| bench = false | ||
|
|
||
| [dependencies] | ||
| opentelemetry = { workspace = true, features = ["trace", "otel_unstable"] } | ||
| opentelemetry_sdk = { workspace = true, features = ["trace"] } | ||
| opentelemetry-stdout = { workspace = true, features = ["trace"] } |
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,35 @@ | ||
| # Environment Variable Propagation Example | ||
|
|
||
| This example demonstrates distributed tracing across a parent process and a | ||
| child process using environment variables as the propagation carrier. | ||
|
|
||
| ## What This Example Does | ||
|
|
||
| 1. The parent process configures the W3C `TraceContextPropagator` and starts a | ||
| root span. | ||
| 2. The parent injects that span context into an `EnvVarInjector`, then passes | ||
| the resulting environment variables to a child process. The child inherits | ||
| the rest of the parent's environment through `std::process::Command`. | ||
| 3. The child process builds an `EnvVarExtractor` from the active propagator's | ||
| fields, extracts the propagated context, and starts its own span as a child | ||
| of the parent's span. | ||
|
|
||
| The example prints the parent's `TraceId` and `SpanId`, then prints the child's | ||
| `TraceId` and remote parent `SpanId` so you can see that the trace was | ||
| propagated across the process boundary. | ||
|
|
||
| ## Usage | ||
|
|
||
| From `examples/env-var-propagation`, run: | ||
|
|
||
| ```shell | ||
| cargo run | ||
| ``` | ||
|
|
||
| The output should show: | ||
|
|
||
| - the parent and child spans sharing the same `TraceId` | ||
| - the child's reported `parent_span_id` matching the parent's `span_id` | ||
|
|
||
| Both processes also export their spans to stdout using the OpenTelemetry stdout | ||
| exporter. |
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,93 @@ | ||
| use opentelemetry::{ | ||
| global, | ||
| propagation::{EnvVarExtractor, EnvVarInjector}, | ||
| trace::{Span, TraceContextExt, Tracer}, | ||
| Context, | ||
| }; | ||
| use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::SdkTracerProvider}; | ||
| use opentelemetry_stdout::SpanExporter; | ||
| use std::{env, error::Error, io, process::Command}; | ||
|
|
||
| fn init_tracer() -> SdkTracerProvider { | ||
| // Parent and child both use W3C Trace Context when encoding env vars. | ||
| global::set_text_map_propagator(TraceContextPropagator::new()); | ||
|
|
||
| let provider = SdkTracerProvider::builder() | ||
| .with_simple_exporter(SpanExporter::default()) | ||
| .build(); | ||
| global::set_tracer_provider(provider.clone()); | ||
|
|
||
| provider | ||
| } | ||
|
|
||
| fn run_parent() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | ||
| let tracer = global::tracer("examples/env-var-propagation/parent"); | ||
| let span = tracer.start("parent"); | ||
| let span_context = span.span_context().clone(); | ||
| let cx = Context::current_with_span(span); | ||
|
|
||
| println!( | ||
| "parent trace_id={} span_id={}", | ||
| span_context.trace_id(), | ||
| span_context.span_id() | ||
| ); | ||
|
|
||
| // Inject into a fresh environment map. `Command` inherits the parent | ||
| // environment by default, and `envs` adds the propagated context variables. | ||
| let mut child_env = EnvVarInjector::new(); | ||
| global::get_text_map_propagator(|propagator| propagator.inject_context(&cx, &mut child_env)); | ||
|
|
||
| // Re-exec the current binary in child mode to keep the example self-contained. | ||
| let status = Command::new(env::current_exe()?) | ||
| .arg("--child") | ||
| .envs(child_env) | ||
| .status()?; | ||
|
|
||
| cx.span().end(); | ||
|
|
||
| if status.success() { | ||
| Ok(()) | ||
| } else { | ||
| Err(io::Error::other(format!("child process failed with status {status}")).into()) | ||
| } | ||
| } | ||
|
|
||
| fn run_child() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | ||
| let parent_cx = global::get_text_map_propagator(|propagator| { | ||
| // Read only the environment variables used by the active propagator. | ||
| let extractor = EnvVarExtractor::from_fields(propagator.fields()); | ||
| propagator.extract(&extractor) | ||
| }); | ||
| let remote_parent = parent_cx.span().span_context().clone(); | ||
|
|
||
| if !remote_parent.is_valid() { | ||
| return Err(io::Error::other("missing propagated span context").into()); | ||
| } | ||
|
|
||
| let tracer = global::tracer("examples/env-var-propagation/child"); | ||
| let mut span = tracer.start_with_context("child", &parent_cx); | ||
| let span_context = span.span_context().clone(); | ||
|
|
||
| println!( | ||
| "child trace_id={} parent_span_id={}", | ||
| span_context.trace_id(), | ||
| remote_parent.span_id() | ||
| ); | ||
|
|
||
| span.end(); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | ||
| let provider = init_tracer(); | ||
| // The parent launches the same executable with `--child` to demonstrate | ||
| // propagation across a real process boundary. | ||
| let result = match env::args().nth(1).as_deref() { | ||
| Some("--child") => run_child(), | ||
| _ => run_parent(), | ||
| }; | ||
|
|
||
| provider.shutdown()?; | ||
| result | ||
| } | ||
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.