2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-22 22:19:11 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
2019-10-22 22:19:11 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-26 18:20:49 +02:00
|
|
|
"context"
|
2021-03-08 21:12:13 +02:00
|
|
|
"fmt"
|
2019-10-22 22:19:11 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2020-06-09 20:47:54 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
2020-03-13 22:07:36 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-10-22 22:19:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-01 20:40:29 +02:00
|
|
|
defaultTracerName = "go.opentelemetry.io/otel/sdk/tracer"
|
2019-10-22 22:19:11 +02:00
|
|
|
)
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
// TODO (MrAlias): unify this API option design:
|
|
|
|
// https://github.com/open-telemetry/opentelemetry-go/issues/536
|
2019-11-02 01:35:18 +02:00
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// TracerProviderConfig
|
|
|
|
type TracerProviderConfig struct {
|
2020-09-09 19:19:03 +02:00
|
|
|
processors []SpanProcessor
|
|
|
|
config Config
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
type TracerProviderOption func(*TracerProviderConfig)
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
type TracerProvider struct {
|
2019-10-22 22:19:11 +02:00
|
|
|
mu sync.Mutex
|
2020-06-09 20:47:54 +02:00
|
|
|
namedTracer map[instrumentation.Library]*tracer
|
2019-10-22 22:19:11 +02:00
|
|
|
spanProcessors atomic.Value
|
|
|
|
config atomic.Value // access atomically
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
var _ trace.TracerProvider = &TracerProvider{}
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// NewTracerProvider creates an instance of trace provider. Optional
|
2019-10-22 22:19:11 +02:00
|
|
|
// parameter configures the provider with common options applicable
|
|
|
|
// to all tracer instances that will be created by this provider.
|
2020-09-24 00:16:13 +02:00
|
|
|
func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
|
|
|
|
o := &TracerProviderConfig{}
|
2019-10-22 22:19:11 +02:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(o)
|
|
|
|
}
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := &TracerProvider{
|
2020-06-09 20:47:54 +02:00
|
|
|
namedTracer: make(map[instrumentation.Library]*tracer),
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
tp.config.Store(&Config{
|
2021-02-18 21:31:35 +02:00
|
|
|
DefaultSampler: ParentBased(AlwaysSample()),
|
|
|
|
IDGenerator: defaultIDGenerator(),
|
|
|
|
SpanLimits: SpanLimits{
|
|
|
|
AttributeCountLimit: DefaultAttributeCountLimit,
|
|
|
|
EventCountLimit: DefaultEventCountLimit,
|
|
|
|
LinkCountLimit: DefaultLinkCountLimit,
|
|
|
|
AttributePerEventCountLimit: DefaultAttributePerEventCountLimit,
|
|
|
|
AttributePerLinkCountLimit: DefaultAttributePerLinkCountLimit,
|
|
|
|
},
|
2019-10-22 22:19:11 +02:00
|
|
|
})
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
for _, sp := range o.processors {
|
|
|
|
tp.RegisterSpanProcessor(sp)
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tp.ApplyConfig(o.config)
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
return tp
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
2021-03-03 22:09:58 +02:00
|
|
|
// 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.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
|
|
|
|
c := trace.NewTracerConfig(opts...)
|
2020-09-10 21:15:17 +02:00
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
if name == "" {
|
|
|
|
name = defaultTracerName
|
|
|
|
}
|
2020-06-09 20:47:54 +02:00
|
|
|
il := instrumentation.Library{
|
|
|
|
Name: name,
|
|
|
|
Version: c.InstrumentationVersion,
|
|
|
|
}
|
|
|
|
t, ok := p.namedTracer[il]
|
2019-10-22 22:19:11 +02:00
|
|
|
if !ok {
|
2020-06-09 20:47:54 +02:00
|
|
|
t = &tracer{
|
|
|
|
provider: p,
|
|
|
|
instrumentationLibrary: il,
|
|
|
|
}
|
|
|
|
p.namedTracer[il] = t
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterSpanProcessor adds the given SpanProcessor to the list of SpanProcessors
|
2020-09-24 00:16:13 +02:00
|
|
|
func (p *TracerProvider) RegisterSpanProcessor(s SpanProcessor) {
|
2019-10-22 22:19:11 +02:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2020-09-24 20:43:23 +02:00
|
|
|
new := spanProcessorStates{}
|
|
|
|
if old, ok := p.spanProcessors.Load().(spanProcessorStates); ok {
|
|
|
|
new = append(new, old...)
|
|
|
|
}
|
|
|
|
newSpanSync := &spanProcessorState{
|
|
|
|
sp: s,
|
|
|
|
state: &sync.Once{},
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
2020-09-24 20:43:23 +02:00
|
|
|
new = append(new, newSpanSync)
|
2019-10-22 22:19:11 +02:00
|
|
|
p.spanProcessors.Store(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors
|
2020-09-24 00:16:13 +02:00
|
|
|
func (p *TracerProvider) UnregisterSpanProcessor(s SpanProcessor) {
|
2020-08-22 09:38:52 +02:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2021-03-03 18:26:26 +02:00
|
|
|
spss := spanProcessorStates{}
|
2020-09-24 20:43:23 +02:00
|
|
|
old, ok := p.spanProcessors.Load().(spanProcessorStates)
|
|
|
|
if !ok || len(old) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-03-03 18:26:26 +02:00
|
|
|
spss = append(spss, old...)
|
2020-09-24 20:43:23 +02:00
|
|
|
|
|
|
|
// stop the span processor if it is started and remove it from the list
|
|
|
|
var stopOnce *spanProcessorState
|
|
|
|
var idx int
|
2021-03-03 18:26:26 +02:00
|
|
|
for i, sps := range spss {
|
2020-09-24 20:43:23 +02:00
|
|
|
if sps.sp == s {
|
|
|
|
stopOnce = sps
|
|
|
|
idx = i
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-24 20:43:23 +02:00
|
|
|
if stopOnce != nil {
|
|
|
|
stopOnce.state.Do(func() {
|
2020-12-11 07:28:41 +02:00
|
|
|
if err := s.Shutdown(context.Background()); err != nil {
|
|
|
|
otel.Handle(err)
|
|
|
|
}
|
2019-10-22 22:19:11 +02:00
|
|
|
})
|
|
|
|
}
|
2021-03-03 18:26:26 +02:00
|
|
|
if len(spss) > 1 {
|
|
|
|
copy(spss[idx:], spss[idx+1:])
|
2020-09-24 20:43:23 +02:00
|
|
|
}
|
2021-03-03 18:26:26 +02:00
|
|
|
spss[len(spss)-1] = nil
|
|
|
|
spss = spss[:len(spss)-1]
|
2020-09-24 20:43:23 +02:00
|
|
|
|
2021-03-03 18:26:26 +02:00
|
|
|
p.spanProcessors.Store(spss)
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyConfig changes the configuration of the provider.
|
|
|
|
// If a field in the configuration is empty or nil then its original value is preserved.
|
2020-09-24 00:16:13 +02:00
|
|
|
func (p *TracerProvider) ApplyConfig(cfg Config) {
|
2019-10-22 22:19:11 +02:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
c := *p.config.Load().(*Config)
|
|
|
|
if cfg.DefaultSampler != nil {
|
|
|
|
c.DefaultSampler = cfg.DefaultSampler
|
|
|
|
}
|
|
|
|
if cfg.IDGenerator != nil {
|
|
|
|
c.IDGenerator = cfg.IDGenerator
|
|
|
|
}
|
2021-02-18 21:31:35 +02:00
|
|
|
if cfg.SpanLimits.EventCountLimit > 0 {
|
|
|
|
c.SpanLimits.EventCountLimit = cfg.SpanLimits.EventCountLimit
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
2021-02-18 21:31:35 +02:00
|
|
|
if cfg.SpanLimits.AttributeCountLimit > 0 {
|
|
|
|
c.SpanLimits.AttributeCountLimit = cfg.SpanLimits.AttributeCountLimit
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
2021-02-18 21:31:35 +02:00
|
|
|
if cfg.SpanLimits.LinkCountLimit > 0 {
|
|
|
|
c.SpanLimits.LinkCountLimit = cfg.SpanLimits.LinkCountLimit
|
|
|
|
}
|
|
|
|
if cfg.SpanLimits.AttributePerEventCountLimit > 0 {
|
|
|
|
c.SpanLimits.AttributePerEventCountLimit = cfg.SpanLimits.AttributePerEventCountLimit
|
|
|
|
}
|
|
|
|
if cfg.SpanLimits.AttributePerLinkCountLimit > 0 {
|
|
|
|
c.SpanLimits.AttributePerLinkCountLimit = cfg.SpanLimits.AttributePerLinkCountLimit
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
2021-02-15 22:28:37 +02:00
|
|
|
c.Resource = cfg.Resource
|
|
|
|
if c.Resource == nil {
|
|
|
|
c.Resource = resource.Default()
|
2020-03-13 22:07:36 +02:00
|
|
|
}
|
2019-10-22 22:19:11 +02:00
|
|
|
p.config.Store(&c)
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:12:13 +02: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 {
|
|
|
|
spss, ok := p.spanProcessors.Load().(spanProcessorStates)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to load span processors")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown shuts down the span processors in the order they were registered.
|
2020-10-26 18:20:49 +02:00
|
|
|
func (p *TracerProvider) Shutdown(ctx context.Context) error {
|
|
|
|
spss, ok := p.spanProcessors.Load().(spanProcessorStates)
|
2021-03-08 21:12:13 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to load span processors")
|
|
|
|
}
|
|
|
|
if len(spss) == 0 {
|
2020-10-26 18:20:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sps := range spss {
|
2021-03-08 21:12:13 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2020-10-26 18:20:49 +02:00
|
|
|
sps.state.Do(func() {
|
2021-03-08 21:12:13 +02:00
|
|
|
err = sps.sp.Shutdown(ctx)
|
2020-10-26 18:20:49 +02:00
|
|
|
})
|
2021-03-08 21:12:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-26 18:20:49 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// WithSyncer registers the exporter with the TracerProvider using a
|
2020-09-09 19:19:03 +02:00
|
|
|
// SimpleSpanProcessor.
|
2020-09-24 00:16:13 +02:00
|
|
|
func WithSyncer(e export.SpanExporter) TracerProviderOption {
|
2020-09-09 19:19:03 +02:00
|
|
|
return WithSpanProcessor(NewSimpleSpanProcessor(e))
|
|
|
|
}
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// WithBatcher registers the exporter with the TracerProvider using a
|
2020-09-09 19:19:03 +02:00
|
|
|
// BatchSpanProcessor configured with the passed opts.
|
2020-09-24 00:16:13 +02:00
|
|
|
func WithBatcher(e export.SpanExporter, opts ...BatchSpanProcessorOption) TracerProviderOption {
|
2020-09-09 19:19:03 +02:00
|
|
|
return WithSpanProcessor(NewBatchSpanProcessor(e, opts...))
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// WithSpanProcessor registers the SpanProcessor with a TracerProvider.
|
|
|
|
func WithSpanProcessor(sp SpanProcessor) TracerProviderOption {
|
|
|
|
return func(opts *TracerProviderConfig) {
|
2020-09-09 19:19:03 +02:00
|
|
|
opts.processors = append(opts.processors, sp)
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithConfig option sets the configuration to provider.
|
2020-09-24 00:16:13 +02:00
|
|
|
func WithConfig(config Config) TracerProviderOption {
|
|
|
|
return func(opts *TracerProviderConfig) {
|
2019-10-22 22:19:11 +02:00
|
|
|
opts.config = config
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 22:07:36 +02:00
|
|
|
|
2020-05-20 22:08:43 +02:00
|
|
|
// WithResource option attaches a resource to the provider.
|
|
|
|
// The resource is added to the span when it is started.
|
2020-09-24 00:16:13 +02:00
|
|
|
func WithResource(r *resource.Resource) TracerProviderOption {
|
|
|
|
return func(opts *TracerProviderConfig) {
|
2020-05-20 22:08:43 +02:00
|
|
|
opts.config.Resource = r
|
2020-03-13 22:07:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-10 03:30:32 +02:00
|
|
|
|
|
|
|
// WithIDGenerator option registers an IDGenerator with the TracerProvider.
|
|
|
|
func WithIDGenerator(g IDGenerator) TracerProviderOption {
|
|
|
|
return func(opts *TracerProviderConfig) {
|
|
|
|
opts.config.IDGenerator = g
|
|
|
|
}
|
|
|
|
}
|