forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry_test.go
More file actions
60 lines (49 loc) · 1.68 KB
/
Copy pathtelemetry_test.go
File metadata and controls
60 lines (49 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright IBM Corp. 2014, 2026
// SPDX-License-Identifier: BUSL-1.1
package main
import (
"context"
"testing"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
func TestExtractParentTraceContext(t *testing.T) {
// Install the same propagator that openTelemetryInit configures when
// telemetry export is enabled, so extraction has something to work with.
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{}, propagation.Baggage{},
))
t.Cleanup(func() { otel.SetTextMapPropagator(nil) })
const (
traceID = "0af7651916cd43dd8448eb211c80319c"
spanID = "b7ad6b7169203331"
)
t.Run("adopts the parent from TRACEPARENT", func(t *testing.T) {
t.Setenv("TRACEPARENT", "00-"+traceID+"-"+spanID+"-01")
ctx := extractParentTraceContext(context.Background())
sc := trace.SpanContextFromContext(ctx)
if !sc.IsValid() {
t.Fatal("expected a valid span context, got an invalid one")
}
if got := sc.TraceID().String(); got != traceID {
t.Errorf("trace id = %s, want %s", got, traceID)
}
if got := sc.SpanID().String(); got != spanID {
t.Errorf("span id = %s, want %s", got, spanID)
}
if !sc.IsRemote() {
t.Error("expected the extracted span context to be marked remote")
}
})
t.Run("returns the context unchanged when no trace context is present", func(t *testing.T) {
// Treat empty values as unset.
t.Setenv("TRACEPARENT", "")
t.Setenv("TRACESTATE", "")
t.Setenv("BAGGAGE", "")
ctx := extractParentTraceContext(context.Background())
if trace.SpanContextFromContext(ctx).IsValid() {
t.Error("expected no span context when the environment is empty")
}
})
}