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

Commit

Permalink
Avoid global configuration overriding all (#655)
Browse files Browse the repository at this point in the history
It will be more common for the users to tweak a few
settings rather than all.

Rename SetConfig to ApplyConfig and only
override the non-zero values.
  • Loading branch information
rakyll committed Mar 29, 2018
1 parent 7cc1962 commit 7630269
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 26 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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(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()
SetConfig(Config{DefaultSampler: AlwaysSample()})
ApplyConfig(Config{DefaultSampler: AlwaysSample()})
fn(b)
})
b.Run("NeverSample", func(b *testing.B) {
b.ReportAllocs()
SetConfig(Config{DefaultSampler: NeverSample()})
ApplyConfig(Config{DefaultSampler: NeverSample()})
fn(b)
})
}
10 changes: 6 additions & 4 deletions trace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ type Config struct {
DefaultSampler Sampler
}

// SetConfig sets the global tracing configuration.
func SetConfig(cfg Config) {
// ApplyConfig applies changes to the global tracing configuration.
//
// Fields not provided in the given config are going to be preserved.
func ApplyConfig(cfg Config) {
mu.Lock()
if cfg.DefaultSampler == nil {
cfg.DefaultSampler = newDefaultSampler()
cfg.DefaultSampler = config.DefaultSampler
}
mu.Lock()
// TODO(jbd): Reduce the global contention on config.
config = cfg
mu.Unlock()
Expand Down
28 changes: 28 additions & 0 deletions trace/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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

import (
"reflect"
"testing"
)

func TestApplyZeroConfig(t *testing.T) {
cfg := config
ApplyConfig(Config{})
if got, want := reflect.ValueOf(config.DefaultSampler).Pointer(), reflect.ValueOf(cfg.DefaultSampler).Pointer(); got != want {
t.Fatalf("config.DefaultSampler = %#v; want %#v", got, want)
}
}
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 SetConfig. Use a ProbabilitySampler
frequency for your entire program, call ApplyConfig. Use a ProbabilitySampler
to sample a subset of traces, or use AlwaysSample to collect a trace on every run:
trace.SetConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
Adding Spans to a Trace
Expand Down
2 changes: 1 addition & 1 deletion trace/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newDefaultSampler() Sampler {

// SetDefaultSampler sets the default sampler used when creating new spans.
//
// Deprecated: Use SetConfig.
// Deprecated: Use ApplyConfig.
func SetDefaultSampler(sampler Sampler) {
}

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.
SetConfig(Config{DefaultSampler: ProbabilitySampler(0)})
ApplyConfig(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),
} {
SetConfig(Config{DefaultSampler: defaultSampler})
ApplyConfig(Config{DefaultSampler: defaultSampler})
sampler := NeverSample()
if test.parentTraceOptions == 1 {
sampler = AlwaysSample()
Expand All @@ -174,7 +174,7 @@ func TestSampling(t *testing.T) {
}
}
}
SetConfig(Config{DefaultSampler: ProbabilitySampler(0)}) // reset the default sampler.
ApplyConfig(Config{DefaultSampler: ProbabilitySampler(0)}) // reset the default sampler.
}

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

0 comments on commit 7630269

Please sign in to comment.