2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 07:05:28 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-03-23 22:41:10 -07:00
|
|
|
|
2020-11-13 16:34:24 +01:00
|
|
|
package propagation // import "go.opentelemetry.io/otel/propagation"
|
2020-02-20 19:31:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-06-08 15:06:37 +00:00
|
|
|
"go.opentelemetry.io/otel/baggage"
|
2020-02-20 19:31:21 +01:00
|
|
|
)
|
|
|
|
|
2020-10-20 10:51:17 -07:00
|
|
|
const baggageHeader = "baggage"
|
2020-02-20 19:31:21 +01:00
|
|
|
|
2020-10-05 08:25:09 -07:00
|
|
|
// Baggage is a propagator that supports the W3C Baggage format.
|
|
|
|
//
|
|
|
|
// This propagates user-defined baggage associated with a trace. The complete
|
2021-11-23 14:50:38 -08:00
|
|
|
// specification is defined at https://www.w3.org/TR/baggage/.
|
2020-09-09 14:13:37 -04:00
|
|
|
type Baggage struct{}
|
2020-02-20 19:31:21 +01:00
|
|
|
|
2020-11-13 16:34:24 +01:00
|
|
|
var _ TextMapPropagator = Baggage{}
|
2020-02-20 19:31:21 +01:00
|
|
|
|
2020-10-05 08:25:09 -07:00
|
|
|
// Inject sets baggage key-values from ctx into the carrier.
|
2020-11-13 16:34:24 +01:00
|
|
|
func (b Baggage) Inject(ctx context.Context, carrier TextMapCarrier) {
|
2021-06-08 15:06:37 +00:00
|
|
|
bStr := baggage.FromContext(ctx).String()
|
|
|
|
if bStr != "" {
|
|
|
|
carrier.Set(baggageHeader, bStr)
|
2020-02-20 19:31:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 08:25:09 -07:00
|
|
|
// Extract returns a copy of parent with the baggage from the carrier added.
|
2020-11-13 16:34:24 +01:00
|
|
|
func (b Baggage) Extract(parent context.Context, carrier TextMapCarrier) context.Context {
|
2021-06-08 15:06:37 +00:00
|
|
|
bStr := carrier.Get(baggageHeader)
|
|
|
|
if bStr == "" {
|
2020-10-05 08:25:09 -07:00
|
|
|
return parent
|
2020-02-20 19:31:21 +01:00
|
|
|
}
|
|
|
|
|
2021-06-08 15:06:37 +00:00
|
|
|
bag, err := baggage.Parse(bStr)
|
|
|
|
if err != nil {
|
|
|
|
return parent
|
2020-07-09 15:02:49 -04:00
|
|
|
}
|
2021-06-08 15:06:37 +00:00
|
|
|
return baggage.ContextWithBaggage(parent, bag)
|
2020-02-20 19:31:21 +01:00
|
|
|
}
|
|
|
|
|
2020-10-02 12:27:16 -07:00
|
|
|
// Fields returns the keys who's values are set with Inject.
|
|
|
|
func (b Baggage) Fields() []string {
|
2020-09-09 14:13:37 -04:00
|
|
|
return []string{baggageHeader}
|
2020-02-20 19:31:21 +01:00
|
|
|
}
|