From adf7f317f682bb9c38eb5904a2528becf8aa9da0 Mon Sep 17 00:00:00 2001 From: Mikayla Toffler Date: Mon, 25 Nov 2024 11:37:32 -0500 Subject: [PATCH 1/3] Add log warning --- ddtrace/internal/globaltracer.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ddtrace/internal/globaltracer.go b/ddtrace/internal/globaltracer.go index 363d1f9983..d02f1167ef 100644 --- a/ddtrace/internal/globaltracer.go +++ b/ddtrace/internal/globaltracer.go @@ -9,6 +9,7 @@ import ( "sync/atomic" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace" + "gopkg.in/DataDog/dd-trace-go.v1/internal/log" ) var ( @@ -46,6 +47,7 @@ type NoopTracer struct{} // StartSpan implements ddtrace.Tracer. func (NoopTracer) StartSpan(_ string, _ ...ddtrace.StartSpanOption) ddtrace.Span { + log.Warn("Tracer must be started before starting a span; Review the docs for more information: https://docs.datadoghq.com/tracing/trace_collection/library_config/go/") return NoopSpan{} } From 18b1ce1355bba5d777634142564bed536a157463 Mon Sep 17 00:00:00 2001 From: Mikayla Toffler Date: Tue, 26 Nov 2024 13:07:25 -0500 Subject: [PATCH 2/3] Added test --- ddtrace/tracer/tracer_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ddtrace/tracer/tracer_test.go b/ddtrace/tracer/tracer_test.go index 038107843e..9ab46d33ef 100644 --- a/ddtrace/tracer/tracer_test.go +++ b/ddtrace/tracer/tracer_test.go @@ -6,6 +6,7 @@ package tracer import ( + "bytes" "context" "encoding/base64" "encoding/binary" @@ -13,6 +14,7 @@ import ( "errors" "fmt" "io" + llog "log" "net/http" "net/http/httptest" "os" @@ -2616,3 +2618,29 @@ func TestExecutionTraceSpanTagged(t *testing.T) { assert.Equal(t, partialSpan.Meta["go_execution_traced"], "partial") assert.NotContains(t, untracedSpan.Meta, "go_execution_traced") } + +func TestNoopTracerStartSpan(t *testing.T) { + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("Failed to create pipe: %v", err) + } + + undo := log.UseLogger(customLogger{l: llog.New(w, "", llog.LstdFlags)}) + defer undo() + + StartSpan("abcd") + + w.Close() + var buf bytes.Buffer + buf.ReadFrom(r) + + log := buf.String() + expected := "Tracer must be started before starting a span" + assert.Contains(t, log, expected) +} + +type customLogger struct{ l *llog.Logger } + +func (c customLogger) Log(msg string) { + c.l.Print(msg) +} From e6fef16be40831bd16a0d1cfc156e3056bbf27a5 Mon Sep 17 00:00:00 2001 From: Mikayla Toffler Date: Mon, 16 Dec 2024 20:19:01 -0500 Subject: [PATCH 3/3] fix BenchmarkStartRequestSpan: call tracer.Start --- contrib/internal/httptrace/httptrace_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contrib/internal/httptrace/httptrace_test.go b/contrib/internal/httptrace/httptrace_test.go index 22de28c62d..7adc59b52c 100644 --- a/contrib/internal/httptrace/httptrace_test.go +++ b/contrib/internal/httptrace/httptrace_test.go @@ -358,6 +358,9 @@ func BenchmarkStartRequestSpan(b *testing.B) { b.Errorf("Failed to create request: %v", err) return } + + mt := mocktracer.Start() + defer mt.Stop() opts := []ddtrace.StartSpanOption{ tracer.ServiceName("SomeService"), tracer.ResourceName("SomeResource"),