Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracer: Log a warning on NoopTracer.StartSpan() #2991

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions contrib/internal/httptrace/httptrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 2 additions & 0 deletions ddtrace/internal/globaltracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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{}
}

Expand Down
28 changes: 28 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
package tracer

import (
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
llog "log"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -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)
}
Loading