Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Introduce trace.Config
Browse files Browse the repository at this point in the history
trace.Config allows to set global configuration for tracing
and deprecating the global default sampler setter.

Fixes #652.
  • Loading branch information
rakyll committed Mar 28, 2018
1 parent 232014e commit d5d6a6c
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/http/helloworld_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
trace.RegisterExporter(exporter)

// Always trace for this demo.
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

// Report stats at every second.
view.SetReportingPeriod(1 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion examples/http/helloworld_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
trace.RegisterExporter(exporter)

// Always trace for this demo.
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

// Report stats at every second.
view.SetReportingPeriod(1 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion exporter/jaeger/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
trace.RegisterExporter(exporter)

// For demoing purposes, always sample.
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

ctx, span := trace.StartSpan(ctx, "/foo")
bar(ctx)
Expand Down
4 changes: 2 additions & 2 deletions exporter/stackdriver/stackdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestExport(t *testing.T) {
view.RegisterExporter(exporter)
defer view.UnregisterExporter(exporter)

trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

span := trace.NewSpan("custom-span", nil, trace.StartOptions{})
time.Sleep(10 * time.Millisecond)
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestGRPC(t *testing.T) {
view.RegisterExporter(exporter)
defer view.UnregisterExporter(exporter)

trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

client, done := testpb.NewTestClient(t)
defer done()
Expand Down
2 changes: 1 addition & 1 deletion exporter/stackdriver/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBundling(t *testing.T) {
}
trace.RegisterExporter(exporter)

trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
for i := 0; i < 35; i++ {
_, span := trace.StartSpan(context.Background(), "span")
span.End()
Expand Down
2 changes: 1 addition & 1 deletion exporter/zipkin/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
trace.RegisterExporter(exporter)

// For example purposes, sample every trace.
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

ctx := context.Background()
foo(ctx)
Expand Down
10 changes: 5 additions & 5 deletions plugin/ocgrpc/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (t *testExporter) ExportSpan(s *trace.SpanData) {
}

func TestStreaming(t *testing.T) {
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
te := testExporter{make(chan *trace.SpanData)}
trace.RegisterExporter(&te)
defer trace.UnregisterExporter(&te)
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestStreaming(t *testing.T) {
}

func TestStreamingFail(t *testing.T) {
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
te := testExporter{make(chan *trace.SpanData)}
trace.RegisterExporter(&te)
defer trace.UnregisterExporter(&te)
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestStreamingFail(t *testing.T) {
}

func TestSingle(t *testing.T) {
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
te := testExporter{make(chan *trace.SpanData)}
trace.RegisterExporter(&te)
defer trace.UnregisterExporter(&te)
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestServerSpanDuration(t *testing.T) {
trace.RegisterExporter(&te)
defer trace.UnregisterExporter(&te)

trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

ctx := context.Background()
const sleep = 100 * time.Millisecond
Expand All @@ -174,7 +174,7 @@ loop:
}

func TestSingleFail(t *testing.T) {
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
te := testExporter{make(chan *trace.SpanData)}
trace.RegisterExporter(&te)
defer trace.UnregisterExporter(&te)
Expand Down
2 changes: 1 addition & 1 deletion plugin/ochttp/propagation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestRoundTripAllFormats(t *testing.T) {
}

ctx := context.Background()
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
ctx, span := trace.StartSpan(ctx, "test")
sc := span.SpanContext()
wantStr := fmt.Sprintf("trace_id=%x, span_id=%x, options=%d", sc.TraceID, sc.SpanID, sc.TraceOptions)
Expand Down
2 changes: 1 addition & 1 deletion plugin/ochttp/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (c *collector) ExportSpan(s *trace.SpanData) {
}

func TestEndToEnd(t *testing.T) {
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

tc := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions trace/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ func BenchmarkSpanID_DotString(b *testing.B) {
func traceBenchmark(b *testing.B, fn func(*testing.B)) {
b.Run("AlwaysSample", func(b *testing.B) {
b.ReportAllocs()
SetDefaultSampler(AlwaysSample())
SetConfig(Config{DefaultSampler: AlwaysSample()})
fn(b)
})
b.Run("NeverSample", func(b *testing.B) {
b.ReportAllocs()
SetDefaultSampler(NeverSample())
SetConfig(Config{DefaultSampler: NeverSample()})
fn(b)
})
}
4 changes: 2 additions & 2 deletions trace/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ one of the provided exporters or write your own.
trace.RegisterExporter(anExporter)
By default, traces will be sampled relatively rarely. To change the sampling
frequency for your entire program, call SetDefaultSampler. Use a ProbabilitySampler
frequency for your entire program, call SetConfig. Use a ProbabilitySampler
to sample a subset of traces, or use AlwaysSample to collect a trace on every run:
trace.SetDefaultSampler(trace.AlwaysSample())
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
Adding Spans to a Trace
Expand Down
12 changes: 2 additions & 10 deletions trace/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,14 @@ import (

const defaultSamplingProbability = 1e-4

func init() {
defaultSampler = ProbabilitySampler(defaultSamplingProbability)
}

func newDefaultSampler() Sampler {
return ProbabilitySampler(defaultSamplingProbability)
}

// SetDefaultSampler sets the default sampler used when creating new spans.
//
// Deprecated: Use SetConfig.
func SetDefaultSampler(sampler Sampler) {
if sampler == nil {
sampler = newDefaultSampler()
}
mu.Lock()
defaultSampler = sampler
mu.Unlock()
}

// Sampler decides whether a trace should be sampled and exported.
Expand Down
17 changes: 9 additions & 8 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type StartOptions struct {
// If not provided, then the behavior differs based on whether
// the parent of this Span is remote, local, or there is no parent.
// In the case of a remote parent or no parent, the
// default sampler (see SetDefaultSampler) will be consulted. Otherwise,
// default sampler (see Config) will be consulted. Otherwise,
// when there is a non-remote parent, no new sampling decision will be made:
// we will preserve the sampling of the parent.
Sampler Sampler
Expand Down Expand Up @@ -158,11 +158,12 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa
span := &Span{}
span.spanContext = parent
mu.Lock()
// TODO(jbd): Reduce the contention.
if !hasParent {
span.spanContext.TraceID = newTraceIDLocked()
}
span.spanContext.SpanID = newSpanIDLocked()
sampler := defaultSampler
sampler := config.DefaultSampler
mu.Unlock()

if !hasParent || remoteParent || o.Sampler != nil {
Expand Down Expand Up @@ -405,12 +406,12 @@ func (s *Span) String() string {
}

var (
mu sync.Mutex // protects the variables below
traceIDRand *rand.Rand
traceIDAdd [2]uint64
nextSpanID uint64
spanIDInc uint64
defaultSampler Sampler
mu sync.Mutex // protects the variables below
traceIDRand *rand.Rand
traceIDAdd [2]uint64
nextSpanID uint64
spanIDInc uint64
config Config
)

func init() {
Expand Down
6 changes: 3 additions & 3 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (

func init() {
// no random sampling, but sample children of sampled spans.
SetDefaultSampler(ProbabilitySampler(0))
SetConfig(Config{DefaultSampler: ProbabilitySampler(0)})
}

func TestStrings(t *testing.T) {
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestSampling(t *testing.T) {
AlwaysSample(),
ProbabilitySampler(0),
} {
SetDefaultSampler(defaultSampler)
SetConfig(Config{DefaultSampler: defaultSampler})
sampler := NeverSample()
if test.parentTraceOptions == 1 {
sampler = AlwaysSample()
Expand All @@ -174,7 +174,7 @@ func TestSampling(t *testing.T) {
}
}
}
SetDefaultSampler(ProbabilitySampler(0)) // reset the default sampler.
SetConfig(Config{DefaultSampler: ProbabilitySampler(0)}) // reset the default sampler.
}

func TestProbabilitySampler(t *testing.T) {
Expand Down

0 comments on commit d5d6a6c

Please sign in to comment.