Skip to content

Commit

Permalink
feat(test): improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Jul 5, 2023
1 parent 7a1b3d9 commit f45b85a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions foxtrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit f45b85a

Please sign in to comment.