hs-opentelemetry-api
Copyright(c) Ian Duncan 2021-2026
LicenseBSD-3
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

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

https://opentelemetry.io/docs/specs/otel/baggage/api/

Synopsis

Constructing Baggage structures

data Baggage Source #

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

Instances

Instances details
Semigroup Baggage Source # 
Instance details

Defined in OpenTelemetry.Baggage

Show Baggage Source # 
Instance details

Defined in OpenTelemetry.Baggage

Eq Baggage Source # 
Instance details

Defined in OpenTelemetry.Baggage

Methods

(==) :: Baggage -> Baggage -> Bool #

(/=) :: Baggage -> Baggage -> Bool #

empty :: Baggage Source #

An empty initial baggage value

Since: 0.0.1.0

fromHashMap :: HashMap Token Element -> Baggage Source #

Convert a HashMap into Baggage

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

data Token Source #

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

Instances

Instances details
Show Token Source # 
Instance details

Defined in OpenTelemetry.Baggage

Methods

showsPrec :: Int -> Token -> ShowS #

show :: Token -> String #

showList :: [Token] -> ShowS #

Eq Token Source # 
Instance details

Defined in OpenTelemetry.Baggage

Methods

(==) :: Token -> Token -> Bool #

(/=) :: Token -> Token -> Bool #

Ord Token Source # 
Instance details

Defined in OpenTelemetry.Baggage

Methods

compare :: Token -> Token -> Ordering #

(<) :: Token -> Token -> Bool #

(<=) :: Token -> Token -> Bool #

(>) :: Token -> Token -> Bool #

(>=) :: Token -> Token -> Bool #

max :: Token -> Token -> Token #

min :: Token -> Token -> Token #

Hashable Token Source # 
Instance details

Defined in OpenTelemetry.Baggage

Methods

hashWithSalt :: Int -> Token -> Int #

hash :: Token -> Int #

Lift Token Source # 
Instance details

Defined in OpenTelemetry.Baggage

Methods

lift :: Quote m => Token -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => Token -> Code m Token #

token :: QuasiQuoter Source #

Since: 0.0.1.0

mkToken :: Text -> Maybe Token Source #

Since: 0.0.1.0

tokenValue :: Token -> ByteString Source #

Convert a Token into a ByteString

Since: 0.0.1.0

data Element Source #

An entry into the baggage

Since: 0.0.1.0

Constructors

Element 

Fields

Instances

Instances details
Show Element Source # 
Instance details

Defined in OpenTelemetry.Baggage

Eq Element Source # 
Instance details

Defined in OpenTelemetry.Baggage

Methods

(==) :: Element -> Element -> Bool #

(/=) :: Element -> Element -> Bool #

element :: Text -> Element Source #

Since: 0.0.1.0

property :: Token -> Maybe Text -> Property Source #

Since: 0.0.1.0

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

insert Source #

Arguments

:: Token

The name for which to set the value

-> Element

The value to set. Use element to construct a well-formed element value.

-> 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:

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