Skip to content

Commit 6e6185e

Browse files
committed
move to otelhttptrace/test/clienttrace_test.go
1 parent 67945df commit 6e6185e

File tree

3 files changed

+17
-128
lines changed

3 files changed

+17
-128
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2121

2222
- Superfluous call to `WriteHeader` when flushing after setting a status code in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#6074)
2323
- Superfluous call to `WriteHeader` when writing the response body after setting a status code in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#6055)
24+
- Possible nil dereference panic in `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace`. (#5965)
2425

2526
<!-- Released section -->
2627
<!-- Don't change this section unless doing release -->
@@ -53,7 +54,6 @@ The next release will require at least [Go 1.22].
5354
### Fixed
5455

5556
- Race condition when reading the HTTP body and writing the response in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5916)
56-
- Possible nil dereference panic in `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace`. (#5965)
5757

5858
## [1.28.0/0.53.0/0.22.0/0.8.0/0.3.0/0.1.0] - 2024-07-02
5959

instrumentation/net/http/httptrace/otelhttptrace/clienttrace_test.go

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,9 @@ package otelhttptrace
55

66
import (
77
"context"
8-
"errors"
98
"fmt"
109
"net/http"
1110
"net/http/httptrace"
12-
"testing"
13-
14-
"github.com/stretchr/testify/assert"
15-
"github.com/stretchr/testify/require"
16-
17-
"go.opentelemetry.io/otel"
18-
"go.opentelemetry.io/otel/attribute"
19-
sdktrace "go.opentelemetry.io/otel/sdk/trace"
20-
"go.opentelemetry.io/otel/sdk/trace/tracetest"
21-
"go.opentelemetry.io/otel/trace"
2211

2312
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
2413
)
@@ -43,119 +32,3 @@ func ExampleNewClientTrace() {
4332

4433
fmt.Println(resp.Status)
4534
}
46-
47-
func Test_clientTracer_end(t *testing.T) {
48-
t.Run("end called with no parent clientTracer span", func(t *testing.T) {
49-
fixture := prepareClientTraceTest()
50-
fixture.ct.end("http.getconn", nil, HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
51-
assert.Len(t, fixture.spanRecorder.Ended(), 0)
52-
})
53-
54-
t.Run("end called with no sub spans, no root span, and no errors", func(t *testing.T) {
55-
fixture := prepareClientTraceTest()
56-
WithoutSubSpans().apply(fixture.ct)
57-
58-
ctx, span := fixture.tracer.Start(fixture.ct.Context, "client request")
59-
fixture.ct.Context = ctx
60-
61-
fixture.ct.end("http.getconn", nil, HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
62-
span.End()
63-
64-
require.Len(t, fixture.spanRecorder.Ended(), 1)
65-
recSpan := fixture.spanRecorder.Ended()[0]
66-
67-
require.Len(t, recSpan.Events(), 1)
68-
gotEvent := recSpan.Events()[0]
69-
assert.Equal(t, "http.getconn.done", gotEvent.Name)
70-
71-
assert.Equal(t,
72-
[]attribute.KeyValue{HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true)},
73-
gotEvent.Attributes,
74-
)
75-
})
76-
77-
t.Run("end called with no sub spans, root span set, and no errors", func(t *testing.T) {
78-
fixture := prepareClientTraceTest()
79-
WithoutSubSpans().apply(fixture.ct)
80-
81-
ctx, span := fixture.tracer.Start(fixture.ct.Context, "client request")
82-
fixture.ct.Context = ctx
83-
fixture.ct.root = span
84-
85-
fixture.ct.end("http.getconn", nil, HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
86-
span.End()
87-
88-
require.Len(t, fixture.spanRecorder.Ended(), 1)
89-
recSpan := fixture.spanRecorder.Ended()[0]
90-
91-
require.Len(t, recSpan.Events(), 1)
92-
gotEvent := recSpan.Events()[0]
93-
assert.Equal(t, "http.getconn.done", gotEvent.Name)
94-
95-
assert.Equal(t,
96-
[]attribute.KeyValue{
97-
HTTPConnectionReused.Bool(true),
98-
HTTPConnectionWasIdle.Bool(true),
99-
},
100-
gotEvent.Attributes,
101-
)
102-
})
103-
104-
t.Run("end called with no sub spans, root span set, and error", func(t *testing.T) {
105-
fixture := prepareClientTraceTest()
106-
WithoutSubSpans().apply(fixture.ct)
107-
108-
ctx, span := fixture.tracer.Start(fixture.ct.Context, "client request")
109-
fixture.ct.Context = ctx
110-
fixture.ct.root = span
111-
112-
fixture.ct.end("http.getconn", errors.New("testError"), HTTPConnectionReused.Bool(true), HTTPConnectionWasIdle.Bool(true))
113-
span.End()
114-
115-
require.Len(t, fixture.spanRecorder.Ended(), 1)
116-
recSpan := fixture.spanRecorder.Ended()[0]
117-
118-
require.Len(t, recSpan.Events(), 1)
119-
gotEvent := recSpan.Events()[0]
120-
assert.Equal(t, "http.getconn.done", gotEvent.Name)
121-
122-
assert.Equal(t,
123-
[]attribute.KeyValue{
124-
HTTPConnectionReused.Bool(true),
125-
HTTPConnectionWasIdle.Bool(true),
126-
attribute.Key("http.getconn.error").String("testError"),
127-
},
128-
gotEvent.Attributes,
129-
)
130-
})
131-
}
132-
133-
type clientTraceTestFixture struct {
134-
spanRecorder *tracetest.SpanRecorder
135-
tracer trace.Tracer
136-
ct *clientTracer
137-
}
138-
139-
func prepareClientTraceTest() clientTraceTestFixture {
140-
fixture := clientTraceTestFixture{}
141-
fixture.spanRecorder = tracetest.NewSpanRecorder()
142-
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(fixture.spanRecorder))
143-
otel.SetTracerProvider(provider)
144-
145-
fixture.tracer = provider.Tracer(
146-
ScopeName,
147-
trace.WithInstrumentationVersion(Version()))
148-
149-
fixture.ct = &clientTracer{
150-
Context: context.Background(),
151-
tracerProvider: otel.GetTracerProvider(),
152-
root: nil,
153-
tr: fixture.tracer,
154-
activeHooks: make(map[string]context.Context),
155-
redactedHeaders: map[string]struct{}{},
156-
addHeaders: true,
157-
useSpans: true,
158-
}
159-
160-
return fixture
161-
}

instrumentation/net/http/httptrace/otelhttptrace/test/clienttrace_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ func TestEndBeforeStartCreatesSpan(t *testing.T) {
250250
require.Len(t, spans, 1)
251251
}
252252

253+
func TestEndBeforeStartWithoutSubSpansDoesNotPanic(t *testing.T) {
254+
sr := tracetest.NewSpanRecorder()
255+
tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
256+
otel.SetTracerProvider(tp)
257+
258+
ct := otelhttptrace.NewClientTrace(context.Background(), otelhttptrace.WithoutSubSpans())
259+
260+
require.NotPanics(t, func() {
261+
ct.DNSDone(httptrace.DNSDoneInfo{})
262+
})
263+
264+
// no spans created because we were just using background context without span
265+
// and Start wasn't called which would have started a span
266+
require.Len(t, sr.Ended(), 0)
267+
}
268+
253269
type clientTraceTestFixture struct {
254270
Address string
255271
URL string

0 commit comments

Comments
 (0)