...

Source file src/go.opentelemetry.io/otel/internal/global/propagator.go

Documentation: go.opentelemetry.io/otel/internal/global

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package global // import "go.opentelemetry.io/otel/internal/global"
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  
    21  	"go.opentelemetry.io/otel/propagation"
    22  )
    23  
    24  // textMapPropagator is a default TextMapPropagator that delegates calls to a
    25  // registered delegate if one is set, otherwise it defaults to delegating the
    26  // calls to a the default no-op propagation.TextMapPropagator.
    27  type textMapPropagator struct {
    28  	mtx      sync.Mutex
    29  	once     sync.Once
    30  	delegate propagation.TextMapPropagator
    31  	noop     propagation.TextMapPropagator
    32  }
    33  
    34  // Compile-time guarantee that textMapPropagator implements the
    35  // propagation.TextMapPropagator interface.
    36  var _ propagation.TextMapPropagator = (*textMapPropagator)(nil)
    37  
    38  func newTextMapPropagator() *textMapPropagator {
    39  	return &textMapPropagator{
    40  		noop: propagation.NewCompositeTextMapPropagator(),
    41  	}
    42  }
    43  
    44  // SetDelegate sets a delegate propagation.TextMapPropagator that all calls are
    45  // forwarded to. Delegation can only be performed once, all subsequent calls
    46  // perform no delegation.
    47  func (p *textMapPropagator) SetDelegate(delegate propagation.TextMapPropagator) {
    48  	if delegate == nil {
    49  		return
    50  	}
    51  
    52  	p.mtx.Lock()
    53  	p.once.Do(func() { p.delegate = delegate })
    54  	p.mtx.Unlock()
    55  }
    56  
    57  // effectiveDelegate returns the current delegate of p if one is set,
    58  // otherwise the default noop TextMapPropagator is returned. This method
    59  // can be called concurrently.
    60  func (p *textMapPropagator) effectiveDelegate() propagation.TextMapPropagator {
    61  	p.mtx.Lock()
    62  	defer p.mtx.Unlock()
    63  	if p.delegate != nil {
    64  		return p.delegate
    65  	}
    66  	return p.noop
    67  }
    68  
    69  // Inject set cross-cutting concerns from the Context into the carrier.
    70  func (p *textMapPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
    71  	p.effectiveDelegate().Inject(ctx, carrier)
    72  }
    73  
    74  // Extract reads cross-cutting concerns from the carrier into a Context.
    75  func (p *textMapPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
    76  	return p.effectiveDelegate().Extract(ctx, carrier)
    77  }
    78  
    79  // Fields returns the keys whose values are set with Inject.
    80  func (p *textMapPropagator) Fields() []string {
    81  	return p.effectiveDelegate().Fields()
    82  }
    83  

View as plain text