Skip to content

Commit

Permalink
Fix panic in otelhttptrace
Browse files Browse the repository at this point in the history
Wait100Continue trace event is sent here before "http.receive" phase which results with a panic
  • Loading branch information
Alkorin committed May 29, 2023
1 parent 75d0c13 commit 31bdeb2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (ct *clientTracer) got100Continue() {
func (ct *clientTracer) wait100Continue() {
span := ct.root
if ct.useSpans {
span = ct.span("http.receive")
span = ct.span("http.send")
}
span.AddEvent("GOT 100 - Wait")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package test

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -504,3 +505,35 @@ func TestHTTPRequestWithTraceContext(t *testing.T) {
require.Equal(t, parent.SpanContext().TraceID(), getconn.SpanContext().TraceID())
require.Equal(t, parent.SpanContext().SpanID(), getconn.Parent().SpanID())
}

func TestHTTPRequestWithExpect100Continue(t *testing.T) {
fixture := prepareClientTraceTest(t)

ctx, span := otel.Tracer("oteltest").Start(context.Background(), "root")
ctx = httptrace.WithClientTrace(ctx, otelhttptrace.NewClientTrace(ctx))

req, err := http.NewRequestWithContext(ctx, http.MethodPost, fixture.URL, bytes.NewReader([]byte("test")))
require.NoError(t, err)

// Set Expect: 100-continue
req.Header.Set("Expect", "100-continue")
resp, err := fixture.Client.Do(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
span.End()

require.Len(t, fixture.SpanRecorder.Ended(), 5)

// http.send should contains "GOT 100 - Wait" event
send, ok := getSpanFromRecorder(fixture.SpanRecorder, "http.send")
require.True(t, ok)

found := false
for _, v := range send.Events() {
if v.Name == "GOT 100 - Wait" {
found = true
break
}
}
require.True(t, found)
}

0 comments on commit 31bdeb2

Please sign in to comment.