|
7 | 7 | "testing" |
8 | 8 | "time" |
9 | 9 |
|
| 10 | + "go.opentelemetry.io/otel" |
10 | 11 | "go.opentelemetry.io/otel/attribute" |
| 12 | + "go.opentelemetry.io/otel/propagation" |
11 | 13 | "go.opentelemetry.io/otel/trace" |
12 | 14 | ) |
13 | 15 |
|
@@ -170,3 +172,73 @@ func TestRecordLLMResult_NilSpan(t *testing.T) { |
170 | 172 | RecordLLMResult(nil, 100*time.Millisecond, 0, nil) |
171 | 173 | RecordLLMResult(nil, 100*time.Millisecond, 0, fmt.Errorf("err")) |
172 | 174 | } |
| 175 | + |
| 176 | +// A well-formed W3C traceparent: version-traceID-spanID-flags. |
| 177 | +const validTraceParent = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" |
| 178 | +const wantExtractedTraceID = "0af7651916cd43dd8448eb211c80319c" |
| 179 | + |
| 180 | +func TestContextWithTraceParentFromEnv_Extracts(t *testing.T) { |
| 181 | + setupEnabledTelemetry(t) |
| 182 | + // Init registers TraceContext+Baggage at the global propagator; mirror |
| 183 | + // that here so Extract works without going through the full Init path. |
| 184 | + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( |
| 185 | + propagation.TraceContext{}, propagation.Baggage{})) |
| 186 | + |
| 187 | + t.Setenv("TRACEPARENT", validTraceParent) |
| 188 | + ctx := ContextWithTraceParentFromEnv(context.Background()) |
| 189 | + |
| 190 | + sc := trace.SpanContextFromContext(ctx) |
| 191 | + if !sc.IsValid() { |
| 192 | + t.Fatal("expected a valid SpanContext after extracting TRACEPARENT") |
| 193 | + } |
| 194 | + if got := sc.TraceID().String(); got != wantExtractedTraceID { |
| 195 | + t.Errorf("extracted TraceID = %q, want %q", got, wantExtractedTraceID) |
| 196 | + } |
| 197 | + |
| 198 | + // A span started on this ctx must inherit the upstream trace (child, not root). |
| 199 | + _, span := StartSpan(ctx, "test.child") |
| 200 | + defer span.End() |
| 201 | + if got := span.SpanContext().TraceID().String(); got != wantExtractedTraceID { |
| 202 | + t.Errorf("child span TraceID = %q, want %q (must inherit upstream)", got, wantExtractedTraceID) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +func TestContextWithTraceParentFromEnv_AbsentOrDisabled(t *testing.T) { |
| 207 | + // Telemetry disabled: must short-circuit and leave ctx with no span context. |
| 208 | + initialized = false |
| 209 | + shutdownFuncs = nil |
| 210 | + t.Setenv("TRACEPARENT", validTraceParent) |
| 211 | + ctx := ContextWithTraceParentFromEnv(context.Background()) |
| 212 | + if sc := trace.SpanContextFromContext(ctx); sc.IsValid() { |
| 213 | + t.Error("expected no valid SpanContext when telemetry is disabled") |
| 214 | + } |
| 215 | + |
| 216 | + // Telemetry enabled but TRACEPARENT unset: ctx unchanged (no upstream parent). |
| 217 | + setupEnabledTelemetry(t) |
| 218 | + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( |
| 219 | + propagation.TraceContext{}, propagation.Baggage{})) |
| 220 | + t.Setenv("TRACEPARENT", "") |
| 221 | + ctx = ContextWithTraceParentFromEnv(context.Background()) |
| 222 | + if sc := trace.SpanContextFromContext(ctx); sc.IsValid() { |
| 223 | + t.Error("expected no valid SpanContext when TRACEPARENT is unset") |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +func TestContextWithTraceParentFromEnv_Malformed(t *testing.T) { |
| 228 | + setupEnabledTelemetry(t) |
| 229 | + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( |
| 230 | + propagation.TraceContext{}, propagation.Baggage{})) |
| 231 | + |
| 232 | + for _, bad := range []string{ |
| 233 | + "not-a-traceparent", |
| 234 | + "00-invalidtraceid-invalidspanid-01", |
| 235 | + } { |
| 236 | + t.Run(bad, func(t *testing.T) { |
| 237 | + t.Setenv("TRACEPARENT", bad) |
| 238 | + ctx := ContextWithTraceParentFromEnv(context.Background()) |
| 239 | + if sc := trace.SpanContextFromContext(ctx); sc.IsValid() { |
| 240 | + t.Errorf("expected no valid SpanContext for malformed TRACEPARENT %q", bad) |
| 241 | + } |
| 242 | + }) |
| 243 | + } |
| 244 | +} |
0 commit comments