-
Notifications
You must be signed in to change notification settings - Fork 766
envcar: add environment carrier #8442
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
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
478ba68
feat: add environment carrier
Joibel aa72967
test: add examples
Joibel f58fdd1
Merge branch 'main' into envcar
Joibel 02deedc
Update CODEOWNERS
Joibel edad481
Update CHANGELOG.md
Joibel 166eab6
chore: remove error return and add test
Joibel 1b54363
Update propagators/envcar/carrier.go
Joibel 7533bf3
Merge branch 'main' into envcar
Joibel 9ea9d63
feat: persistent values from carrier
Joibel f819347
chore: various code review feedback items
Joibel 35a4228
Merge branch 'main' into envcar
Joibel bd65603
chore: move changelog entry
Joibel c29d61e
docs: improve godocs
Joibel a27cbe6
chore: go.sum
Joibel feb0dc2
Update propagators/envcar/carrier.go
Joibel 311ae22
Update propagators/envcar/carrier.go
Joibel ba6e72b
Update propagators/envcar/carrier.go
Joibel 2025db5
Update propagators/envcar/carrier.go
Joibel 4a8eead
Update propagators/envcar/carrier.go
Joibel c667885
chore: fix test
Joibel e4a9322
Merge remote-tracking branch 'upstream/main' into envcar
Joibel 1e24f82
feat: use underscores for non-A-Z
Joibel ab56ec0
chore: linter fix
Joibel 4556ab9
chore: fix up the comments and linter problems
Joibel 1dca629
Merge branch 'main' into envcar
pellared d776978
Fix CHANGELOG
pellared 1f25da6
Merge branch 'main' into envcar
pellared 2e78d6b
docs: small updates from code review
Joibel f3bac0d
test: new keys and get test
Joibel bb38d84
Merge branch 'main' into envcar
Joibel 517f352
Update propagators/envcar/carrier_test.go
Joibel 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
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,111 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package envcar_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
|
|
||
| "go.opentelemetry.io/otel/propagation" | ||
| "go.opentelemetry.io/otel/trace" | ||
|
|
||
| "go.opentelemetry.io/contrib/propagators/envcar" | ||
| ) | ||
|
|
||
| // This example is a go program where the environment variables are carrying the | ||
| // trace information, and we're going to pick them up into our context. | ||
| func ExampleCarrier_extractFromParent() { | ||
| // Simulate environment variables set by a parent process. | ||
| // In practice, these would already be set when this process starts. | ||
| _ = os.Setenv("TRACEPARENT", "00-0102030405060708090a0b0c0d0e0f10-0102030405060708-01") | ||
|
|
||
| // Create a carrier to read trace context from environment variables. | ||
| carrier := envcar.Carrier{} | ||
|
|
||
| // Extract trace context that was propagated by the parent process. | ||
| prop := propagation.TraceContext{} | ||
| ctx := prop.Extract(context.Background(), carrier) | ||
|
|
||
| // The context now contains the span context from the parent. | ||
| spanCtx := trace.SpanContextFromContext(ctx) | ||
| fmt.Printf("Trace ID: %s\n", spanCtx.TraceID()) | ||
| fmt.Printf("Span ID: %s\n", spanCtx.SpanID()) | ||
| fmt.Printf("Sampled: %t\n", spanCtx.IsSampled()) | ||
| // Output: | ||
| // Trace ID: 0102030405060708090a0b0c0d0e0f10 | ||
| // Span ID: 0102030405060708 | ||
| // Sampled: true | ||
| } | ||
|
|
||
| // This example is a go program where we have a trace and we'd like to inject it | ||
| // in our current environment variables. | ||
|
pellared marked this conversation as resolved.
Outdated
|
||
| func ExampleCarrier_injectWithSetenv() { | ||
| // Create a span context with a known trace ID. | ||
| traceID := trace.TraceID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} | ||
| spanID := trace.SpanID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} | ||
| spanCtx := trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: traceID, | ||
| SpanID: spanID, | ||
| TraceFlags: trace.FlagsSampled, | ||
| }) | ||
| ctx := trace.ContextWithSpanContext(context.Background(), spanCtx) | ||
|
|
||
| // Create a carrier that sets environment variables in the current process. | ||
| // This is useful when about to exec() or when child processes will | ||
| // inherit the current environment. | ||
| carrier := envcar.Carrier{ | ||
| SetEnvFunc: os.Setenv, | ||
| } | ||
|
|
||
| // Inject trace context into the current process's environment. | ||
| prop := propagation.TraceContext{} | ||
| prop.Inject(ctx, carrier) | ||
|
|
||
| // The environment variable is now set in the current process. | ||
| fmt.Println(os.Getenv("TRACEPARENT")) | ||
| // Output: 00-0102030405060708090a0b0c0d0e0f10-0102030405060708-01 | ||
| } | ||
|
|
||
| // This example is a go program where we have a trace and we'd like to inject it | ||
| // into a command we're going to run. | ||
| func ExampleCarrier_childProcess() { | ||
| // Create a span context with a known trace ID. | ||
| traceID := trace.TraceID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} | ||
| spanID := trace.SpanID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} | ||
| spanCtx := trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: traceID, | ||
| SpanID: spanID, | ||
| TraceFlags: trace.FlagsSampled, | ||
| }) | ||
| ctx := trace.ContextWithSpanContext(context.Background(), spanCtx) | ||
|
|
||
| // Prepare a command that prints the TRACEPARENT environment variable. | ||
| cmd := exec.Command("printenv", "TRACEPARENT") | ||
| cmd.Env = os.Environ() | ||
|
|
||
| // Create a carrier that injects trace context into the child | ||
| // process's environment rather than the current process's. | ||
| carrier := envcar.Carrier{ | ||
| SetEnvFunc: func(key, value string) error { | ||
| cmd.Env = append(cmd.Env, key+"="+value) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| // Inject trace context into the child's environment. | ||
| prop := propagation.TraceContext{} | ||
| prop.Inject(ctx, carrier) | ||
|
|
||
| // The child process now has trace context in its environment, | ||
| // independent of the parent process's environment variables. | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| fmt.Println("error:", err) | ||
| return | ||
| } | ||
| fmt.Print(string(out)) | ||
| // Output: 00-0102030405060708090a0b0c0d0e0f10-0102030405060708-01 | ||
| } | ||
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
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.