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

Introduce trace.Config #653

Merged
merged 1 commit into from
Mar 28, 2018
Merged
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
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)
})
}
36 changes: 36 additions & 0 deletions trace/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package trace

func init() {
config.DefaultSampler = ProbabilitySampler(defaultSamplingProbability)
}

// Config represents the global tracing configuration.
type Config struct {
// DefaultSampler is the default sampler used when creating new spans.
DefaultSampler Sampler
}

// SetConfig sets the global tracing configuration.
func SetConfig(cfg Config) {
if cfg.DefaultSampler == nil {
cfg.DefaultSampler = newDefaultSampler()
}
mu.Lock()
// TODO(jbd): Reduce the global contention on config.
config = cfg
mu.Unlock()
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this thread safe? Shouldn't you use something like this: https://golang.org/pkg/sync/atomic/#Value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole block is guarded by a mutex right now.

If you are asking about the reference, in Go, things are pass by value. User cannot mutate the config.DefaultSampler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you prefer atomic values for all the global state, I added a TODO for that.

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