hs-opentelemetry-api
Copyright(c) Ian Duncan 2021-2026
LicenseBSD-3
MaintainerIan Duncan
Stabilityexperimental
Portabilitynon-portable (GHC extensions)
Safe HaskellNone
LanguageHaskell2010

OpenTelemetry.Trace.Core

Description

Overview

This module provides the core tracing API for creating and managing spans. Most application code should use OpenTelemetry.Trace (from the SDK package) for initialization and OpenTelemetry.Trace.Monad for a cleaner monadic interface. This module is useful when you need direct control or are writing instrumentation libraries.

Quick example

import OpenTelemetry.Trace.Core

-- Wrap any IO action in a span:
handleRequest :: Tracer -> Request -> IO Response
handleRequest tracer req =
  inSpan tracer "handleRequest" defaultSpanArguments $ do
    result <- processRequest req
    pure result

-- Access the span to add attributes:
fetchUser :: Tracer -> UserId -> IO User
fetchUser tracer uid =
  inSpan' tracer "fetchUser" defaultSpanArguments $ \span -> do
    addAttribute span "user.id" (toAttribute uid)
    user <- db_lookupUser uid
    addAttribute span "user.name" (toAttribute (userName user))
    pure user

Key concepts

TracerProvider
Factory that holds configuration (processors, exporters, samplers) and creates Tracers. Typically one per application, created at startup.
Tracer
Obtained from a TracerProvider, scoped to an instrumentation library or application component. Carries the library name and version for attribution.
Span
Represents a unit of work. Has a name, start/end timestamps, attributes, events, links, and status. Created by inSpan or createSpan.

Creating spans

The inSpan family of functions is the primary API:

  • inSpan: wraps an IO a action (or any MonadUnliftIO action), automatically ending the span and recording exceptions. Captures source location from the call site.
  • inSpan′: like inSpan, but passes the Span to the callback so you can add attributes or events during execution. (In Haskell source the name ends with one ASCII prime character.)
  • inSpan′′: raw variant with no automatic code.* attributes from the call site. Preferred for instrumentation libraries where those attributes would describe library internals rather than user code. (In Haskell source the name ends with two ASCII prime characters.)

For manual span lifecycle management, use createSpan and endSpan.

Adding metadata

inSpan' tracer "processOrder" defaultSpanArguments $ \span -> do
  addAttribute span "order.id" (toAttribute orderId)
  addAttributes span
    [ ("order.total", toAttribute total)
    , ("order.currency", toAttribute "USD")
    ]
  addEvent span (newEvent "order.validated")
  setStatus span Ok

Error handling

inSpan and inSpan′ automatically catch exceptions, record them on the span (as an exception event with stack trace), set the span status to Error, and re-throw. You can also manually set error status:

setStatus span (Error "payment declined")
recordException span mempty Nothing myException

Source location

inSpan, inSpan′, and createSpan automatically add source location attributes from GHC's HasCallStack. The attribute names depend on the OTEL_SEMCONV_STABILITY_OPT_IN setting:

  • Default (Old): code.function, code.namespace, code.filepath, code.lineno
  • code: code.function.name, code.file.path, code.line.number (stable semconv v1.33+)
  • code/dup: both old and stable names emitted

If you provide any code.* attribute yourself in SpanArguments, the automatic attributes are suppressed.

Spec reference

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

Synopsis

TracerProvider operations

data TracerProvider Source #

Factory for creating Tracer instances. Holds configuration shared across all tracers: processors, sampler, resource, limits, and propagators.

Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#tracerprovider

Since: 0.0.1.0

createTracerProvider :: MonadIO m => [SpanProcessor] -> TracerProviderOptions -> m TracerProvider Source #

Initialize a new tracer provider

You should generally use getGlobalTracerProvider for most applications.

Since: 0.0.1.0

shutdownTracerProvider Source #

Arguments

:: MonadIO m 
=> TracerProvider 
-> Maybe Int

Optional timeout in microseconds, defaults to 5,000,000 (5s)

-> m ShutdownResult 

This method provides a way for provider to do any cleanup required.

This will also trigger shutdowns on all internal processors.

Since: 0.0.1.0

worstShutdown :: ShutdownResult -> ShutdownResult -> ShutdownResult Source #

Combine two shutdown results, preferring the "worst" outcome. Failure > Timeout > Success.

Since: 0.4.0.0

forceFlushTracerProvider Source #

Arguments

:: MonadIO m 
=> TracerProvider 
-> Maybe Int

Optional timeout in microseconds, defaults to 5,000,000 (5s)

-> m FlushResult

Result that denotes whether the flush action succeeded, failed, or timed out.

This method provides a way for provider to immediately export all spans that have not yet been exported for all the internal processors.

Since: 0.0.1.0

data FlushResult Source #

The outcome of a call to OpenTelemetry.Trace.forceFlush or OpenTelemetry.Log.forceFlush

Since: 0.0.1.0

Constructors

FlushTimeout

One or more spans or LogRecords did not export from all associated exporters within the alotted timeframe.

FlushSuccess

Flushing spans or LogRecords to all associated exporters succeeded.

FlushError

One or more exporters failed to successfully export one or more unexported spans or LogRecords.

Instances

Instances details
Show FlushResult Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

Eq FlushResult Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

getGlobalTracerProvider :: MonadIO m => m TracerProvider Source #

Access the globally configured TracerProvider. Once the the global tracer provider is initialized via the OpenTelemetry SDK, Tracers created from this TracerProvider will export spans to their configured exporters. Prior to that, any Tracers acquired from the uninitialized TracerProvider will create no-op spans.

Since: 0.0.1.0

setGlobalTracerProvider :: MonadIO m => TracerProvider -> m () Source #

Overwrite the globally configured TracerProvider.

Tracers acquired from the previously installed TracerProvider will continue to use that TracerProviders configured span processors, exporters, and other settings.

Since: 0.0.1.0

emptyTracerProviderOptions :: TracerProviderOptions Source #

Options for creating a TracerProvider with invalid ids, no resources, default limits, and no propagators.

In effect, tracing is a no-op when using this configuration.

Since: 0.0.1.0

Tracer operations

data Tracer Source #

Creates and manages Spans for a specific instrumentation scope.

Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#tracer

Since: 0.0.1.0

Instances

Instances details
Show Tracer Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

tracerName :: Tracer -> InstrumentationLibrary Source #

Get the name of the Tracer

Since: 0.0.10

tracerIsEnabled :: Tracer -> Bool Source #

Check if the Tracer is enabled.

This function helps users avoid performing computationally expensive operations when creating Spans if the tracer is not enabled.

A Tracer is considered enabled if it has at least one configured processor. If the TracerProvider has no processors, all spans will be dropped, so the tracer is disabled.

Callers SHOULD invoke this before each span creation to get the most up-to-date response, as the result may change over time.

Since: 0.3.1.0

class HasTracer s where Source #

A small utility lens for extracting a Tracer from a larger data type

This will generally be most useful as a means of implementing getTracer

Since: 0.0.1.0

Methods

tracerL :: Lens' s Tracer Source #

makeTracer :: TracerProvider -> InstrumentationLibrary -> TracerOptions -> Tracer Source #

Construct a Tracer from a provider, library, and options.

Prefer a non-empty libraryName per the OpenTelemetry specification; use getTracer if you want a warning when the name is empty.

Since: 0.0.1.0

getTracer :: MonadIO m => TracerProvider -> InstrumentationLibrary -> TracerOptions -> m Tracer Source #

Like makeTracer but caches by InstrumentationLibrary, so repeated calls with the same scope return the same Tracer instance. Spec: implementations SHOULD return a single Tracer per InstrumentationScope.

Since: 0.0.1.0

data InstrumentationLibrary Source #

An instrumentation scope identifies the library or component providing instrumentation. The OpenTelemetry specification renamed this concept from "Instrumentation Library" to "Instrumentation Scope"; this type retains the old constructor name for backwards compatibility but InstrumentationScope is the preferred type alias.

Spec: https://opentelemetry.io/docs/specs/otel/common/instrumentation-scope/

Since: 0.0.1.0

Constructors

InstrumentationLibrary 

Fields

Instances

Instances details
IsString InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

Generic InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

Associated Types

type Rep InstrumentationLibrary 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

type Rep InstrumentationLibrary = D1 ('MetaData "InstrumentationLibrary" "OpenTelemetry.Internal.Common.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "InstrumentationLibrary" 'PrefixI 'True) ((S1 ('MetaSel ('Just "libraryName") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Just "libraryVersion") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Text)) :*: (S1 ('MetaSel ('Just "librarySchemaUrl") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Just "libraryAttributes") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Attributes))))
Show InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

Eq InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

Ord InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

Hashable InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

Lift InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

type Rep InstrumentationLibrary Source # 
Instance details

Defined in OpenTelemetry.Internal.Common.Types

type Rep InstrumentationLibrary = D1 ('MetaData "InstrumentationLibrary" "OpenTelemetry.Internal.Common.Types" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "InstrumentationLibrary" 'PrefixI 'True) ((S1 ('MetaSel ('Just "libraryName") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Just "libraryVersion") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Text)) :*: (S1 ('MetaSel ('Just "librarySchemaUrl") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Just "libraryAttributes") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Attributes))))

instrumentationLibrary :: Text -> Text -> InstrumentationLibrary Source #

Create an InstrumentationLibrary with a name and version. Schema URL and attributes default to empty.

Prefer instrumentationScope for new code.

Since: 0.4.0.0

detectInstrumentationLibrary :: (Quasi m, Quote m) => m Exp Source #

Works out the instrumentation library for your package.

Since: 0.0.1.0

data TracerOptions Source #

Tracer configuration options.

Since: 0.0.1.0

Constructors

TracerOptions 

Fields

  • tracerSchema :: Maybe Text

    OpenTelemetry provides a schema for describing common attributes so that backends can easily parse and identify relevant information. It is important to understand these conventions when writing instrumentation, in order to normalize your data and increase its utility.

    In particular, this option is valuable to set when possible, because it allows vendors to normalize data accross releases in order to account for attribute name changes.

  • tracerExceptionHandlerOptions :: [ExceptionHandler]

    Exception handlers specific to this tracer, consulted before provider-level handlers. Defaults to [].

    Since: 0.4.0.0

tracerOptions :: TracerOptions Source #

Default Tracer options

Since: 0.0.1.0

Span operations

data Span Source #

A single unit of work in a distributed trace.

A span carries a name, timestamps, attributes, events, links, and a SpanContext (trace ID + span ID). The three constructors represent:

  • Span: a live, recording span created by this process.
  • FrozenSpan: a non-recording wrapper around a remote SpanContext, used as a parent when continuing a trace from another process.
  • Dropped: a span that was not sampled. Carries enough context (trace ID, span ID) for propagation but records nothing.

Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span

Since: 0.0.1.0

Instances

Instances details
Show Span Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

Methods

showsPrec :: Int -> Span -> ShowS #

show :: Span -> String #

showList :: [Span] -> ShowS #

toImmutableSpan :: MonadIO m => Span -> m (Either FrozenOrDropped ImmutableSpan) Source #

Extracts the values from a Span if it is still mutable. Returns a Left with FrozenOrDropped if the Span is frozen or dropped.

Since: 0.0.1.0

data ImmutableSpan Source #

The representation of a Span for processors and exporters.

Cold (immutable) fields live directly in the record and are never copied. Hot (mutable) fields sit behind an IORef so that CAS operations only allocate a fresh SpanHot instead of the entire span.

Since: 0.0.1.0

Constructors

ImmutableSpan 

Fields

Instances

Instances details
Show ImmutableSpan Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

data SpanHot Source #

Mutable fields of a span, stored behind an IORef and updated via CAS. Only ~48 bytes, so each CAS allocates much less than copying the full span.

Since: 0.0.1.0

Instances

Instances details
Show SpanHot Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

data SpanContext Source #

The serializable portion of a Span: trace ID, span ID, trace flags, and trace state. Immutable once created.

Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#spancontext W3C: https://www.w3.org/TR/trace-context/

Since: 0.0.1.0

Instances

Instances details
Show SpanContext Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

Eq SpanContext Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

data TraceFlags Source #

Contain details about the trace. Unlike TraceState values, TraceFlags are present in all traces. The current version of the specification only supports a single flag called sampled.

Since: 0.0.1.0

traceFlagsValue :: TraceFlags -> Word8 Source #

Get the current bitmask for the TraceFlags, useful for serialization purposes.

Since: 0.0.1.0

traceFlagsFromWord8 :: Word8 -> TraceFlags Source #

Create a TraceFlags, from an arbitrary Word8. Note that for backwards-compatibility reasons, no checking is performed to determine whether the TraceFlags bitmask provided is valid.

Since: 0.0.1.0

defaultTraceFlags :: TraceFlags Source #

TraceFlags with the sampled flag not set. This means that it is up to the sampling configuration to decide whether or not to sample the trace.

Since: 0.0.1.0

isSampled :: TraceFlags -> Bool Source #

Will the trace associated with this TraceFlags value be sampled?

Since: 0.0.1.0

setSampled :: TraceFlags -> TraceFlags Source #

Set the sampled flag on the TraceFlags

Since: 0.0.1.0

unsetSampled :: TraceFlags -> TraceFlags Source #

Unset the sampled flag on the TraceFlags. This means that the application may choose whether or not to emit this Trace.

Since: 0.0.1.0

isRandom :: TraceFlags -> Bool Source #

Test the W3C Level 2 random flag (bit 1). When set, the trace-id has sufficient randomness for probabilistic sampling decisions.

See https://www.w3.org/TR/trace-context-2/#random-trace-id-flag.

Since: 0.4.0.0

setRandom :: TraceFlags -> TraceFlags Source #

Set the W3C Level 2 random flag (bit 1) on the TraceFlags. Indicates that the trace-id was generated with a CSPRNG or equivalent and has sufficient randomness for probabilistic sampling.

Since: 0.4.0.0

unsetRandom :: TraceFlags -> TraceFlags Source #

Unset the W3C Level 2 random flag (bit 1).

Since: 0.4.0.0

Creating Spans

inSpan Source #

Arguments

:: (MonadUnliftIO m, HasCallStack) 
=> Tracer 
-> Text

The name of the span. This may be updated later via updateName

-> SpanArguments

Additional options for creating the span, such as SpanKind, span links, starting attributes, etc.

-> m a

The action to perform. inSpan will record the time spent on the action without forcing strict evaluation of the result. Any uncaught exceptions will be recorded and rethrown.

-> m a 

The simplest function for annotating code with trace information.

Since: 0.0.1.0

inSpan' Source #

Arguments

:: (MonadUnliftIO m, HasCallStack) 
=> Tracer 
-> Text

The name of the span. This may be updated later via updateName

-> SpanArguments 
-> (Span -> m a) 
-> m a 

Like inSpan, but passes the created Span to the action.

Since: 0.0.1.0

inSpan'' Source #

Arguments

:: (MonadUnliftIO m, HasCallStack) 
=> Tracer 
-> Text

The name of the span. This may be updated later via updateName

-> SpanArguments 
-> (Span -> m a) 
-> m a 

Like inSpan′ (the Haskell name has one ASCII prime), but does not add automatic caller source location attributes.

Since: 0.4.0.0

createSpan Source #

Arguments

:: (MonadIO m, HasCallStack) 
=> Tracer

Tracer to create the span from. Associated Processors and Exporters will be used for the lifecycle of the created Span

-> Context

Context, potentially containing a parent span. If no existing parent (or context) exists, you can use empty.

-> Text

Span name

-> SpanArguments

Additional span information

-> m Span

The created span. Try and infer source code information unless the user has set any of the attributes already, which we take as an indication that our automatic strategy won't work well.

Create a Span.

If the provided Context has a span in it (inserted via insertSpan), that Span will be used as the parent of the Span created via this API.

Note: if the hs-opentelemetry-sdk or another SDK is not installed, all actions that use the created Spans produced will be no-ops.

Since: 0.0.1.0

createSpanWithoutCallStack Source #

Arguments

:: MonadIO m 
=> Tracer

Tracer to create the span from. Associated Processors and Exporters will be used for the lifecycle of the created Span

-> Context

Context, potentially containing a parent span. If no existing parent (or context) exists, you can use empty.

-> Text

Span name

-> SpanArguments

Additional span information

-> m Span

The created span.

The same thing as createSpan, except that it does not have a HasCallStack constraint.

Since: 0.0.1.0

wrapSpanContext :: SpanContext -> Span Source #

Wrap a SpanContext as a non-recording Span (FrozenSpan).

Since: 0.0.1.0

wrapDroppedContext :: SpanContext -> Span Source #

Construct a non-recording parent span representing a dropped (not sampled) trace, e.g. for tests or when continuing a trace whose parent was not recorded.

Since: 0.4.0.0

data SpanKind Source #

SpanKind describes the relationship between the Span, its parents, and its children in a Trace. SpanKind describes two independent properties that benefit tracing systems during analysis.

The first property described by SpanKind reflects whether the Span is a remote child or parent. Spans with a remote parent are interesting because they are sources of external load. Spans with a remote child are interesting because they reflect a non-local system dependency.

The second property described by SpanKind reflects whether a child Span represents a synchronous call. When a child span is synchronous, the parent is expected to wait for it to complete under ordinary circumstances. It can be useful for tracing systems to know this property, since synchronous Spans may contribute to the overall trace latency. Asynchronous scenarios can be remote or local.

In order for SpanKind to be meaningful, callers SHOULD arrange that a single Span does not serve more than one purpose. For example, a server-side span SHOULD NOT be used directly as the parent of another remote span. As a simple guideline, instrumentation should create a new Span prior to extracting and serializing the SpanContext for a remote call.

To summarize the interpretation of these kinds

SpanKindSynchronousAsynchronousRemote IncomingRemote Outgoing
Clientyesyes
Serveryesyes
Produceryesmaybe
Consumeryesmaybe
Internal

Since: 0.0.1.0

Constructors

Server

Indicates that the span covers server-side handling of a synchronous RPC or other remote request. This span is the child of a remote Client span that was expected to wait for a response.

Client

Indicates that the span describes a synchronous request to some remote service. This span is the parent of a remote Server span and waits for its response.

Producer

Indicates that the span describes the parent of an asynchronous request. This parent span is expected to end before the corresponding child Producer span, possibly even before the child span starts. In messaging scenarios with batching, tracing individual messages requires a new Producer span per message to be created.

Consumer

Indicates that the span describes the child of an asynchronous Producer request.

Internal

Default value. Indicates that the span represents an internal operation within an application, as opposed to an operations with remote parents or children.

Instances

Instances details
Show SpanKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

Eq SpanKind Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

defaultSpanArguments :: SpanArguments Source #

Smart constructor for SpanArguments providing reasonable values for most Spans created that are internal to an application.

Defaults:

Since: 0.0.1.0

data SpanArguments Source #

Non-name fields that may be set on initial creation of a Span.

Since: 0.0.1.0

Constructors

SpanArguments 

Fields

  • kind :: SpanKind

    The kind of the span. See SpanKinds documentation for the semantics of the various values that may be specified.

  • attributes :: AttributeMap

    An initial set of attributes set at Span creation time. Adding attributes at span creation is preferred to calling addAttribute later, because samplers can only consider information already present during span creation.

  • links :: [NewLink]

    A collection of Links that point to causally related Spans.

  • startTime :: Maybe Timestamp

    An explicit start time, if the span has already begun.

Recording Events

data Event Source #

A “log” that happens as part of a span. An operation that is too fast for its own span, but too unique to roll up into its parent span.

Events contain a name, a timestamp, and an optional set of Attributes, along with a timestamp. Events represent an event that occurred at a specific time within a span’s workload.

Since: 0.0.1.0

Constructors

Event 

Fields

Instances

Instances details
Show Event Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

Methods

showsPrec :: Int -> Event -> ShowS #

show :: Event -> String #

showList :: [Event] -> ShowS #

data NewEvent Source #

A “log” that happens as part of a span. An operation that is too fast for its own span, but too unique to roll up into its parent span.

Events contain a name, a timestamp, and an optional set of Attributes, along with a timestamp. Events represent an event that occurred at a specific time within a span’s workload.

When creating an event, this is the version that you will use. Attributes added that exceed the configured attribute limits will be dropped, which is accounted for in the Event structure.

Since: 0.0.1.0

Constructors

NewEvent 

Fields

addEvent :: MonadIO m => Span -> NewEvent -> m () Source #

Add an event to a recording span. Events will not be recorded for remote spans and dropped spans.

Since: 0.0.1.0

Enriching Spans with additional information

updateName Source #

Arguments

:: MonadIO m 
=> Span 
-> Text

The new span name, which supersedes whatever was passed in when the Span was started

-> m () 

Updates the Span name. Upon this update, any sampling behavior based on Span name will depend on the implementation.

Note that Samplers can only consider information already present during span creation. Any changes done later, including updated span name, cannot change their decisions.

Alternatives for the name update may be late Span creation, when Span is started with the explicit timestamp from the past at the moment where the final Span name is known, or reporting a Span with the desired name as a child Span.

Since: 0.0.1.0

addAttribute Source #

Arguments

:: (MonadIO m, ToAttribute a) 
=> Span

Span to add the attribute to

-> Text

Attribute name

-> a

Attribute value

-> m () 

Add an attribute to a span. Only affects recording spans.

See the OTel attribute naming conventions for guidance on choosing attribute names.

Since: 0.0.1.0

addAttributes :: MonadIO m => Span -> HashMap Text Attribute -> m () Source #

A convenience function related to addAttribute that adds multiple attributes to a span at the same time.

This function may be slightly more performant than repeatedly calling addAttribute.

Since: 0.0.1.0

addAttributes' :: MonadIO m => Span -> AttrsBuilder -> m () Source #

Like addAttributes, but takes an AttrsBuilder instead of a HashMap. More efficient when setting many attributes at once.

With typed AttributeKeys from semantic conventions:

addAttributes' span $
    SC.http_request_method .@ method
 <> SC.url_full .@ url
 <> SC.server_port .@? mPort

With plain Text keys:

addAttributes' span $
    attr "custom.key" value
 <> optAttr "custom.optional" mValue

Since: 0.4.1.0

spanGetAttributes :: MonadIO m => Span -> m Attributes Source #

This can be useful for pulling data for attributes and using it to copy / otherwise use the data to further enrich instrumentation.

Since: 0.0.1.0

data Attribute #

Instances

Instances details
Data Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Attribute -> c Attribute #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Attribute #

toConstr :: Attribute -> Constr #

dataTypeOf :: Attribute -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Attribute) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Attribute) #

gmapT :: (forall b. Data b => b -> b) -> Attribute -> Attribute #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Attribute -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Attribute -> r #

gmapQ :: (forall d. Data d => d -> u) -> Attribute -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Attribute -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Attribute -> m Attribute #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Attribute -> m Attribute #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Attribute -> m Attribute #

IsString Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Generic Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Associated Types

type Rep Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep Attribute = D1 ('MetaData "Attribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AttributeValue" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 PrimitiveAttribute)) :+: C1 ('MetaCons "AttributeArray" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 [PrimitiveAttribute])))
Read Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Show Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Eq Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Ord Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Hashable Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

FromAttribute Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

ToAttribute Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Lift Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Methods

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

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

type Rep Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep Attribute = D1 ('MetaData "Attribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AttributeValue" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 PrimitiveAttribute)) :+: C1 ('MetaCons "AttributeArray" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 [PrimitiveAttribute])))

class ToAttribute a where #

Minimal complete definition

Nothing

Methods

toAttribute :: a -> Attribute #

data PrimitiveAttribute #

Instances

Instances details
Data PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> PrimitiveAttribute -> c PrimitiveAttribute #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c PrimitiveAttribute #

toConstr :: PrimitiveAttribute -> Constr #

dataTypeOf :: PrimitiveAttribute -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c PrimitiveAttribute) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrimitiveAttribute) #

gmapT :: (forall b. Data b => b -> b) -> PrimitiveAttribute -> PrimitiveAttribute #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> PrimitiveAttribute -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> PrimitiveAttribute -> r #

gmapQ :: (forall d. Data d => d -> u) -> PrimitiveAttribute -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> PrimitiveAttribute -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> PrimitiveAttribute -> m PrimitiveAttribute #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> PrimitiveAttribute -> m PrimitiveAttribute #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> PrimitiveAttribute -> m PrimitiveAttribute #

IsString PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Generic PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Associated Types

type Rep PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep PrimitiveAttribute = D1 ('MetaData "PrimitiveAttribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) ((C1 ('MetaCons "TextAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "BoolAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Bool))) :+: (C1 ('MetaCons "DoubleAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Double)) :+: C1 ('MetaCons "IntAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Int64))))
Read PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Show PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Eq PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Ord PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Hashable PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

FromAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

FromPrimitiveAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

ToAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

ToPrimitiveAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Lift PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep PrimitiveAttribute = D1 ('MetaData "PrimitiveAttribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) ((C1 ('MetaCons "TextAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "BoolAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Bool))) :+: (C1 ('MetaCons "DoubleAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Double)) :+: C1 ('MetaCons "IntAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Int64))))

Attribute builder

data AttrsBuilder Source #

Church-encoded left fold over attribute key-value pairs. Avoids allocating intermediate tuples, list spines, or HashMaps when adding multiple attributes to a span.

Construct individual entries with attr / .@ and combine with <>. GHC can inline and fuse static builder expressions, eliminating all intermediate allocation.

addAttributes' span $
    SC.http_request_method .@ method
 <> SC.url_full .@ url
 <> SC.server_port .@? mPort

Since: 0.4.0.0

attr :: ToAttribute a => Text -> a -> AttrsBuilder Source #

Build an attribute entry from a Text key. The value is converted to Attribute lazily; actual conversion is deferred until the exporter thread reads the attribute, keeping the instrumented thread fast.

Since: 0.4.0.0

optAttr :: ToAttribute a => Text -> Maybe a -> AttrsBuilder Source #

Build an optional attribute entry. Nothing contributes nothing to the builder (zero cost).

Since: 0.4.0.0

(.@) :: ToAttribute a => AttributeKey a -> a -> AttrsBuilder infixl 8 Source #

Build an attribute entry from a typed AttributeKey. Type-safe: the value type must match the key's phantom type.

SC.http_request_method .@ (GET :: Text)

Since: 0.4.0.0

(.@?) :: ToAttribute a => AttributeKey a -> Maybe a -> AttrsBuilder infixl 8 Source #

Build an optional attribute entry from a typed AttributeKey. Nothing contributes nothing to the builder.

Since: 0.4.0.0

buildAttrs :: AttrsBuilder -> AttributeMap Source #

Materialize a builder into an AttributeMap. Useful when a raw HashMap is needed (e.g. for NewEvent attributes or SpanArguments).

Since: 0.4.0.0

data Link Source #

Frozen (immutable) version of NewLink, stored on completed spans. Created internally by freezing a NewLink; attributes are truncated per the configured limits. See NewLink for full documentation on link semantics.

Since: 0.0.1.0

Constructors

Link 

Fields

Instances

data NewLink Source #

This is a link that is being added to a span which is going to be created.

A Span may be linked to zero or more other Spans (defined by SpanContext) that are causally related. Links can point to Spans inside a single Trace or across different Traces. Links can be used to represent batched operations where a Span was initiated by multiple initiating Spans, each representing a single incoming item being processed in the batch.

Another example of using a Link is to declare the relationship between the originating and following trace. This can be used when a Trace enters trusted boundaries of a service and service policy requires the generation of a new Trace rather than trusting the incoming Trace context. The new linked Trace may also represent a long running asynchronous data processing operation that was initiated by one of many fast incoming requests.

When using the scatter/gather (also called fork/join) pattern, the root operation starts multiple downstream processing operations and all of them are aggregated back in a single Span. This last Span is linked to many operations it aggregates. All of them are the Spans from the same Trace. And similar to the Parent field of a Span. It is recommended, however, to not set parent of the Span in this scenario as semantically the parent field represents a single parent scenario, in many cases the parent Span fully encloses the child Span. This is not the case in scatter/gather and batch scenarios.

Since: 0.0.1.0

Constructors

NewLink 

Fields

Instances

addLink :: MonadIO m => Span -> NewLink -> m () Source #

Add a link to a recording span.

Since: 0.0.1.0

Recording error information

recordException :: (MonadIO m, Exception e) => Span -> AttributeMap -> Maybe Timestamp -> e -> m () Source #

A specialized variant of addEvent that records attributes conforming to the OpenTelemetry specification's semantic conventions

Since: 0.0.1.0

recordError :: (MonadIO m, Exception e) => Span -> e -> m () Source #

Record an error and set the span status in one call.

Combines setStatus with Error and recordException. This is a common pattern when handling errors outside of inSpan (which does this automatically for uncaught exceptions).

case result of
  Left err -> recordError span err
  Right _  -> setStatus span Ok

Since: 0.4.1.0

setStatus :: MonadIO m => Span -> SpanStatus -> m () Source #

Sets the Status of the Span. If used, this will override the default Span status, which is Unset.

These values form a total order: Ok > Error > Unset. This means that setting Status with StatusCode=Ok will override any prior or future attempts to set span Status with StatusCode=Error or StatusCode=Unset.

Since: 0.0.1.0

data SpanStatus Source #

The status of a Span. This may be used to indicate the successful completion of a span.

The default is Unset.

Ok is final: once a span's status is Ok, subsequent setStatus calls are ignored. Error can be set from Unset, and Ok can override Error (or Unset). Transitions: Unset -> Error, Unset -> Ok, Error -> Ok.

An Ord instance is provided (Ok > Error > Unset) for convenience but does not govern status-setting precedence.

Since: 0.0.1.0

Constructors

Unset

The default status.

Error Text

The operation contains an error. The text field may be empty, or else provide a description of the error.

Ok

The operation has been validated by an Application developer or Operator to have completed successfully.

Instances

Instances details
Show SpanStatus Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

Eq SpanStatus Source # 
Instance details

Defined in OpenTelemetry.Internal.Trace.Types

Ord SpanStatus Source #

Ok > Error > Unset. This ordering is for sorting/display; status-setting precedence is handled by mergeStatus in OpenTelemetry.Trace.Core.

Instance details

Defined in OpenTelemetry.Internal.Trace.Types

Exception handling

data ExceptionClassification Source #

How an exception should be treated by the tracing system when caught by inSpan and similar bracket-style functions.

Since: 0.4.0.0

Constructors

ErrorException

Set span status to Error, record an exception event. This is the default behavior for all exceptions.

RecordedException

Record an exception event on the span, but do not set the span status to Error. Useful for exceptions that represent expected control flow (e.g. a cache miss exception) that you still want visibility into.

IgnoredException

Do not record an exception event and do not set the span status to Error. The exception is completely invisible to the tracing system. Useful for ExitSuccess, AsyncCancelled, and similar non-error exceptions.

data ExceptionResponse Source #

The result of classifying an exception via an ExceptionHandler.

Since: 0.4.0.0

Constructors

ExceptionResponse 

Fields

type ExceptionHandler = SomeException -> Maybe ExceptionResponse Source #

A function that inspects a SomeException and optionally classifies it.

Returns Nothing to indicate this handler does not recognize the exception, deferring to the next handler in the chain. Returns Just ExceptionResponse to provide a classification and optional extra attributes.

Multiple handlers are chained: tracer-level handlers are consulted first, then provider-level handlers. The first Just result wins. If all handlers return Nothing, the default behavior (ErrorException with no extra attributes) applies.

Since: 0.4.0.0

defaultExceptionResponse :: ExceptionResponse Source #

The default response when no handler matches: classify as ErrorException with no additional attributes.

Since: 0.4.0.0

resolveException :: Tracer -> SomeException -> ExceptionResponse Source #

Resolve exception classification by consulting tracer-level handlers first, then provider-level handlers. Returns defaultExceptionResponse if no handler matches.

Since: 0.4.0.0

Completing Spans

endSpan Source #

Arguments

:: MonadIO m 
=> Span 
-> Maybe Timestamp

Optional Timestamp signalling the end time of the span. If not provided, the current time will be used.

-> m () 

Signals that the operation described by this span has now (or at the time optionally specified) ended.

This does have any effects on child spans. Those may still be running and can be ended later.

This also does not inactivate the Span in any Context it is active in. It is still possible to use an ended span as parent via a Context it is contained in. Also, putting the Span into a Context will still work after the Span was ended.

Since: 0.0.1.0

Accessing other Span information

getSpanContext :: MonadIO m => Span -> m SpanContext Source #

When sending tracing information across process boundaries, the SpanContext is used to serialize the relevant information.

Since: 0.0.1.0

isRecording :: MonadIO m => Span -> m Bool Source #

Returns whether the Span is currently recording.

A live Span created by this process returns True until endSpan is called. A FrozenSpan (non-recording context-only wrapper, e.g. from wrapSpanContext) and a Dropped span always return False.

Since: 0.0.1.0

isValid :: SpanContext -> Bool Source #

Returns True if the SpanContext has a non-zero TraceID and a non-zero SpanID. Spec: "true if the SpanContext has a non-zero TraceID and a non-zero SpanID".

Since: 0.0.1.0

spanIsRemote :: MonadIO m => Span -> m Bool Source #

Returns True if the SpanContext was propagated from a remote parent,

When extracting a SpanContext through the Propagators API, isRemote MUST return True, whereas for the SpanContext of any child spans it MUST return False.

Since: 0.0.1.0

Active span

getActiveSpan :: MonadIO m => m (Maybe Span) Source #

Retrieve the active Span from the current thread's context.

Returns Nothing if there is no span in the current context (e.g. at the top level, before any tracing has started).

This is the Haskell equivalent of Go's trace.SpanFromContext(ctx) and Rust's get_active_span.

Since: 0.4.1.0

withActiveSpan :: MonadIO m => (Span -> m ()) -> m () Source #

Run an action on the active span. If there is no active span in the current context, the action is silently skipped.

withActiveSpan $ \span -> do
  addAttribute span "user.id" (toAttribute userId)
  addEvent span (newEvent "cache-miss")

Since: 0.4.1.0

getActiveSpanContext :: MonadIO m => m (Maybe SpanContext) Source #

Retrieve the SpanContext of the active span, useful for log correlation (extracting trace/span IDs) without needing the full Span handle.

Since: 0.4.1.0

Event constructors

newEvent :: Text -> NewEvent Source #

Construct a NewEvent with just a name (no attributes, current timestamp).

addEvent span (newEvent "cache-miss")

Since: 0.4.1.0

newEventWith :: Text -> AttributeMap -> NewEvent Source #

Construct a NewEvent with a name and attributes (current timestamp).

addEvent span (newEventWith "retry" [("attempt", toAttribute retryCount)])

Since: 0.4.1.0

Utilities

data Timestamp Source #

Wall-clock timestamp stored as nanoseconds since Unix epoch. Matches the OTLP wire format directly (fixed64 nanoseconds).

Since: 0.0.1.0

getTimestamp :: MonadIO m => m Timestamp Source #

Sometimes, you may have a more accurate notion of when a traced operation has ended. In this case you may call getTimestamp, and then supply endSpan with the more accurate timestamp you have acquired.

When using the monadic interface, (such as inSpan, you may call endSpan early to record the information, and the first call to endSpan will be honored.

Since: 0.0.1.0

timestampNanoseconds :: Timestamp -> Word64 Source #

Nanoseconds since the Unix epoch.

Since: 0.0.1.0

unsafeReadSpan :: MonadIO m => Span -> m ImmutableSpan Source #

Really only intended for tests, this function does not conform to semantic versioning .

Since: 0.0.1.0

whenSpanIsRecording :: MonadIO m => Span -> m () -> m () Source #

Run an action only when the span is recording. Use this to guard expensive attribute computation that would be wasted on non-recording spans.

Since: 0.0.1.0

ownCodeAttributes :: HasCallStack => AttributeMap Source #

Creates source code attributes describing the caller of the current function. You should use this if you are getting source code attributes from inside a function that is creating a span.

Respects OTEL_SEMCONV_STABILITY_OPT_IN=code to select stable vs legacy attribute names.

Note: this will return nothing if the call stack is frozen.

callerAttributes :: HasCallStack => AttributeMap Source #

Creates source code attributes describing where the current function is called. You should use this if you are getting source code attributes from inside a "span creation" function.

Respects OTEL_SEMCONV_STABILITY_OPT_IN=code to select stable vs legacy attribute names.

Note: this will return nothing if the call stack is frozen.

addAttributesToSpanArguments :: AttributeMap -> SpanArguments -> SpanArguments Source #

Attributes are added to the end of the span argument list, so will be discarded if the number of attributes in the span exceeds the limit.

Limits

bracketError :: MonadUnliftIO m => m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c Source #

Like bracket, but provides the after function with information about uncaught exceptions.

Since: 0.1.0.0