From f45b85a424b30aa336318d13d53461e216a6ac7a Mon Sep 17 00:00:00 2001 From: tigerwill90 Date: Wed, 5 Jul 2023 12:36:47 +0200 Subject: [PATCH] feat(test): improve test coverage --- foxtrace_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/foxtrace_test.go b/foxtrace_test.go index 83c2ea6..37b83ed 100644 --- a/foxtrace_test.go +++ b/foxtrace_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" "github.com/tigerwill90/fox" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/trace" "net/http" @@ -86,3 +87,35 @@ func TestPropagationWithCustomPropagators(t *testing.T) { require.NoError(t, err) router.ServeHTTP(w, r) } + +func TestWithSpanAttributes(t *testing.T) { + provider := trace.NewNoopTracerProvider() + otel.SetTextMapPropagator(b3prop.New()) + + r := httptest.NewRequest("GET", "/user/123?foo=bar", nil) + w := httptest.NewRecorder() + + ctx := context.Background() + sc := trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: trace.TraceID{0x01}, + SpanID: trace.SpanID{0x01}, + }) + ctx = trace.ContextWithRemoteSpanContext(ctx, sc) + ctx, _ = provider.Tracer(tracerName).Start(ctx, "test") + otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header)) + + router := fox.New() + mw := New("foobar", WithTracerProvider(provider), WithSpanAttributes(func(r *http.Request) []attribute.KeyValue { + attrs := make([]attribute.KeyValue, 1) + attrs[0] = attribute.String("http.target", r.URL.String()) + return attrs + })) + err := router.Handle(http.MethodGet, "/user/{id}", mw.Trace(func(c fox.Context) { + span := trace.SpanFromContext(c.Request().Context()) + assert.Equal(t, sc.TraceID(), span.SpanContext().TraceID()) + assert.Equal(t, sc.SpanID(), span.SpanContext().SpanID()) + })) + + require.NoError(t, err) + router.ServeHTTP(w, r) +}