| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package envcar_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/propagation" |
| 13 |
"go.opentelemetry.io/otel/trace" |
| 14 |
|
| 15 |
"go.opentelemetry.io/contrib/propagators/envcar" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func ExampleCarrier_extractFromParent() { |
| 21 |
|
| 22 |
|
| 23 |
_ = os.Setenv("TRACEPARENT", "00-0102030405060708090a0b0c0d0e0f10-0102030405060708-01") |
| 24 |
|
| 25 |
|
| 26 |
carrier := envcar.Carrier{} |
| 27 |
|
| 28 |
|
| 29 |
prop := propagation.TraceContext{} |
| 30 |
ctx := prop.Extract(context.Background(), &carrier) |
| 31 |
|
| 32 |
|
| 33 |
spanCtx := trace.SpanContextFromContext(ctx) |
| 34 |
fmt.Printf("Trace ID: %s\n", spanCtx.TraceID()) |
| 35 |
fmt.Printf("Span ID: %s\n", spanCtx.SpanID()) |
| 36 |
fmt.Printf("Sampled: %t\n", spanCtx.IsSampled()) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func ExampleCarrier_childProcess() { |
| 46 |
|
| 47 |
traceID := trace.TraceID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} |
| 48 |
spanID := trace.SpanID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} |
| 49 |
spanCtx := trace.NewSpanContext(trace.SpanContextConfig{ |
| 50 |
TraceID: traceID, |
| 51 |
SpanID: spanID, |
| 52 |
TraceFlags: trace.FlagsSampled, |
| 53 |
}) |
| 54 |
ctx := trace.ContextWithSpanContext(context.Background(), spanCtx) |
| 55 |
|
| 56 |
|
| 57 |
cmd := exec.CommandContext(context.Background(), "printenv", "TRACEPARENT") |
| 58 |
cmd.Env = os.Environ() |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
carrier := envcar.Carrier{ |
| 63 |
SetEnvFunc: func(key, value string) { |
| 64 |
cmd.Env = append(cmd.Env, key+"="+value) |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
prop := propagation.TraceContext{} |
| 70 |
prop.Inject(ctx, &carrier) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
out, err := cmd.Output() |
| 75 |
if err != nil { |
| 76 |
fmt.Println("error:", err) |
| 77 |
return |
| 78 |
} |
| 79 |
fmt.Print(string(out)) |
| 80 |
|
| 81 |
} |