|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "go.opentelemetry.io/otel" |
| 11 | + "go.opentelemetry.io/otel/propagation" |
| 12 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 13 | + "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 14 | + "go.opentelemetry.io/otel/trace" |
| 15 | +) |
| 16 | + |
| 17 | +func TestExtractTraceContext_WithParentTrace(t *testing.T) { |
| 18 | + sr := tracetest.NewSpanRecorder() |
| 19 | + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 20 | + t.Cleanup(func() { _ = tp.Shutdown(context.Background()) }) |
| 21 | + otel.SetTracerProvider(tp) |
| 22 | + otel.SetTextMapPropagator(propagation.TraceContext{}) |
| 23 | + |
| 24 | + tracer := tp.Tracer("test") |
| 25 | + |
| 26 | + // create a parent span to get a valid trace context |
| 27 | + parentCtx, parentSpan := tracer.Start(context.Background(), "parent") |
| 28 | + parentSpanCtx := parentSpan.SpanContext() |
| 29 | + parentSpan.End() |
| 30 | + |
| 31 | + // inject the parent context into headers |
| 32 | + headers := make(http.Header) |
| 33 | + otel.GetTextMapPropagator().Inject(parentCtx, propagation.HeaderCarrier(headers)) |
| 34 | + |
| 35 | + var extractedSpanCtx trace.SpanContext |
| 36 | + handler := ExtractTraceContext(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | + // create a child span using the extracted context |
| 38 | + _, childSpan := tracer.Start(r.Context(), "child") |
| 39 | + extractedSpanCtx = childSpan.SpanContext() |
| 40 | + childSpan.End() |
| 41 | + w.WriteHeader(http.StatusOK) |
| 42 | + })) |
| 43 | + |
| 44 | + req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 45 | + req.Header = headers |
| 46 | + rec := httptest.NewRecorder() |
| 47 | + |
| 48 | + handler.ServeHTTP(rec, req) |
| 49 | + |
| 50 | + require.Equal(t, http.StatusOK, rec.Code) |
| 51 | + require.True(t, extractedSpanCtx.IsValid(), "child span should be valid") |
| 52 | + require.Equal(t, parentSpanCtx.TraceID(), extractedSpanCtx.TraceID(), "child should have same trace ID as parent") |
| 53 | +} |
| 54 | + |
| 55 | +func TestExtractTraceContext_WithoutParentTrace(t *testing.T) { |
| 56 | + sr := tracetest.NewSpanRecorder() |
| 57 | + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 58 | + t.Cleanup(func() { _ = tp.Shutdown(context.Background()) }) |
| 59 | + otel.SetTracerProvider(tp) |
| 60 | + otel.SetTextMapPropagator(propagation.TraceContext{}) |
| 61 | + |
| 62 | + tracer := tp.Tracer("test") |
| 63 | + |
| 64 | + var extractedSpanCtx trace.SpanContext |
| 65 | + handler := ExtractTraceContext(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 66 | + // create a span - should be a root span since no parent headers |
| 67 | + _, span := tracer.Start(r.Context(), "root") |
| 68 | + extractedSpanCtx = span.SpanContext() |
| 69 | + span.End() |
| 70 | + w.WriteHeader(http.StatusOK) |
| 71 | + })) |
| 72 | + |
| 73 | + req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 74 | + // no trace headers |
| 75 | + rec := httptest.NewRecorder() |
| 76 | + |
| 77 | + handler.ServeHTTP(rec, req) |
| 78 | + |
| 79 | + require.Equal(t, http.StatusOK, rec.Code) |
| 80 | + require.True(t, extractedSpanCtx.IsValid(), "span should be valid") |
| 81 | + |
| 82 | + // verify it's a root span by checking no parent exists in the recorded spans |
| 83 | + spans := sr.Ended() |
| 84 | + require.Len(t, spans, 1) |
| 85 | + require.False(t, spans[0].Parent().IsValid(), "span should be a root span with no parent") |
| 86 | +} |
0 commit comments