Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions contrib/gin-gonic/gin/gintrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ func TestServiceName(t *testing.T) {
// TestTracerStartedMultipleTimes tests a v2 regression where the global service name was being set to an empty string
// when the tracer is started more than once.
func TestTracerStartedMultipleTimes(t *testing.T) {
// tracer.WithService below overwrites the global service name, this call
// ensures we restore it its previous value after this test finishes.
testutils.SetGlobalServiceName(t, "")

tt1 := testtracer.Start(t)
defer tt1.Stop()
tt2 := testtracer.Start(t, testtracer.WithTracerStartOpts(tracer.WithService("global_service")))
Expand Down
54 changes: 33 additions & 21 deletions contrib/log/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
)

var cfg = newConfig()

func init() {
_ = instrumentation.Load(instrumentation.PackageLogSlog)
}
Expand All @@ -31,16 +29,6 @@ type group struct {
attrs []slog.Attr
}

type config struct {
log128bits bool
}

func newConfig() *config {
return &config{
log128bits: options.GetBoolEnv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED", true),
}
}

// NewJSONHandler is a convenience function that returns a *slog.JSONHandler logger enhanced with
// tracing information.
func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
Expand All @@ -52,9 +40,18 @@ func WrapHandler(h slog.Handler) slog.Handler {
return &handler{wrapped: h}
}

type trilean uint8

const (
trileanUnknown trilean = iota
trileanTrue
trileanFalse
)

type handler struct {
wrapped slog.Handler
groups []group
log128BitsState trilean
wrapped slog.Handler
groups []group
}

// Enabled calls the wrapped handler Enabled method.
Expand All @@ -73,7 +70,7 @@ func (h *handler) Handle(ctx context.Context, rec slog.Record) error {
span, ok := tracer.SpanFromContext(ctx)
if ok && span.Context().TraceID() != tracer.TraceIDZero {
var traceID string
if cfg.log128bits {
if h.log128Bits() {
traceID = span.Context().TraceID()
} else {
traceID = strconv.FormatUint(span.Context().TraceIDLower(), 10)
Expand Down Expand Up @@ -101,8 +98,9 @@ func (h *handler) Handle(ctx context.Context, rec slog.Record) error {
func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(h.groups) == 0 {
return &handler{
wrapped: h.wrapped.WithAttrs(attrs),
groups: h.groups,
wrapped: h.wrapped.WithAttrs(attrs),
groups: h.groups,
log128BitsState: h.log128BitsState,
}
}
groups := append([]group{}, h.groups...)
Expand All @@ -111,17 +109,31 @@ func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
groups[len(groups)-1] = curGroup

return &handler{
wrapped: h.wrapped,
groups: groups,
wrapped: h.wrapped,
groups: groups,
log128BitsState: h.log128BitsState,
}
}

// WithGroup saves the provided group to be used later in the Handle method.
func (h *handler) WithGroup(name string) slog.Handler {
return &handler{
wrapped: h.wrapped,
groups: append(h.groups, group{name: name}),
wrapped: h.wrapped,
groups: append(h.groups, group{name: name}),
log128BitsState: h.log128BitsState,
}
}

// log128Bits determines the value of the log128bits field.
func (h *handler) log128Bits() bool {
if h.log128BitsState == trileanUnknown {
if options.GetBoolEnv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED", true) {
h.log128BitsState = trileanTrue
} else {
h.log128BitsState = trileanFalse
}
}
return h.log128BitsState == trileanTrue
}

// IsAlreadyWrapped checks whether the given handler is already wrapped by this package.
Expand Down
2 changes: 0 additions & 2 deletions contrib/log/slog/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ func testLogger(t *testing.T, createLogger func(b io.Writer) *slog.Logger, asser
var traceID string
spanID := strconv.FormatUint(span.Context().SpanID(), 10)
if os.Getenv("DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED") == "false" {
// Re-initialize to account for race condition between setting env var in the test and reading it in the contrib
cfg = newConfig()
traceID = strconv.FormatUint(span.Context().TraceIDLower(), 10)
} else {
traceID = span.Context().TraceID()
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci_test_contrib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ for contrib in $CONTRIBS; do
go get k8s.io/[email protected]
fi
go mod tidy
gotestsum --junitfile "${TEST_RESULTS}/gotestsum-report-$contrib_id.xml" -- ./... -v -race -coverprofile="coverage-$contrib_id.txt" -covermode=atomic
gotestsum --junitfile "${TEST_RESULTS}/gotestsum-report-$contrib_id.xml" -- ./... -v -race -coverprofile="coverage-$contrib_id.txt" -covermode=atomic --shuffle=on
test_exit=$?
[[ $test_exit -ne 0 ]] && report_error=1
cd - > /dev/null || exit 1
Expand All @@ -58,7 +58,7 @@ for mod in $INSTRUMENTATION_SUBMODULES; do
# When the issue is resolved, this line can be removed.
go get k8s.io/[email protected]
fi
gotestsum --junitfile "${TEST_RESULTS}/gotestsum-report-$mod_id.xml" -- ./... -v -race -coverprofile="coverage-$mod_id.txt" -covermode=atomic
gotestsum --junitfile "${TEST_RESULTS}/gotestsum-report-$mod_id.xml" -- ./... -v -race -coverprofile="coverage-$mod_id.txt" -covermode=atomic --shuffle=on
test_exit=$?
[[ $test_exit -ne 0 ]] && report_error=1
cd - > /dev/null || exit 1
Expand Down