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"), 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{} } diff --git a/ddtrace/tracer/tracer_test.go b/ddtrace/tracer/tracer_test.go index dd1820621e..3cb08c5e7d 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" @@ -2617,3 +2619,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) +}