Skip to content

Commit

Permalink
ddtrace/opentelemetry: refactor OTel API tests (#2503)
Browse files Browse the repository at this point in the history
Co-authored-by: Katie Hockman <[email protected]>
Co-authored-by: Dario Castañé <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2024
1 parent c527ccc commit 8c4834f
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 139 deletions.
20 changes: 9 additions & 11 deletions ddtrace/opentelemetry/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -23,6 +22,7 @@ import (
)

func TestHttpDistributedTrace(t *testing.T) {
assert := assert.New(t)
tp, payloads, cleanup := mockTracerProvider(t)
defer cleanup()
otel.SetTracerProvider(tp)
Expand All @@ -33,11 +33,10 @@ func TestHttpDistributedTrace(t *testing.T) {

w := otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedSpan := oteltrace.SpanFromContext(r.Context())
assert.Equal(t, rootSpan.SpanContext().TraceID(), receivedSpan.SpanContext().TraceID())
assert.Equal(rootSpan.SpanContext().TraceID(), receivedSpan.SpanContext().TraceID())
}), "testOperation")
testServer := httptest.NewServer(w)
defer testServer.Close()

c := http.Client{Transport: otelhttp.NewTransport(nil)}
req, err := http.NewRequestWithContext(sctx, http.MethodGet, testServer.URL, nil)
require.NoError(t, err)
Expand All @@ -47,12 +46,11 @@ func TestHttpDistributedTrace(t *testing.T) {
rootSpan.End()

p := <-payloads
numSpans := strings.Count(p, "\"span_id\"")
assert.Equal(t, 3, numSpans)
assert.Contains(t, p, `"name":"internal"`)
assert.Contains(t, p, `"name":"server.request`)
assert.Contains(t, p, `"name":"client.request"`)
assert.Contains(t, p, `"resource":"testRootSpan"`)
assert.Contains(t, p, `"resource":"testOperation"`)
assert.Contains(t, p, `"resource":"HTTP GET"`)
assert.Len(p, 2)
assert.Equal("server.request", p[0][0]["name"])
assert.Equal("internal", p[1][0]["name"])
assert.Equal("client.request", p[1][1]["name"])
assert.Equal("testOperation", p[0][0]["resource"])
assert.Equal("testRootSpan", p[1][0]["resource"])
assert.Equal("HTTP GET", p[1][1]["resource"])
}
Loading

0 comments on commit 8c4834f

Please sign in to comment.