Skip to content

Commit

Permalink
add tests for clientTracer.end
Browse files Browse the repository at this point in the history
  • Loading branch information
krantideep95 committed Jul 31, 2024
1 parent 43eb415 commit 24012f4
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions instrumentation/net/http/httptrace/otelhttptrace/clienttrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ package otelhttptrace

import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptrace"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
Expand All @@ -32,3 +43,144 @@ func ExampleNewClientTrace() {

fmt.Println(resp.Status)
}

func Test_clientTracer_end(t *testing.T) {
t.Run("end called with no parent clientTracer span", func(t *testing.T) {
fixture := prepareClientTraceTest()
fixture.ct.end("http.getconn", nil, HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
assert.Len(t, fixture.spanRecorder.Ended(), 0)
})

t.Run("end called with no sub spans, no root span, and no errors", func(t *testing.T) {
fixture := prepareClientTraceTest()
WithoutSubSpans().apply(fixture.ct)

ctx, span := fixture.tracer.Start(fixture.ct.Context, "client request")
fixture.ct.Context = ctx

fixture.ct.end("http.getconn", nil, HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
span.End()

require.Len(t, fixture.spanRecorder.Ended(), 1)
recSpan := fixture.spanRecorder.Ended()[0]

require.Len(t, recSpan.Events(), 1)
gotEvent := recSpan.Events()[0]
assert.Equal(t, "http.getconn.done", gotEvent.Name)

assert.Equal(t,
[]attribute.KeyValue{HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true)},
gotEvent.Attributes,
)
})

t.Run("end called with no sub spans, root span set, and no errors", func(t *testing.T) {
fixture := prepareClientTraceTest()
WithoutSubSpans().apply(fixture.ct)

ctx, span := fixture.tracer.Start(fixture.ct.Context, "client request")
fixture.ct.Context = ctx

fixture.ct.end("http.getconn", nil, HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
span.End()

require.Len(t, fixture.spanRecorder.Ended(), 1)
recSpan := fixture.spanRecorder.Ended()[0]

require.Len(t, recSpan.Events(), 1)
gotEvent := recSpan.Events()[0]
assert.Equal(t, "http.getconn.done", gotEvent.Name)

assert.Equal(t,
[]attribute.KeyValue{
HTTPConnectionReused.Bool(true),
HTTPConnectionWasIdle.Bool(true),
},
gotEvent.Attributes,
)
})

t.Run("end called with no sub spans, root span set, and error", func(t *testing.T) {
fixture := prepareClientTraceTest()
WithoutSubSpans().apply(fixture.ct)

ctx, span := fixture.tracer.Start(fixture.ct.Context, "client request")
fixture.ct.Context = ctx

fixture.ct.end("http.getconn", errors.New("testError"), HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
span.End()

require.Len(t, fixture.spanRecorder.Ended(), 1)
recSpan := fixture.spanRecorder.Ended()[0]

require.Len(t, recSpan.Events(), 1)
gotEvent := recSpan.Events()[0]
assert.Equal(t, "http.getconn.done", gotEvent.Name)

assert.Equal(t,
[]attribute.KeyValue{
HTTPConnectionReused.Bool(true),
HTTPConnectionWasIdle.Bool(true),
attribute.Key("http.getconn.error").String("testError"),
},
gotEvent.Attributes,
)
})

t.Run("end called with no sub spans, root span set, and errors", func(t *testing.T) {
fixture := prepareClientTraceTest()
WithoutSubSpans().apply(fixture.ct)

ctx, span := fixture.tracer.Start(fixture.ct.Context, "client request")
fixture.ct.Context = ctx

fixture.ct.end("http.getconn", errors.New("testError"), HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
span.End()

require.Len(t, fixture.spanRecorder.Ended(), 1)
recSpan := fixture.spanRecorder.Ended()[0]

require.Len(t, recSpan.Events(), 1)
gotEvent := recSpan.Events()[0]
assert.Equal(t, "http.getconn.done", gotEvent.Name)

assert.Equal(t,
[]attribute.KeyValue{
HTTPConnectionReused.Bool(true),
HTTPConnectionWasIdle.Bool(true),
attribute.Key("http.getconn.error").String("testError"),
},
gotEvent.Attributes,
)
})
}

type clientTraceTestFixture struct {
spanRecorder *tracetest.SpanRecorder
tracer trace.Tracer
ct *clientTracer
}

func prepareClientTraceTest() clientTraceTestFixture {
fixture := clientTraceTestFixture{}
fixture.spanRecorder = tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(fixture.spanRecorder))
otel.SetTracerProvider(provider)

fixture.tracer = provider.Tracer(
ScopeName,
trace.WithInstrumentationVersion(Version()))

fixture.ct = &clientTracer{
Context: context.Background(),
tracerProvider: otel.GetTracerProvider(),
root: nil,
tr: fixture.tracer,
activeHooks: make(map[string]context.Context),
redactedHeaders: map[string]struct{}{},
addHeaders: true,
useSpans: true,
}

return fixture
}

0 comments on commit 24012f4

Please sign in to comment.