2021-08-09 07:19:24 +00:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-08-19 08:01:33 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-08-09 07:19:24 +00:00
|
|
|
|
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel"
|
2023-02-01 17:06:36 +00:00
|
|
|
"go.opentelemetry.io/otel/internal/global"
|
2021-08-09 07:19:24 +00:00
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2023-02-01 17:06:36 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-12-21 16:20:48 +00:00
|
|
|
"go.opentelemetry.io/otel/trace/embedded"
|
|
|
|
"go.opentelemetry.io/otel/trace/noop"
|
2021-08-09 07:19:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTracerName = "go.opentelemetry.io/otel/sdk/tracer"
|
|
|
|
)
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// tracerProviderConfig.
|
|
|
|
type tracerProviderConfig struct {
|
|
|
|
// processors contains collection of SpanProcessors that are processing pipeline
|
|
|
|
// for spans in the trace signal.
|
|
|
|
// SpanProcessors registered with a TracerProvider and are called at the start
|
|
|
|
// and end of a Span's lifecycle, and are called in the order they are
|
|
|
|
// registered.
|
2021-08-09 07:19:24 +00:00
|
|
|
processors []SpanProcessor
|
|
|
|
|
|
|
|
// sampler is the default sampler used when creating new spans.
|
|
|
|
sampler Sampler
|
|
|
|
|
|
|
|
// idGenerator is used to generate all Span and Trace IDs when needed.
|
|
|
|
idGenerator IDGenerator
|
|
|
|
|
|
|
|
// spanLimits defines the attribute, event, and link limits for spans.
|
|
|
|
spanLimits SpanLimits
|
|
|
|
|
|
|
|
// resource contains attributes representing an entity that produces telemetry.
|
|
|
|
resource *resource.Resource
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
// MarshalLog is the marshaling function used by the logging system to represent this Provider.
|
2023-02-01 17:06:36 +00:00
|
|
|
func (cfg tracerProviderConfig) MarshalLog() interface{} {
|
|
|
|
return struct {
|
|
|
|
SpanProcessors []SpanProcessor
|
|
|
|
SamplerType string
|
|
|
|
IDGeneratorType string
|
|
|
|
SpanLimits SpanLimits
|
|
|
|
Resource *resource.Resource
|
|
|
|
}{
|
|
|
|
SpanProcessors: cfg.processors,
|
|
|
|
SamplerType: fmt.Sprintf("%T", cfg.sampler),
|
|
|
|
IDGeneratorType: fmt.Sprintf("%T", cfg.idGenerator),
|
|
|
|
SpanLimits: cfg.spanLimits,
|
|
|
|
Resource: cfg.resource,
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 07:19:24 +00:00
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// TracerProvider is an OpenTelemetry TracerProvider. It provides Tracers to
|
|
|
|
// instrumentation so it can trace operational flow through a system.
|
2021-08-09 07:19:24 +00:00
|
|
|
type TracerProvider struct {
|
2023-12-21 16:20:48 +00:00
|
|
|
embedded.TracerProvider
|
|
|
|
|
2021-08-09 07:19:24 +00:00
|
|
|
mu sync.Mutex
|
2023-02-01 17:06:36 +00:00
|
|
|
namedTracer map[instrumentation.Scope]*tracer
|
2023-12-18 20:31:00 +00:00
|
|
|
spanProcessors atomic.Pointer[spanProcessorStates]
|
|
|
|
|
|
|
|
isShutdown atomic.Bool
|
2023-02-01 17:06:36 +00:00
|
|
|
|
|
|
|
// These fields are not protected by the lock mu. They are assumed to be
|
|
|
|
// immutable after creation of the TracerProvider.
|
|
|
|
sampler Sampler
|
|
|
|
idGenerator IDGenerator
|
|
|
|
spanLimits SpanLimits
|
|
|
|
resource *resource.Resource
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ trace.TracerProvider = &TracerProvider{}
|
|
|
|
|
|
|
|
// NewTracerProvider returns a new and configured TracerProvider.
|
|
|
|
//
|
|
|
|
// By default the returned TracerProvider is configured with:
|
2023-02-01 17:06:36 +00:00
|
|
|
// - a ParentBased(AlwaysSample) Sampler
|
|
|
|
// - a random number IDGenerator
|
|
|
|
// - the resource.Default() Resource
|
|
|
|
// - the default SpanLimits.
|
2021-08-09 07:19:24 +00:00
|
|
|
//
|
|
|
|
// The passed opts are used to override these default values and configure the
|
|
|
|
// returned TracerProvider appropriately.
|
|
|
|
func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
|
2023-02-01 17:06:36 +00:00
|
|
|
o := tracerProviderConfig{
|
|
|
|
spanLimits: NewSpanLimits(),
|
|
|
|
}
|
|
|
|
o = applyTracerProviderEnvConfigs(o)
|
2021-08-09 07:19:24 +00:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
2023-02-01 17:06:36 +00:00
|
|
|
o = opt.apply(o)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
o = ensureValidTracerProviderConfig(o)
|
2021-08-09 07:19:24 +00:00
|
|
|
|
|
|
|
tp := &TracerProvider{
|
2023-02-01 17:06:36 +00:00
|
|
|
namedTracer: make(map[instrumentation.Scope]*tracer),
|
2021-08-09 07:19:24 +00:00
|
|
|
sampler: o.sampler,
|
|
|
|
idGenerator: o.idGenerator,
|
|
|
|
spanLimits: o.spanLimits,
|
|
|
|
resource: o.resource,
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
global.Info("TracerProvider created", "config", o)
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
spss := make(spanProcessorStates, 0, len(o.processors))
|
2021-08-09 07:19:24 +00:00
|
|
|
for _, sp := range o.processors {
|
2023-12-18 20:31:00 +00:00
|
|
|
spss = append(spss, newSpanProcessorState(sp))
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
tp.spanProcessors.Store(&spss)
|
2021-08-09 07:19:24 +00:00
|
|
|
|
|
|
|
return tp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer returns a Tracer with the given name and options. If a Tracer for
|
|
|
|
// the given name and options does not exist it is created, otherwise the
|
|
|
|
// existing Tracer is returned.
|
|
|
|
//
|
|
|
|
// If name is empty, DefaultTracerName is used instead.
|
|
|
|
//
|
|
|
|
// This method is safe to be called concurrently.
|
|
|
|
func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
|
2023-12-18 20:31:00 +00:00
|
|
|
// This check happens before the mutex is acquired to avoid deadlocking if Tracer() is called from within Shutdown().
|
|
|
|
if p.isShutdown.Load() {
|
2023-12-21 16:20:48 +00:00
|
|
|
return noop.NewTracerProvider().Tracer(name, opts...)
|
2023-12-18 20:31:00 +00:00
|
|
|
}
|
2021-08-09 07:19:24 +00:00
|
|
|
c := trace.NewTracerConfig(opts...)
|
|
|
|
if name == "" {
|
|
|
|
name = defaultTracerName
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
is := instrumentation.Scope{
|
|
|
|
Name: name,
|
|
|
|
Version: c.InstrumentationVersion(),
|
|
|
|
SchemaURL: c.SchemaURL(),
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
|
|
|
|
t, ok := func() (trace.Tracer, bool) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
// Must check the flag after acquiring the mutex to avoid returning a valid tracer if Shutdown() ran
|
|
|
|
// after the first check above but before we acquired the mutex.
|
|
|
|
if p.isShutdown.Load() {
|
2023-12-21 16:20:48 +00:00
|
|
|
return noop.NewTracerProvider().Tracer(name, opts...), true
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
t, ok := p.namedTracer[is]
|
|
|
|
if !ok {
|
|
|
|
t = &tracer{
|
|
|
|
provider: p,
|
|
|
|
instrumentationScope: is,
|
|
|
|
}
|
|
|
|
p.namedTracer[is] = t
|
|
|
|
}
|
|
|
|
return t, ok
|
|
|
|
}()
|
|
|
|
if !ok {
|
|
|
|
// This code is outside the mutex to not hold the lock while calling third party logging code:
|
|
|
|
// - That code may do slow things like I/O, which would prolong the duration the lock is held,
|
|
|
|
// slowing down all tracing consumers.
|
|
|
|
// - Logging code may be instrumented with tracing and deadlock because it could try
|
|
|
|
// acquiring the same non-reentrant mutex.
|
|
|
|
global.Info("Tracer created", "name", name, "version", is.Version, "schemaURL", is.SchemaURL)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// RegisterSpanProcessor adds the given SpanProcessor to the list of SpanProcessors.
|
2023-12-18 20:31:00 +00:00
|
|
|
func (p *TracerProvider) RegisterSpanProcessor(sp SpanProcessor) {
|
|
|
|
// This check prevents calls during a shutdown.
|
|
|
|
if p.isShutdown.Load() {
|
|
|
|
return
|
|
|
|
}
|
2021-08-09 07:19:24 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2023-12-18 20:31:00 +00:00
|
|
|
// This check prevents calls after a shutdown.
|
|
|
|
if p.isShutdown.Load() {
|
|
|
|
return
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
|
|
|
|
current := p.getSpanProcessors()
|
|
|
|
newSPS := make(spanProcessorStates, 0, len(current)+1)
|
|
|
|
newSPS = append(newSPS, current...)
|
|
|
|
newSPS = append(newSPS, newSpanProcessorState(sp))
|
|
|
|
p.spanProcessors.Store(&newSPS)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors.
|
2023-12-18 20:31:00 +00:00
|
|
|
func (p *TracerProvider) UnregisterSpanProcessor(sp SpanProcessor) {
|
|
|
|
// This check prevents calls during a shutdown.
|
|
|
|
if p.isShutdown.Load() {
|
|
|
|
return
|
|
|
|
}
|
2021-08-09 07:19:24 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2023-12-18 20:31:00 +00:00
|
|
|
// This check prevents calls after a shutdown.
|
|
|
|
if p.isShutdown.Load() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
old := p.getSpanProcessors()
|
|
|
|
if len(old) == 0 {
|
2021-08-09 07:19:24 +00:00
|
|
|
return
|
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
spss := make(spanProcessorStates, len(old))
|
|
|
|
copy(spss, old)
|
2021-08-09 07:19:24 +00:00
|
|
|
|
|
|
|
// stop the span processor if it is started and remove it from the list
|
|
|
|
var stopOnce *spanProcessorState
|
|
|
|
var idx int
|
|
|
|
for i, sps := range spss {
|
2023-12-18 20:31:00 +00:00
|
|
|
if sps.sp == sp {
|
2021-08-09 07:19:24 +00:00
|
|
|
stopOnce = sps
|
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if stopOnce != nil {
|
|
|
|
stopOnce.state.Do(func() {
|
2023-12-18 20:31:00 +00:00
|
|
|
if err := sp.Shutdown(context.Background()); err != nil {
|
2021-08-09 07:19:24 +00:00
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(spss) > 1 {
|
|
|
|
copy(spss[idx:], spss[idx+1:])
|
|
|
|
}
|
|
|
|
spss[len(spss)-1] = nil
|
|
|
|
spss = spss[:len(spss)-1]
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
p.spanProcessors.Store(&spss)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForceFlush immediately exports all spans that have not yet been exported for
|
|
|
|
// all the registered span processors.
|
|
|
|
func (p *TracerProvider) ForceFlush(ctx context.Context) error {
|
2023-12-18 20:31:00 +00:00
|
|
|
spss := p.getSpanProcessors()
|
2021-08-09 07:19:24 +00:00
|
|
|
if len(spss) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sps := range spss {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sps.sp.ForceFlush(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
// Shutdown shuts down TracerProvider. All registered span processors are shut down
|
|
|
|
// in the order they were registered and any held computational resources are released.
|
|
|
|
// After Shutdown is called, all methods are no-ops.
|
2021-08-09 07:19:24 +00:00
|
|
|
func (p *TracerProvider) Shutdown(ctx context.Context) error {
|
2023-12-18 20:31:00 +00:00
|
|
|
// This check prevents deadlocks in case of recursive shutdown.
|
|
|
|
if p.isShutdown.Load() {
|
|
|
|
return nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
// This check prevents calls after a shutdown has already been done concurrently.
|
|
|
|
if !p.isShutdown.CompareAndSwap(false, true) { // did toggle?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
var retErr error
|
2023-12-18 20:31:00 +00:00
|
|
|
for _, sps := range p.getSpanProcessors() {
|
2021-08-09 07:19:24 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
sps.state.Do(func() {
|
|
|
|
err = sps.sp.Shutdown(ctx)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
if retErr == nil {
|
|
|
|
retErr = err
|
|
|
|
} else {
|
|
|
|
// Poor man's list of errors
|
2024-08-19 08:01:33 +00:00
|
|
|
retErr = fmt.Errorf("%w; %w", retErr, err)
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
p.spanProcessors.Store(&spanProcessorStates{})
|
2023-02-01 17:06:36 +00:00
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
func (p *TracerProvider) getSpanProcessors() spanProcessorStates {
|
|
|
|
return *(p.spanProcessors.Load())
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// TracerProviderOption configures a TracerProvider.
|
|
|
|
type TracerProviderOption interface {
|
|
|
|
apply(tracerProviderConfig) tracerProviderConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type traceProviderOptionFunc func(tracerProviderConfig) tracerProviderConfig
|
|
|
|
|
|
|
|
func (fn traceProviderOptionFunc) apply(cfg tracerProviderConfig) tracerProviderConfig {
|
|
|
|
return fn(cfg)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithSyncer registers the exporter with the TracerProvider using a
|
|
|
|
// SimpleSpanProcessor.
|
2023-02-01 17:06:36 +00:00
|
|
|
//
|
|
|
|
// This is not recommended for production use. The synchronous nature of the
|
|
|
|
// SimpleSpanProcessor that will wrap the exporter make it good for testing,
|
|
|
|
// debugging, or showing examples of other feature, but it will be slow and
|
|
|
|
// have a high computation resource usage overhead. The WithBatcher option is
|
|
|
|
// recommended for production use instead.
|
2021-08-09 07:19:24 +00:00
|
|
|
func WithSyncer(e SpanExporter) TracerProviderOption {
|
|
|
|
return WithSpanProcessor(NewSimpleSpanProcessor(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBatcher registers the exporter with the TracerProvider using a
|
|
|
|
// BatchSpanProcessor configured with the passed opts.
|
|
|
|
func WithBatcher(e SpanExporter, opts ...BatchSpanProcessorOption) TracerProviderOption {
|
|
|
|
return WithSpanProcessor(NewBatchSpanProcessor(e, opts...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSpanProcessor registers the SpanProcessor with a TracerProvider.
|
|
|
|
func WithSpanProcessor(sp SpanProcessor) TracerProviderOption {
|
2023-02-01 17:06:36 +00:00
|
|
|
return traceProviderOptionFunc(func(cfg tracerProviderConfig) tracerProviderConfig {
|
|
|
|
cfg.processors = append(cfg.processors, sp)
|
|
|
|
return cfg
|
|
|
|
})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-02-01 17:06:36 +00:00
|
|
|
return traceProviderOptionFunc(func(cfg tracerProviderConfig) tracerProviderConfig {
|
|
|
|
var err error
|
|
|
|
cfg.resource, err = resource.Merge(resource.Environment(), r)
|
|
|
|
if err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-02-01 17:06:36 +00:00
|
|
|
return traceProviderOptionFunc(func(cfg tracerProviderConfig) tracerProviderConfig {
|
2021-08-09 07:19:24 +00:00
|
|
|
if g != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
cfg.idGenerator = g
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
return cfg
|
|
|
|
})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
2023-02-01 17:06:36 +00:00
|
|
|
// This option overrides the Sampler configured through the OTEL_TRACES_SAMPLER
|
|
|
|
// and OTEL_TRACES_SAMPLER_ARG environment variables. If this option is not used
|
|
|
|
// and the sampler is not configured through environment variables or the environment
|
|
|
|
// contains invalid/unsupported configuration, the TracerProvider will use a
|
2021-08-09 07:19:24 +00:00
|
|
|
// ParentBased(AlwaysSample) Sampler by default.
|
|
|
|
func WithSampler(s Sampler) TracerProviderOption {
|
2023-02-01 17:06:36 +00:00
|
|
|
return traceProviderOptionFunc(func(cfg tracerProviderConfig) tracerProviderConfig {
|
2021-08-09 07:19:24 +00:00
|
|
|
if s != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
cfg.sampler = s
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
return cfg
|
|
|
|
})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithSpanLimits returns a TracerProviderOption that configures a
|
|
|
|
// TracerProvider to use the SpanLimits sl. These SpanLimits bound any Span
|
|
|
|
// created by a Tracer from the TracerProvider.
|
2021-08-09 07:19:24 +00:00
|
|
|
//
|
2023-02-01 17:06:36 +00:00
|
|
|
// If any field of sl is zero or negative it will be replaced with the default
|
|
|
|
// value for that field.
|
|
|
|
//
|
|
|
|
// If this or WithRawSpanLimits are not provided, the TracerProvider will use
|
|
|
|
// the limits defined by environment variables, or the defaults if unset.
|
|
|
|
// Refer to the NewSpanLimits documentation for information about this
|
|
|
|
// relationship.
|
|
|
|
//
|
|
|
|
// Deprecated: Use WithRawSpanLimits instead which allows setting unlimited
|
|
|
|
// and zero limits. This option will be kept until the next major version
|
|
|
|
// incremented release.
|
2021-08-09 07:19:24 +00:00
|
|
|
func WithSpanLimits(sl SpanLimits) TracerProviderOption {
|
2023-02-01 17:06:36 +00:00
|
|
|
if sl.AttributeValueLengthLimit <= 0 {
|
|
|
|
sl.AttributeValueLengthLimit = DefaultAttributeValueLengthLimit
|
|
|
|
}
|
|
|
|
if sl.AttributeCountLimit <= 0 {
|
|
|
|
sl.AttributeCountLimit = DefaultAttributeCountLimit
|
|
|
|
}
|
|
|
|
if sl.EventCountLimit <= 0 {
|
|
|
|
sl.EventCountLimit = DefaultEventCountLimit
|
|
|
|
}
|
|
|
|
if sl.AttributePerEventCountLimit <= 0 {
|
|
|
|
sl.AttributePerEventCountLimit = DefaultAttributePerEventCountLimit
|
|
|
|
}
|
|
|
|
if sl.LinkCountLimit <= 0 {
|
|
|
|
sl.LinkCountLimit = DefaultLinkCountLimit
|
|
|
|
}
|
|
|
|
if sl.AttributePerLinkCountLimit <= 0 {
|
|
|
|
sl.AttributePerLinkCountLimit = DefaultAttributePerLinkCountLimit
|
|
|
|
}
|
|
|
|
return traceProviderOptionFunc(func(cfg tracerProviderConfig) tracerProviderConfig {
|
|
|
|
cfg.spanLimits = sl
|
|
|
|
return cfg
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRawSpanLimits returns a TracerProviderOption that configures a
|
|
|
|
// TracerProvider to use these limits. These limits bound any Span created by
|
|
|
|
// a Tracer from the TracerProvider.
|
|
|
|
//
|
|
|
|
// The limits will be used as-is. Zero or negative values will not be changed
|
|
|
|
// to the default value like WithSpanLimits does. Setting a limit to zero will
|
|
|
|
// effectively disable the related resource it limits and setting to a
|
|
|
|
// negative value will mean that resource is unlimited. Consequentially, this
|
|
|
|
// means that the zero-value SpanLimits will disable all span resources.
|
|
|
|
// Because of this, limits should be constructed using NewSpanLimits and
|
|
|
|
// updated accordingly.
|
|
|
|
//
|
|
|
|
// If this or WithSpanLimits are not provided, the TracerProvider will use the
|
|
|
|
// limits defined by environment variables, or the defaults if unset. Refer to
|
|
|
|
// the NewSpanLimits documentation for information about this relationship.
|
|
|
|
func WithRawSpanLimits(limits SpanLimits) TracerProviderOption {
|
|
|
|
return traceProviderOptionFunc(func(cfg tracerProviderConfig) tracerProviderConfig {
|
|
|
|
cfg.spanLimits = limits
|
|
|
|
return cfg
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyTracerProviderEnvConfigs(cfg tracerProviderConfig) tracerProviderConfig {
|
|
|
|
for _, opt := range tracerProviderOptionsFromEnv() {
|
|
|
|
cfg = opt.apply(cfg)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func tracerProviderOptionsFromEnv() []TracerProviderOption {
|
|
|
|
var opts []TracerProviderOption
|
|
|
|
|
|
|
|
sampler, err := samplerFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sampler != nil {
|
|
|
|
opts = append(opts, WithSampler(sampler))
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensureValidTracerProviderConfig ensures that given TracerProviderConfig is valid.
|
2023-02-01 17:06:36 +00:00
|
|
|
func ensureValidTracerProviderConfig(cfg tracerProviderConfig) tracerProviderConfig {
|
2021-08-09 07:19:24 +00:00
|
|
|
if cfg.sampler == nil {
|
|
|
|
cfg.sampler = ParentBased(AlwaysSample())
|
|
|
|
}
|
|
|
|
if cfg.idGenerator == nil {
|
|
|
|
cfg.idGenerator = defaultIDGenerator()
|
|
|
|
}
|
|
|
|
if cfg.resource == nil {
|
|
|
|
cfg.resource = resource.Default()
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
return cfg
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|