Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into v2-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Feb 1, 2024
2 parents a53f613 + e707cc9 commit 64fd9f5
Show file tree
Hide file tree
Showing 12 changed files with 422 additions and 181 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"])
}
12 changes: 11 additions & 1 deletion ddtrace/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"strconv"
"strings"
"sync"

"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
Expand All @@ -24,7 +25,8 @@ import (
var _ oteltrace.Span = (*span)(nil)

type span struct {
noop.Span // https://pkg.go.dev/go.opentelemetry.io/otel/trace#hdr-API_Implementations
noop.Span // https://pkg.go.dev/go.opentelemetry.io/otel/trace#hdr-API_Implementations
mu sync.RWMutex `msg:"-"` // all fields are protected by this RWMutex
DD *tracer.Span
finished bool
attributes map[string]interface{}
Expand All @@ -37,10 +39,14 @@ type span struct {
func (s *span) TracerProvider() oteltrace.TracerProvider { return s.oteltracer.provider }

func (s *span) SetName(name string) {
s.mu.Lock()
defer s.mu.Unlock()
s.attributes[ext.SpanName] = strings.ToLower(name)
}

func (s *span) End(options ...oteltrace.SpanEndOption) {
s.mu.Lock()
defer s.mu.Unlock()
if s.finished {
return
}
Expand Down Expand Up @@ -151,6 +157,8 @@ type statusInfo struct {
// value before (OK > Error > Unset), the code will not be changed.
// The code and description are set once when the span is finished.
func (s *span) SetStatus(code otelcodes.Code, description string) {
s.mu.Lock()
defer s.mu.Unlock()
if code >= s.statusInfo.code {
s.statusInfo = statusInfo{code, description}
}
Expand All @@ -169,6 +177,8 @@ func (s *span) SetStatus(code otelcodes.Code, description string) {
// The list of reserved tags might be extended in the future.
// Any other non-reserved tags will be set as provided.
func (s *span) SetAttributes(kv ...attribute.KeyValue) {
s.mu.Lock()
defer s.mu.Unlock()
for _, kv := range kv {
if k, v := toReservedAttributes(string(kv.Key), kv.Value); k != "" {
s.attributes[k] = v
Expand Down
Loading

0 comments on commit 64fd9f5

Please sign in to comment.