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.
|
|
|
|
|
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
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-10-09 04:58:56 +02:00
|
|
|
var _ otel.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{
|
2020-09-10 21:39:04 +02:00
|
|
|
DefaultSampler: ParentBased(AlwaysSample()),
|
2019-10-22 22:19:11 +02:00
|
|
|
IDGenerator: defIDGenerator(),
|
|
|
|
MaxAttributesPerSpan: DefaultMaxAttributesPerSpan,
|
|
|
|
MaxEventsPerSpan: DefaultMaxEventsPerSpan,
|
|
|
|
MaxLinksPerSpan: DefaultMaxLinksPerSpan,
|
|
|
|
})
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-25 19:46:07 +02:00
|
|
|
// Tracer with the given name. If a tracer for the given name does not exist,
|
2019-10-30 01:53:50 +02:00
|
|
|
// it is created first. If the name is empty, DefaultTracerName is used.
|
2020-10-09 04:58:56 +02:00
|
|
|
func (p *TracerProvider) Tracer(name string, opts ...otel.TracerOption) otel.Tracer {
|
|
|
|
c := otel.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()
|
2020-09-24 20:43:23 +02:00
|
|
|
new := spanProcessorStates{}
|
|
|
|
old, ok := p.spanProcessors.Load().(spanProcessorStates)
|
|
|
|
if !ok || len(old) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
new = append(new, old...)
|
|
|
|
|
|
|
|
// stop the span processor if it is started and remove it from the list
|
|
|
|
var stopOnce *spanProcessorState
|
|
|
|
var idx int
|
|
|
|
for i, sps := range new {
|
|
|
|
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() {
|
2019-10-22 22:19:11 +02:00
|
|
|
s.Shutdown()
|
|
|
|
})
|
|
|
|
}
|
2020-09-24 20:43:23 +02:00
|
|
|
if len(new) > 1 {
|
|
|
|
copy(new[idx:], new[idx+1:])
|
|
|
|
}
|
|
|
|
new[len(new)-1] = nil
|
|
|
|
new = new[:len(new)-1]
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
p.spanProcessors.Store(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
if cfg.MaxEventsPerSpan > 0 {
|
|
|
|
c.MaxEventsPerSpan = cfg.MaxEventsPerSpan
|
|
|
|
}
|
|
|
|
if cfg.MaxAttributesPerSpan > 0 {
|
|
|
|
c.MaxAttributesPerSpan = cfg.MaxAttributesPerSpan
|
|
|
|
}
|
|
|
|
if cfg.MaxLinksPerSpan > 0 {
|
|
|
|
c.MaxLinksPerSpan = cfg.MaxLinksPerSpan
|
|
|
|
}
|
2020-03-13 22:07:36 +02:00
|
|
|
if cfg.Resource != nil {
|
2020-04-23 21:10:58 +02:00
|
|
|
c.Resource = cfg.Resource
|
2020-03-13 22:07:36 +02:00
|
|
|
}
|
2019-10-22 22:19:11 +02:00
|
|
|
p.config.Store(&c)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|