Skip to content

Commit

Permalink
move to otelhttptrace/test/clienttrace_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
krantideep95 committed Sep 10, 2024
1 parent 67945df commit 1ee5129
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 127 deletions.
127 changes: 0 additions & 127 deletions instrumentation/net/http/httptrace/otelhttptrace/clienttrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,9 @@ 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 @@ -43,119 +32,3 @@ 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.root = span

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.root = span

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
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ func TestEndBeforeStartCreatesSpan(t *testing.T) {
require.Len(t, spans, 1)
}

func TestEndBeforeStartWithSubSpansDoesNotPanic(t *testing.T) {
sr := tracetest.NewSpanRecorder()
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
otel.SetTracerProvider(tp)

ct := otelhttptrace.NewClientTrace(context.Background(), otelhttptrace.WithoutSubSpans())

require.NotPanics(t, func() {
ct.DNSDone(httptrace.DNSDoneInfo{})
})

// no spans created because we were just using background context without span
// and Start wasn't called which would have started a span
require.Len(t, sr.Ended(), 0)
}

type clientTraceTestFixture struct {
Address string
URL string
Expand Down

0 comments on commit 1ee5129

Please sign in to comment.