Baggage is used to attach log messages or debugging information to the context. It allows you to propagate key-value pairs across service boundaries alongside trace context.
Overview
Baggage provides a way to carry user-defined metadata across process boundaries. Unlike trace context, which is used for correlation, baggage is designed to carry application-specific data that needs to be available throughout a distributed transaction. References:Methods
get
None if this baggage contains no mapping for the key.
Returns: Some(entry) if the key exists, None otherwise
Example:
updated (with metadata option)
Baggage instance with the updated entry
updated (with metadata)
Baggage instance with the updated entry
Example:
updated (without metadata)
Baggage instance with the updated entry
Example:
removed
Baggage instance without the specified entry
Example:
size
isEmpty
asMap
Map containing all baggage entries keyed by their names
Example:
Factory Methods
empty
Baggage instance with no entries.
Example:
Nested Types
Baggage.Entry
Represents an entry that theBaggage holds associated with a key.
Properties
Factory Method
Entry with the specified value and optional metadata.
Baggage.Metadata
An opaque wrapper for a string that represents metadata for a baggage entry.Properties
Factory Method
Metadata instance wrapping the given string value.
Type Class Instances
Hash
Baggage has a Hash instance from Cats, allowing for equality comparisons and hashing based on the baggage’s entries.
Show
Baggage has a Show instance that formats the baggage as:
Usage Examples
Creating and updating baggage
Retrieving values
Removing entries
Iterating over entries
Working with metadata
Best Practices
- Keep baggage small: Baggage is propagated with every request, so keep the number and size of entries minimal
- Avoid sensitive data: Unless properly secured, baggage may be logged or exposed in headers
- Use metadata wisely: Metadata can indicate properties like whether data is sensitive or its purpose
- Immutability: All operations return new
Baggageinstances; the original is never modified