| Copyright | (c) Ian Duncan 2021-2026 |
|---|---|
| License | BSD-3 |
| Stability | experimental |
| Safe Haskell | None |
| Language | Haskell2010 |
OpenTelemetry.Baggage
Description
Overview
Baggage is a set of key-value pairs that propagate alongside trace context
across service boundaries (typically via the baggage HTTP header). Use it
to pass metadata like tenant IDs, feature flags, or routing hints through
your distributed system.
Baggage is not for span-specific annotations. Use addAttribute
for that.
Quick example
import OpenTelemetry.Baggage
-- Create baggage:
let bag = insert [token|tenant-id|] (element "abc123")
$ insert [token|region|] (element "us-east-1")
$ empty
-- Encode for HTTP propagation:
let headerValue = encodeBaggageHeader bag
-- "tenant-id=abc123,region=us-east-1"
-- Decode from an incoming header:
case decodeBaggageHeader headerBytes of
Right bag -> -- use the baggage
Left err -> -- malformed header
Thread-local baggage
Use the functions in OpenTelemetry.Context.ThreadLocal to get/set baggage on the current thread:
import OpenTelemetry.Context.ThreadLocal (getContext, adjustContext) import OpenTelemetry.Context (insertBaggage, lookupBaggage) -- Read: mbag <- lookupBaggage <$> getContext -- Write: adjustContext (insertBaggage myBaggage)
Limits
W3C Baggage specification enforces:
- Max 8192 bytes total serialized size
- Max 4096 bytes per member
- Max 180 members
insertChecked validates these limits and returns Left InvalidBaggage
on violation.
Spec reference
Synopsis
- data Baggage
- empty :: Baggage
- fromHashMap :: HashMap Token Element -> Baggage
- values :: Baggage -> HashMap Token Element
- data Token
- token :: QuasiQuoter
- mkToken :: Text -> Maybe Token
- tokenValue :: Token -> ByteString
- data Element = Element {
- value :: Text
- properties :: [Property]
- element :: Text -> Element
- property :: Token -> Maybe Text -> Property
- data InvalidBaggage
- maxBaggageBytes :: Int
- maxMemberBytes :: Int
- maxMembers :: Int
- insert :: Token -> Element -> Baggage -> Baggage
- insertChecked :: Token -> Element -> Baggage -> Either InvalidBaggage Baggage
- delete :: Token -> Baggage -> Baggage
- getValue :: Token -> Baggage -> Maybe Text
- encodeBaggageHeader :: Baggage -> ByteString
- encodeBaggageHeaderB :: Baggage -> Builder
- decodeBaggageHeader :: ByteString -> Either String Baggage
Constructing Baggage structures
Baggage is used to annotate telemetry, adding context and information to metrics, traces, and logs. It is a set of name/value pairs describing user-defined properties. Each name in Baggage is associated with exactly one value.
Since: 0.0.1.0
values :: Baggage -> HashMap Token Element Source #
Returns the namevalue pairs in the Baggage. The order of namevalue pairs
is not significant.
Since: 0.0.1.0
A key for a baggage entry, restricted to the set of valid characters
specified in the token definition of RFC 2616:
https://www.rfc-editor.org/rfc/rfc2616#section-2.2
Since: 0.0.1.0
token :: QuasiQuoter Source #
Since: 0.0.1.0
tokenValue :: Token -> ByteString Source #
Convert a Token into a ByteString
Since: 0.0.1.0
An entry into the baggage
Since: 0.0.1.0
Constructors
| Element | |
Fields
| |
Instances
data InvalidBaggage Source #
Since: 0.0.1.0
Constructors
| BaggageTooLong | |
| MemberTooLong | |
| TooManyListMembers | |
| Empty |
Instances
| Show InvalidBaggage Source # | |
Defined in OpenTelemetry.Baggage Methods showsPrec :: Int -> InvalidBaggage -> ShowS # show :: InvalidBaggage -> String # showList :: [InvalidBaggage] -> ShowS # | |
| Eq InvalidBaggage Source # | |
Defined in OpenTelemetry.Baggage Methods (==) :: InvalidBaggage -> InvalidBaggage -> Bool # (/=) :: InvalidBaggage -> InvalidBaggage -> Bool # | |
Limits (W3C Baggage specification)
maxBaggageBytes :: Int Source #
W3C Baggage: max 8192 bytes total, max 180 members, max 4096 bytes per member
Since: 0.0.1.0
maxMemberBytes :: Int Source #
W3C Baggage: max 8192 bytes total, max 180 members, max 4096 bytes per member
Since: 0.0.1.0
maxMembers :: Int Source #
W3C Baggage: max 8192 bytes total, max 180 members, max 4096 bytes per member
Since: 0.0.1.0
Modifying Baggage
Arguments
| :: Token | The name for which to set the value |
| -> Element | The value to set. Use |
| -> Baggage | |
| -> Baggage |
Since: 0.0.1.0
insertChecked :: Token -> Element -> Baggage -> Either InvalidBaggage Baggage Source #
Insert a key/value pair into the baggage with W3C limit enforcement.
Returns Left InvalidBaggage if adding the entry would violate:
TooManyListMembers: exceeds 180 entries (W3C ABNF max)BaggageTooLong: serialized header would exceed 8192 bytes
Since: 0.4.0.0
delete :: Token -> Baggage -> Baggage Source #
Delete a key/value pair from the baggage.
Since: 0.0.1.0
Querying Baggage
getValue :: Token -> Baggage -> Maybe Text Source #
Look up a baggage value by name.
Per the spec, this takes a name and returns the associated value, or
Nothing if the name is not present in the baggage.
Since: 0.4.0.0
Encoding and decoding Baggage
encodeBaggageHeader :: Baggage -> ByteString Source #
Since: 0.0.1.0
encodeBaggageHeaderB :: Baggage -> Builder Source #
Since: 0.0.1.0
decodeBaggageHeader :: ByteString -> Either String Baggage Source #
Since: 0.0.1.0