Skip to content

Commit

Permalink
otelhttptrace: handle missing getconn hook without panic (#5965)
Browse files Browse the repository at this point in the history
Fork of
#5187
updated with main branch and tests, this PR adds nil dereference check
for clientTracer.root in `end()` when span events are used instead of
sub spans

---------

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
Co-authored-by: Tonis Tiigi <tonistiigi@gmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
  • Loading branch information
3 people authored Sep 12, 2024
1 parent 7d7d4c3 commit 042f99b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- Possible nil dereference panic in `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace`. (#5965)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ func (ct *clientTracer) start(hook, spanName string, attrs ...attribute.KeyValue

func (ct *clientTracer) end(hook string, err error, attrs ...attribute.KeyValue) {
if !ct.useSpans {
// sometimes end may be called without previous start
if ct.root == nil {
ct.root = trace.SpanFromContext(ct.Context)
}
if err != nil {
attrs = append(attrs, attribute.String(hook+".error", err.Error()))
}
Expand Down
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 TestEndBeforeStartWithoutSubSpansDoesNotPanic(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 042f99b

Please sign in to comment.