Skip to content

Commit

Permalink
tracer: enable 128-bit TraceID generation by default (#2335)
Browse files Browse the repository at this point in the history
Co-authored-by: Dario Castañé <dario.castane@datadoghq.com>
  • Loading branch information
katiehockman and darccio committed Nov 10, 2023
1 parent 9d39644 commit 491dab0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
7 changes: 3 additions & 4 deletions ddtrace/opentelemetry/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/internal"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
Expand Down Expand Up @@ -79,10 +80,8 @@ func TestSpanWithoutNewRoot(t *testing.T) {

parent, ddCtx := tracer.StartSpanFromContext(context.Background(), "otel.child")
_, child := tr.Start(ddCtx, "otel.child")
var parentBytes oteltrace.TraceID
// TraceID is big-endian so the LOW order bits are at the END of parentBytes
uint64ToByte(parent.Context().TraceID(), parentBytes[8:])
assert.Equal(parentBytes, child.SpanContext().TraceID())
parentCtxW3C := parent.Context().(ddtrace.SpanContextW3C)
assert.Equal(parentCtxW3C.TraceID128Bytes(), [16]byte(child.SpanContext().TraceID()))
}

func TestTracerOptions(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion ddtrace/tracer/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"os"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
Expand Down Expand Up @@ -115,6 +116,7 @@ func Test128(t *testing.T) {
_, _, _, stop := startTestTracer(t)
defer stop()

os.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "false")
span, _ := StartSpanFromContext(context.Background(), "http.request")
assert.NotZero(t, span.Context().TraceID())
w3cCtx, ok := span.Context().(ddtrace.SpanContextW3C)
Expand All @@ -129,7 +131,7 @@ func Test128(t *testing.T) {
assert.Equal(t, span.Context().TraceID(), binary.BigEndian.Uint64(idBytes[8:]))

// Enable 128 bit trace ids
t.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "true")
os.Unsetenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED")
span128, _ := StartSpanFromContext(context.Background(), "http.request")
assert.NotZero(t, span128.Context().TraceID())
w3cCtx, ok = span128.Context().(ddtrace.SpanContextW3C)
Expand Down
10 changes: 6 additions & 4 deletions ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ func TestSpanError(t *testing.T) {
span.SetTag(ext.Error, err)
assert.Equal(int32(0), span.Error)

// '+2' is `_dd.p.dm` + `_dd.base_service`
// '+3' is `_dd.p.dm` + `_dd.base_service`, `_dd.p.tid`
t.Logf("%q\n", span.Meta)
assert.Equal(nMeta+2, len(span.Meta))
assert.Equal(nMeta+3, len(span.Meta))
assert.Equal("", span.Meta[ext.ErrorMsg])
assert.Equal("", span.Meta[ext.ErrorType])
assert.Equal("", span.Meta[ext.ErrorStack])
Expand Down Expand Up @@ -761,7 +761,7 @@ func TestSpanLog(t *testing.T) {
t.Run("128-bit-generation-only", func(t *testing.T) {
// Generate 128 bit trace ids, but don't log them. So only the lower
// 64 bits should be logged in decimal form.
t.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "true")
// DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED is true by default
// DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED is false by default
assert := assert.New(t)
tracer, _, _, stop := startTestTracer(t, WithService("tracer.test"), WithEnv("testenv"))
Expand All @@ -777,8 +777,8 @@ func TestSpanLog(t *testing.T) {
t.Run("128-bit-logging-only", func(t *testing.T) {
// Logging 128-bit trace ids is enabled, but it's not present in
// the span. So only the lower 64 bits should be logged in decimal form.
t.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "false")
t.Setenv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED", "true")
// DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED is false by default
assert := assert.New(t)
tracer, _, _, stop := startTestTracer(t, WithService("tracer.test"), WithEnv("testenv"))
defer stop()
Expand Down Expand Up @@ -811,6 +811,7 @@ func TestSpanLog(t *testing.T) {
// Logging 128-bit trace ids is enabled, and a 128-bit trace id, so
// a quoted 32 byte hex string should be printed for the dd.trace_id.
t.Setenv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED", "true")
t.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "false")
assert := assert.New(t)
tracer, _, _, stop := startTestTracer(t, WithService("tracer.test"), WithEnv("testenv"))
defer stop()
Expand All @@ -826,6 +827,7 @@ func TestSpanLog(t *testing.T) {
// Logging 128-bit trace ids is enabled, and but the upper 64 bits
// are empty, so the dd.trace_id should be printed as raw digits (not hex).
t.Setenv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED", "true")
t.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "false")
assert := assert.New(t)
tracer, _, _, stop := startTestTracer(t, WithService("tracer.test"), WithEnv("testenv"))
defer stop()
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func newSpanContext(span *span, parent *spanContext) *spanContext {
context.setBaggageItem(k, v)
return true
})
} else if sharedinternal.BoolEnv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", false) {
} else if sharedinternal.BoolEnv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", true) {
// add 128 bit trace id, if enabled, formatted as big-endian:
// <32-bit unix seconds> <32 bits of zero> <64 random bits>
id128 := time.Duration(span.Start) / time.Second
Expand Down
16 changes: 12 additions & 4 deletions ddtrace/tracer/textmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"errors"
"fmt"
"net/http"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/internal"
"gopkg.in/DataDog/dd-trace-go.v1/internal/httpmem"
Expand Down Expand Up @@ -359,6 +361,9 @@ func TestTextMapPropagator(t *testing.T) {
assert.Equal(t, strconv.Itoa(int(childSpanID)), dst["x-datadog-parent-id"])
assert.Equal(t, strconv.Itoa(int(childSpanID)), dst["x-datadog-trace-id"])
assert.Equal(t, "1", dst["x-datadog-sampling-priority"])
if tc.xDatadogTagsHeader != "" {
tc.xDatadogTagsHeader += fmt.Sprintf(",_dd.p.tid=%s", child.Context().(ddtrace.SpanContextW3C).TraceID128()[:16])
}
assertTraceTags(t, tc.xDatadogTagsHeader, dst["x-datadog-tags"])
if strings.Contains(tc.injectStyle, "tracecontext") {
// other unit tests check the value of these W3C headers, so just make sure they're present
Expand Down Expand Up @@ -401,12 +406,15 @@ func TestTextMapPropagator(t *testing.T) {
})

t.Run("InjectExtract", func(t *testing.T) {
os.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "true")
defer os.Unsetenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED")
t.Setenv(headerPropagationStyleExtract, "datadog")
t.Setenv(headerPropagationStyleInject, "datadog")
propagator := NewPropagator(&PropagatorConfig{
BaggagePrefix: "bg-",
TraceHeader: "tid",
ParentHeader: "pid",
BaggagePrefix: "bg-",
TraceHeader: "tid",
ParentHeader: "pid",
MaxTagsHeaderLen: defaultMaxTagsHeaderLen,
})
tracer := newTracer(WithPropagator(propagator))
defer tracer.Stop()
Expand All @@ -425,7 +433,7 @@ func TestTextMapPropagator(t *testing.T) {

xctx, ok := sctx.(*spanContext)
assert.True(ok)
assert.Equal(xctx.traceID, ctx.traceID)
assert.Equal(xctx.traceID.HexEncoded(), ctx.traceID.HexEncoded())
assert.Equal(xctx.spanID, ctx.spanID)
assert.Equal(xctx.baggage, ctx.baggage)
assert.Equal(xctx.trace.priority, ctx.trace.priority)
Expand Down
10 changes: 6 additions & 4 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,8 @@ func TestTracerStartSpanOptions128(t *testing.T) {
defer internal.SetGlobalTracer(&internal.NoopTracer{})
t.Run("64-bit-trace-id", func(t *testing.T) {
assert := assert.New(t)
os.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "false")
defer os.Unsetenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED")
opts := []StartSpanOption{
WithSpanID(987654),
}
Expand All @@ -730,8 +732,7 @@ func TestTracerStartSpanOptions128(t *testing.T) {
})
t.Run("128-bit-trace-id", func(t *testing.T) {
assert := assert.New(t)
// Enable 128 bit trace ids
t.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "true")
// 128-bit trace ids are enabled by default.
opts128 := []StartSpanOption{
WithSpanID(987654),
StartTime(time.Unix(123456, 0)),
Expand Down Expand Up @@ -1044,8 +1045,9 @@ func TestNewSpanChild(t *testing.T) {

func testNewSpanChild(t *testing.T, is128 bool) {
t.Run(fmt.Sprintf("TestNewChildSpan(is128=%t)", is128), func(*testing.T) {
if is128 {
t.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "true")
if !is128 {
os.Setenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED", "false")
defer os.Unsetenv("DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED")
}
assert := assert.New(t)

Expand Down

0 comments on commit 491dab0

Please sign in to comment.