baggage
v1.47.0
Documentation
pkg.go.dev ↗Package baggage provides functionality for storing and retrieving baggage items in Go context. For propagating the baggage, see the go.opentelemetry.io/otel/propagation package.
Functions
ContextWithBaggage
func ContextWithBaggage(parent context.Context, b Baggage) context.Context
ContextWithBaggage returns a copy of parent with baggage.
ContextWithoutBaggage
func ContextWithoutBaggage(parent context.Context) context.Context
ContextWithoutBaggage returns a copy of parent with no baggage.
Types
Baggage
type Baggage struct {
// contains filtered or unexported fields
}
Baggage is a list of baggage members representing the baggage-string as defined by the W3C Baggage specification.
FromContext
func FromContext(ctx context.Context) Baggage
FromContext returns the baggage contained in ctx.
New
func New(members ...Member) (Baggage, error)
New returns a new valid Baggage. It returns an error if it results in a Baggage exceeding limits set in that specification.
If the resulting Baggage exceeds the maximum allowed members or bytes, members are dropped until the limits are satisfied and an error is returned along with the partial result.
It expects all the provided members to have already been validated.
Parse
func Parse(bStr string) (Baggage, error)
Parse attempts to decode a baggage-string from the passed string. It returns an error if the input is invalid according to the W3C Baggage specification.
If there are duplicate list-members contained in baggage, the last one defined (reading left-to-right) will be the only one kept. This diverges from the W3C Baggage specification which allows duplicate list-members, but conforms to the OpenTelemetry Baggage specification.
If the raw baggage-string exceeds the maximum allowed bytes (8192), an empty Baggage and an error are returned.
Otherwise, members are parsed left-to-right and accumulated until one of the following conditions is reached, at which point parsing stops and an error is returned alongside the partial result:
- accepting the next member would cause the encoded baggage to exceed 8192 bytes, or
- the baggage already contains 64 distinct keys.
Invalid members are skipped and the error is returned along with the partial result containing the valid members.
Baggage.DeleteMember
func (b Baggage) DeleteMember(key string) Baggage
DeleteMember returns a copy of the Baggage with the list-member identified by key removed.
Baggage.Len
func (b Baggage) Len() int
Len returns the number of list-members in the Baggage.
Baggage.Member
func (b Baggage) Member(key string) Member
Member returns the baggage list-member identified by key.
If there is no list-member matching the passed key the returned Member will be a zero-value Member. The returned member is not validated, as we assume the validation happened when it was added to the Baggage.
Baggage.Members
func (b Baggage) Members() []Member
Members returns all the baggage list-members. The order of the returned list-members is not significant.
The returned members are not validated, as we assume the validation happened when they were added to the Baggage.
Baggage.SetMember
func (b Baggage) SetMember(member Member) (Baggage, error)
SetMember returns a copy of the Baggage with the member included. If the baggage contains a Member with the same key, the existing Member is replaced.
If member is invalid according to the W3C Baggage specification, an error is returned with the original Baggage.
Baggage.String
func (b Baggage) String() string
String encodes Baggage into a header string compliant with the W3C Baggage specification. It would ignore members where the member key is invalid with the W3C Baggage specification. This could happen for a UTF-8 key, as it may contain invalid characters.
Member
type Member struct {
// contains filtered or unexported fields
}
Member is a list-member of a baggage-string as defined by the W3C Baggage specification.
NewMember
func NewMember(key, value string, props ...Property) (Member, error)
NewMember returns a new Member from the passed arguments.
The passed key must be compliant with W3C Baggage specification. The passed value must be percent-encoded as defined in W3C Baggage specification.
Notice: Consider using NewMemberRaw instead that does not require percent-encoding of the value.
NewMemberRaw
func NewMemberRaw(key, value string, props ...Property) (Member, error)
NewMemberRaw returns a new Member from the passed arguments.
The passed key must be valid, non-empty UTF-8 string. The passed value must be valid UTF-8 string. However, the specific Propagators that are used to transmit baggage entries across component boundaries may impose their own restrictions on baggage key. For example, the W3C Baggage specification restricts the baggage keys to strings that satisfy the token definition from RFC7230, Section 3.2.6. For maximum compatibility, alphanumeric value are strongly recommended to be used as baggage key.
Member.Key
func (m Member) Key() string
Key returns the Member key.
Member.Properties
func (m Member) Properties() []Property
Properties returns a copy of the Member properties.
Member.String
func (m Member) String() string
String encodes Member into a baggage header string. It would return empty string if the key is invalid with the W3C Baggage specification. This could happen for a UTF-8 key, as it may contain invalid characters.
Member.Value
func (m Member) Value() string
Value returns the Member value.
Property
type Property struct {
// contains filtered or unexported fields
}
Property is an additional metadata entry for a baggage list-member.
NewKeyProperty
func NewKeyProperty(key string) (Property, error)
NewKeyProperty returns a new Property for key.
The passed key must be valid, non-empty UTF-8 string. If key is invalid, an error will be returned. However, the specific Propagators that are used to transmit baggage entries across component boundaries may impose their own restrictions on Property key. For example, the W3C Baggage specification restricts the Property keys to strings that satisfy the token definition from RFC7230, Section 3.2.6. For maximum compatibility, alphanumeric value are strongly recommended to be used as Property key.
NewKeyValueProperty
func NewKeyValueProperty(key, value string) (Property, error)
NewKeyValueProperty returns a new Property for key with value.
The passed key must be compliant with W3C Baggage specification. The passed value must be percent-encoded as defined in W3C Baggage specification.
Notice: Consider using NewKeyValuePropertyRaw instead that does not require percent-encoding of the value.
NewKeyValuePropertyRaw
func NewKeyValuePropertyRaw(key, value string) (Property, error)
NewKeyValuePropertyRaw returns a new Property for key with value.
The passed key must be valid, non-empty UTF-8 string. The passed value must be valid UTF-8 string. However, the specific Propagators that are used to transmit baggage entries across component boundaries may impose their own restrictions on Property key. For example, the W3C Baggage specification restricts the Property keys to strings that satisfy the token definition from RFC7230, Section 3.2.6. For maximum compatibility, alphanumeric value are strongly recommended to be used as Property key.
Property.Key
func (p Property) Key() string
Key returns the Property key.
Property.String
func (p Property) String() string
String encodes Property into a header string compliant with the W3C Baggage specification. It would return empty string if the key is invalid with the W3C Baggage specification. This could happen for a UTF-8 key, as it may contain invalid characters.
Property.Value
func (p Property) Value() (string, bool)
Value returns the Property value. Additionally, a boolean value is returned indicating if the returned value is the empty if the Property has a value that is empty or if the value is not set.