| Copyright | (c) Ian Duncan 2021-2026 |
|---|---|
| License | BSD-3 |
| Stability | experimental |
| Safe Haskell | None |
| Language | Haskell2010 |
OpenTelemetry.Context
Description
Overview
A Context carries trace state (the current Span) and Baggage across
API boundaries and through your application. You rarely interact with it
directly; the inSpan functions and propagators manage it for you.
When you need Context directly
- Custom propagation: extracting/injecting context from non-HTTP transports
- Manual span parenting: creating a span as a child of a specific context
- Baggage access: reading or modifying baggage entries
Quick example
import OpenTelemetry.Context import OpenTelemetry.Context.ThreadLocal -- Read the current span from thread-local context: ctx <- getContext case lookupSpan ctx of Just span -> addAttribute span "custom.key" (toAttribute "value") Nothing -> pure () -- Attach baggage to the current context: adjustContext (insertBaggage myBaggage)
Thread-local context
In most applications, context is stored in a thread-local variable managed
by OpenTelemetry.Context.ThreadLocal. The inSpan functions automatically
push and pop spans from this thread-local context.
Spec reference
Synopsis
- data Key a
- newKey :: MonadIO m => Text -> m (Key a)
- data Context
- class HasContext s where
- empty :: Context
- lookup :: Key a -> Context -> Maybe a
- insert :: Key a -> a -> Context -> Context
- adjust :: (a -> a) -> Key a -> Context -> Context
- delete :: Key a -> Context -> Context
- union :: Context -> Context -> Context
- insertSpan :: Span -> Context -> Context
- lookupSpan :: Context -> Maybe Span
- removeSpan :: Context -> Context
- insertBaggage :: Baggage -> Context -> Context
- lookupBaggage :: Context -> Maybe Baggage
- removeBaggage :: Context -> Context
Documentation
Keys are used to allow cross-cutting concerns to control access to their local state. They are unique such that other libraries which may use the same context cannot accidentally use the same key. It is recommended that concerns mediate data access via an API, rather than provide direct public access to their keys.
A Context is a propagation mechanism which carries execution-scoped values
across API boundaries and between logically associated execution units.
Cross-cutting concerns access their data in-process using the same shared
Context object.
The span and baggage slots use UMaybe (unboxed sums) for zero-indirection access when UNPACKed into the Context closure. All other keys go through the vault.
removeSpan :: Context -> Context Source #
Since: 0.0.1.0
removeBaggage :: Context -> Context Source #
Since: 0.0.1.0