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"
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2020-03-13 22:07:36 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2019-11-01 20:40:29 +02:00
|
|
|
apitrace "go.opentelemetry.io/otel/api/trace"
|
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
|
|
|
)
|
|
|
|
|
2019-11-02 01:35:18 +02:00
|
|
|
// batcher contains export.SpanBatcher and its options.
|
|
|
|
type batcher struct {
|
|
|
|
b export.SpanBatcher
|
|
|
|
opts []BatchSpanProcessorOption
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
// ProviderOptions
|
|
|
|
type ProviderOptions struct {
|
|
|
|
syncers []export.SpanSyncer
|
2019-11-02 01:35:18 +02:00
|
|
|
batchers []batcher
|
2019-10-22 22:19:11 +02:00
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProviderOption func(*ProviderOptions)
|
|
|
|
|
|
|
|
type Provider struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
namedTracer map[string]*tracer
|
|
|
|
spanProcessors atomic.Value
|
|
|
|
config atomic.Value // access atomically
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ apitrace.Provider = &Provider{}
|
|
|
|
|
|
|
|
// NewProvider 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.
|
|
|
|
func NewProvider(opts ...ProviderOption) (*Provider, error) {
|
|
|
|
o := &ProviderOptions{}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
tp := &Provider{
|
|
|
|
namedTracer: make(map[string]*tracer),
|
|
|
|
}
|
|
|
|
tp.config.Store(&Config{
|
2020-03-05 23:29:51 +02:00
|
|
|
DefaultSampler: AlwaysSample(),
|
2019-10-22 22:19:11 +02:00
|
|
|
IDGenerator: defIDGenerator(),
|
|
|
|
MaxAttributesPerSpan: DefaultMaxAttributesPerSpan,
|
|
|
|
MaxEventsPerSpan: DefaultMaxEventsPerSpan,
|
|
|
|
MaxLinksPerSpan: DefaultMaxLinksPerSpan,
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, syncer := range o.syncers {
|
|
|
|
ssp := NewSimpleSpanProcessor(syncer)
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, batcher := range o.batchers {
|
2019-11-02 01:35:18 +02:00
|
|
|
bsp, err := NewBatchSpanProcessor(batcher.b, batcher.opts...)
|
2019-10-22 22:19:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tp.RegisterSpanProcessor(bsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
tp.ApplyConfig(o.config)
|
|
|
|
|
|
|
|
return tp, nil
|
|
|
|
}
|
|
|
|
|
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.
|
2019-11-25 19:46:07 +02:00
|
|
|
func (p *Provider) Tracer(name string) apitrace.Tracer {
|
2019-10-22 22:19:11 +02:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
if name == "" {
|
|
|
|
name = defaultTracerName
|
|
|
|
}
|
|
|
|
t, ok := p.namedTracer[name]
|
|
|
|
if !ok {
|
|
|
|
t = &tracer{name: name, provider: p}
|
|
|
|
p.namedTracer[name] = t
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterSpanProcessor adds the given SpanProcessor to the list of SpanProcessors
|
|
|
|
func (p *Provider) RegisterSpanProcessor(s SpanProcessor) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
new := make(spanProcessorMap)
|
|
|
|
if old, ok := p.spanProcessors.Load().(spanProcessorMap); ok {
|
|
|
|
for k, v := range old {
|
|
|
|
new[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new[s] = &sync.Once{}
|
|
|
|
p.spanProcessors.Store(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors
|
|
|
|
func (p *Provider) UnregisterSpanProcessor(s SpanProcessor) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
new := make(spanProcessorMap)
|
|
|
|
if old, ok := p.spanProcessors.Load().(spanProcessorMap); ok {
|
|
|
|
for k, v := range old {
|
|
|
|
new[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if stopOnce, ok := new[s]; ok && stopOnce != nil {
|
|
|
|
stopOnce.Do(func() {
|
|
|
|
s.Shutdown()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
delete(new, s)
|
|
|
|
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.
|
|
|
|
func (p *Provider) ApplyConfig(cfg Config) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSyncer options appends the syncer to the existing list of Syncers.
|
|
|
|
// This option can be used multiple times.
|
|
|
|
// The Syncers are wrapped into SimpleSpanProcessors and registered
|
|
|
|
// with the provider.
|
|
|
|
func WithSyncer(syncer export.SpanSyncer) ProviderOption {
|
|
|
|
return func(opts *ProviderOptions) {
|
|
|
|
opts.syncers = append(opts.syncers, syncer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:49:02 +02:00
|
|
|
// WithBatcher options appends the batcher to the existing list of Batchers.
|
2019-10-22 22:19:11 +02:00
|
|
|
// This option can be used multiple times.
|
|
|
|
// The Batchers are wrapped into BatchedSpanProcessors and registered
|
|
|
|
// with the provider.
|
2019-11-02 01:35:18 +02:00
|
|
|
func WithBatcher(b export.SpanBatcher, bopts ...BatchSpanProcessorOption) ProviderOption {
|
2019-10-22 22:19:11 +02:00
|
|
|
return func(opts *ProviderOptions) {
|
2019-11-02 01:35:18 +02:00
|
|
|
opts.batchers = append(opts.batchers, batcher{b, bopts})
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithConfig option sets the configuration to provider.
|
|
|
|
func WithConfig(config Config) ProviderOption {
|
|
|
|
return func(opts *ProviderOptions) {
|
|
|
|
opts.config = config
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 22:07:36 +02:00
|
|
|
|
|
|
|
// WithResourceAttributes option sets the resource attributes to the provider.
|
|
|
|
// Resource is added to the span when it is started.
|
2020-05-14 01:06:03 +02:00
|
|
|
func WithResourceAttributes(attrs ...kv.KeyValue) ProviderOption {
|
2020-03-13 22:07:36 +02:00
|
|
|
return func(opts *ProviderOptions) {
|
|
|
|
opts.config.Resource = resource.New(attrs...)
|
|
|
|
}
|
|
|
|
}
|