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

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

https://opentelemetry.io/docs/specs/otel/context/

Synopsis

Documentation

data Key a Source #

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.

newKey :: MonadIO m => Text -> m (Key a) Source #

Since: 0.0.1.0

data Context Source #

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.

Instances

Instances details
Monoid Context Source # 
Instance details

Defined in OpenTelemetry.Context.Types

Semigroup Context Source # 
Instance details

Defined in OpenTelemetry.Context.Types

class HasContext s where Source #

Since: 0.0.1.0

Methods

contextL :: Lens' s Context Source #

empty :: Context Source #

Since: 0.0.1.0

lookup :: Key a -> Context -> Maybe a Source #

Since: 0.0.1.0

insert :: Key a -> a -> Context -> Context Source #

Since: 0.0.1.0

adjust :: (a -> a) -> Key a -> Context -> Context Source #

Since: 0.0.1.0

delete :: Key a -> Context -> Context Source #

Since: 0.0.1.0

union :: Context -> Context -> Context Source #

Since: 0.0.1.0

insertSpan :: Span -> Context -> Context Source #

Since: 0.0.1.0

lookupSpan :: Context -> Maybe Span Source #

Since: 0.0.1.0

removeSpan :: Context -> Context Source #

Since: 0.0.1.0

insertBaggage :: Baggage -> Context -> Context Source #

Since: 0.0.1.0

removeBaggage :: Context -> Context Source #

Since: 0.0.1.0