Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Added `Marshaler` config option to `otlphttp` to enable otlp over json or protobufs. (#1586)
- A `ForceFlush` method to the `"go.opentelemetry.io/otel/sdk/trace".TracerProvider` to flush all registered `SpanProcessor`s. (#1608)
- Added `WithDefaultSampler` and `WithSpanLimits` to tracer provider. (#1633)
- Added `WithSampler` and `WithSpanLimits` to tracer provider. (#1633, #1702)
- Jaeger exporter falls back to `resource.Default`'s `service.name` if the exported Span does not have one. (#1673)

### Changed
Expand Down
2 changes: 1 addition & 1 deletion example/namedtracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func initTracer() {
}
bsp := sdktrace.NewBatchSpanProcessor(exp)
tp = sdktrace.NewTracerProvider(
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tp)
Expand Down
2 changes: 1 addition & 1 deletion example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func initProvider() func() {

bsp := sdktrace.NewBatchSpanProcessor(exp)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/internal/otlptest/otlptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
// themselves.
func RunEndToEndTest(ctx context.Context, t *testing.T, exp *otlp.Exporter, mcTraces, mcMetrics Collector) {
pOpts := []sdktrace.TracerProviderOption{
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(
exp,
// add following two options to ensure flush
Expand Down
6 changes: 3 additions & 3 deletions exporters/otlp/otlpgrpc/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Example_insecure() {
}()

tp := sdktrace.NewTracerProvider(
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(
exp,
// add following two options to ensure flush
Expand Down Expand Up @@ -102,7 +102,7 @@ func Example_withTLS() {
}()

tp := sdktrace.NewTracerProvider(
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(
exp,
// add following two options to ensure flush
Expand Down Expand Up @@ -163,7 +163,7 @@ func Example_withDifferentSignalCollectors() {
}()

tp := sdktrace.NewTracerProvider(
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(
exp,
// add following two options to ensure flush
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlpgrpc/otlp_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) {
}()

tp := sdktrace.NewTracerProvider(
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(
exp,
// add following two options to ensure flush
Expand Down
2 changes: 1 addition & 1 deletion exporters/trace/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func NewExportPipeline(endpointOption EndpointOption, opts ...Option) (trace.Tra
pOpts := []sdktrace.TracerProviderOption{sdktrace.WithSyncer(exporter)}
if exporter.o.Config != nil {
pOpts = append(pOpts,
sdktrace.WithDefaultSampler(exporter.o.Config.DefaultSampler),
sdktrace.WithSampler(exporter.o.Config.DefaultSampler),
sdktrace.WithIDGenerator(exporter.o.Config.IDGenerator),
sdktrace.WithSpanLimits(exporter.o.Config.SpanLimits),
sdktrace.WithResource(exporter.o.Config.Resource),
Expand Down
2 changes: 1 addition & 1 deletion exporters/trace/jaeger/jaeger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestExporter_ExportSpan(t *testing.T) {
assert.NoError(t, err)

tp := sdktrace.NewTracerProvider(
sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exp),
)
otel.SetTracerProvider(tp)
Expand Down
2 changes: 1 addition & 1 deletion sdk/trace/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ func traceBenchmark(b *testing.B, name string, fn func(*testing.B, trace.Tracer)
}

func tracer(b *testing.B, name string, sampler sdktrace.Sampler) trace.Tracer {
tp := sdktrace.NewTracerProvider(sdktrace.WithDefaultSampler(sampler))
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sampler))
return tp.Tracer(name)
}
48 changes: 39 additions & 9 deletions sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ type TracerProvider struct {

var _ trace.TracerProvider = &TracerProvider{}

// NewTracerProvider creates an instance of trace provider. Optional
// parameter configures the provider with common options applicable
// to all tracer instances that will be created by this provider.
// NewTracerProvider returns a new and configured TracerProvider.
//
// By default the returned TracerProvider is configured with:
// - a ParentBased(AlwaysSample) Sampler
// - a random number IDGenerator
// - the resource.Default() Resource
// - the default SpanLimits.
//
// The passed opts are used to override these default values and configure the
// returned TracerProvider appropriately.
func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
o := &TracerProviderConfig{}

Expand Down Expand Up @@ -274,29 +281,52 @@ func WithSpanProcessor(sp SpanProcessor) TracerProviderOption {
}
}

// WithResource option attaches a resource to the provider.
// The resource is added to the span when it is started.
// WithResource returns a TracerProviderOption that will configure the
// Resource r as a TracerProvider's Resource. The configured Resource is
// referenced by all the Tracers the TracerProvider creates. It represents the
// entity producing telemetry.
//
// If this option is not used, the TracerProvider will use the
// resource.Default() Resource by default.
func WithResource(r *resource.Resource) TracerProviderOption {
return func(opts *TracerProviderConfig) {
opts.config.Resource = r
}
}

// WithIDGenerator option registers an IDGenerator with the TracerProvider.
// WithIDGenerator returns a TracerProviderOption that will configure the
// IDGenerator g as a TracerProvider's IDGenerator. The configured IDGenerator
// is used by the Tracers the TracerProvider creates to generate new Span and
// Trace IDs.
//
// If this option is not used, the TracerProvider will use a random number
// IDGenerator by default.
func WithIDGenerator(g IDGenerator) TracerProviderOption {
return func(opts *TracerProviderConfig) {
opts.config.IDGenerator = g
}
}

// WithDefaultSampler option registers a DefaultSampler with the the TracerProvider.
func WithDefaultSampler(s Sampler) TracerProviderOption {
// WithSampler returns a TracerProviderOption that will configure the Sampler
// s as a TracerProvider's Sampler. The configured Sampler is used by the
// Tracers the TracerProvider creates to make their sampling decisions for the
// Spans they create.
//
// If this option is not used, the TracerProvider will use a
// ParentBased(AlwaysSample) Sampler by default.
func WithSampler(s Sampler) TracerProviderOption {
return func(opts *TracerProviderConfig) {
opts.config.DefaultSampler = s
}
}

// WithSpanLimits option registers a SpanLimits with the the TracerProvider.
// WithSpanLimits returns a TracerProviderOption that will configure the
// SpanLimits sl as a TracerProvider's SpanLimits. The configured SpanLimits
// are used used by the Tracers the TracerProvider and the Spans they create
// to limit tracing resources used.
//
// If this option is not used, the TracerProvider will use the default
// SpanLimits.
func WithSpanLimits(sl SpanLimits) TracerProviderOption {
return func(opts *TracerProviderConfig) {
opts.config.SpanLimits = sl
Expand Down
23 changes: 11 additions & 12 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func TestTracerFollowsExpectedAPIBehaviour(t *testing.T) {
harness := oteltest.NewHarness(t)

harness.TestTracerProvider(func() trace.TracerProvider {
return NewTracerProvider(WithDefaultSampler(TraceIDRatioBased(0)))
return NewTracerProvider(WithSampler(TraceIDRatioBased(0)))
})

tp := NewTracerProvider(WithDefaultSampler(TraceIDRatioBased(0)))
tp := NewTracerProvider(WithSampler(TraceIDRatioBased(0)))
harness.TestTracer(func() trace.Tracer {
return tp.Tracer("")
})
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestSampling(t *testing.T) {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
p := NewTracerProvider(WithDefaultSampler(tc.sampler))
p := NewTracerProvider(WithSampler(tc.sampler))
tr := p.Tracer("test")
var sampled int
for i := 0; i < total; i++ {
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestSetSpanAttributes(t *testing.T) {
func TestSamplerAttributesLocalChildSpan(t *testing.T) {
sampler := &testSampler{prefix: "span", t: t}
te := NewTestExporter()
tp := NewTracerProvider(WithDefaultSampler(sampler), WithSyncer(te), WithResource(resource.Empty()))
tp := NewTracerProvider(WithSampler(sampler), WithSyncer(te), WithResource(resource.Empty()))

ctx := context.Background()
ctx, span := startLocalSpan(tp, ctx, "SpanOne", "span0")
Expand Down Expand Up @@ -953,7 +953,7 @@ func TestEndSpanTwice(t *testing.T) {

func TestStartSpanAfterEnd(t *testing.T) {
te := NewTestExporter()
tp := NewTracerProvider(WithDefaultSampler(AlwaysSample()), WithSyncer(te))
tp := NewTracerProvider(WithSampler(AlwaysSample()), WithSyncer(te))
ctx := context.Background()

tr := tp.Tracer("SpanAfterEnd")
Expand Down Expand Up @@ -998,7 +998,7 @@ func TestStartSpanAfterEnd(t *testing.T) {

func TestChildSpanCount(t *testing.T) {
te := NewTestExporter()
tp := NewTracerProvider(WithDefaultSampler(AlwaysSample()), WithSyncer(te))
tp := NewTracerProvider(WithSampler(AlwaysSample()), WithSyncer(te))

tr := tp.Tracer("ChidSpanCount")
ctx, span0 := tr.Start(context.Background(), "parent")
Expand Down Expand Up @@ -1052,7 +1052,7 @@ func TestNilSpanEnd(t *testing.T) {

func TestExecutionTracerTaskEnd(t *testing.T) {
var n uint64
tp := NewTracerProvider(WithDefaultSampler(NeverSample()))
tp := NewTracerProvider(WithSampler(NeverSample()))
tr := tp.Tracer("Execution Tracer Task End")

executionTracerTaskEnd := func() {
Expand Down Expand Up @@ -1085,7 +1085,6 @@ func TestExecutionTracerTaskEnd(t *testing.T) {
s.executionTracerTaskEnd = executionTracerTaskEnd
spans = append(spans, s) // parent not sampled

// tp.ApplyConfig(Config{DefaultSampler: AlwaysSample()})
_, apiSpan = tr.Start(context.Background(), "foo")
s = apiSpan.(*span)
s.executionTracerTaskEnd = executionTracerTaskEnd
Expand All @@ -1101,7 +1100,7 @@ func TestExecutionTracerTaskEnd(t *testing.T) {

func TestCustomStartEndTime(t *testing.T) {
te := NewTestExporter()
tp := NewTracerProvider(WithSyncer(te), WithDefaultSampler(AlwaysSample()))
tp := NewTracerProvider(WithSyncer(te), WithSampler(AlwaysSample()))

startTime := time.Date(2019, time.August, 27, 14, 42, 0, 0, time.UTC)
endTime := startTime.Add(time.Second * 20)
Expand Down Expand Up @@ -1215,7 +1214,7 @@ func TestRecordErrorNil(t *testing.T) {

func TestWithSpanKind(t *testing.T) {
te := NewTestExporter()
tp := NewTracerProvider(WithSyncer(te), WithDefaultSampler(AlwaysSample()), WithResource(resource.Empty()))
tp := NewTracerProvider(WithSyncer(te), WithSampler(AlwaysSample()), WithResource(resource.Empty()))
tr := tp.Tracer("withSpanKind")

_, span := tr.Start(context.Background(), "WithoutSpanKind")
Expand Down Expand Up @@ -1285,7 +1284,7 @@ func TestWithResource(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
te := NewTestExporter()
defaultOptions := []TracerProviderOption{WithSyncer(te), WithDefaultSampler(AlwaysSample())}
defaultOptions := []TracerProviderOption{WithSyncer(te), WithSampler(AlwaysSample())}
tp := NewTracerProvider(append(defaultOptions, tc.options...)...)
span := startSpan(tp, "WithResource")
span.SetAttributes(attribute.String("key1", "value1"))
Expand Down Expand Up @@ -1708,7 +1707,7 @@ func TestSamplerTraceState(t *testing.T) {
ts := ts
t.Run(ts.name, func(t *testing.T) {
te := NewTestExporter()
tp := NewTracerProvider(WithDefaultSampler(ts.sampler), WithSyncer(te), WithResource(resource.Empty()))
tp := NewTracerProvider(WithSampler(ts.sampler), WithSyncer(te), WithResource(resource.Empty()))
tr := tp.Tracer("TraceState")

sc1 := trace.NewSpanContext(trace.SpanContextConfig{
Expand Down
2 changes: 1 addition & 1 deletion sdk/trace/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ import (
)

func basicTracerProvider(t *testing.T) *sdktrace.TracerProvider {
tp := sdktrace.NewTracerProvider(sdktrace.WithDefaultSampler(sdktrace.AlwaysSample()))
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
return tp
}