...

Source file src/go.opentelemetry.io/otel/propagation/baggage.go

Documentation: go.opentelemetry.io/otel/propagation

     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 propagation // import "go.opentelemetry.io/otel/propagation"
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.opentelemetry.io/otel/baggage"
    21  )
    22  
    23  const baggageHeader = "baggage"
    24  
    25  // Baggage is a propagator that supports the W3C Baggage format.
    26  //
    27  // This propagates user-defined baggage associated with a trace. The complete
    28  // specification is defined at https://www.w3.org/TR/baggage/.
    29  type Baggage struct{}
    30  
    31  var _ TextMapPropagator = Baggage{}
    32  
    33  // Inject sets baggage key-values from ctx into the carrier.
    34  func (b Baggage) Inject(ctx context.Context, carrier TextMapCarrier) {
    35  	bStr := baggage.FromContext(ctx).String()
    36  	if bStr != "" {
    37  		carrier.Set(baggageHeader, bStr)
    38  	}
    39  }
    40  
    41  // Extract returns a copy of parent with the baggage from the carrier added.
    42  func (b Baggage) Extract(parent context.Context, carrier TextMapCarrier) context.Context {
    43  	bStr := carrier.Get(baggageHeader)
    44  	if bStr == "" {
    45  		return parent
    46  	}
    47  
    48  	bag, err := baggage.Parse(bStr)
    49  	if err != nil {
    50  		return parent
    51  	}
    52  	return baggage.ContextWithBaggage(parent, bag)
    53  }
    54  
    55  // Fields returns the keys who's values are set with Inject.
    56  func (b Baggage) Fields() []string {
    57  	return []string{baggageHeader}
    58  }
    59  

View as plain text