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

OpenTelemetry.Trace.ExceptionHandler

Description

Register exception handlers on a TracerProvider or Tracer to control how exceptions interact with span status and events.

By default, every exception caught by inSpan sets the span status to Error and records an exception event. This module provides combinators to override that behavior for specific exception types. For example, treating ExitSuccess or AsyncCancelled as non-errors.

Since: 0.4.0.0

Synopsis

Types

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

Smart constructors

ignoreExceptionType :: Exception e => ExceptionHandler Source #

Ignore all exceptions of the given type. They will not be recorded as events and will not set the span status to Error.

import Control.Exception (AsyncCancelled)

myHandlers = [ignoreExceptionType @AsyncCancelled]

Since: 0.4.0.0

ignoreExceptionMatching :: Exception e => (e -> Bool) -> ExceptionHandler Source #

Ignore exceptions of the given type that match a predicate.

ignoreExceptionMatching @ExitCode (== ExitSuccess)

Since: 0.4.0.0

recordExceptionType :: Exception e => ExceptionHandler Source #

Record exceptions of the given type as events but do not set the span status to Error. The exception remains visible in traces but is not counted as an error.

Since: 0.4.0.0

recordExceptionMatching :: Exception e => (e -> Bool) -> ExceptionHandler Source #

Record exceptions of the given type matching a predicate as events, without setting Error status.

Since: 0.4.0.0

classifyException :: Exception e => (e -> ExceptionResponse) -> ExceptionHandler Source #

Full control: inspect an exception and return a classification with optional extra attributes. Use this to enrich spans with domain-specific information extracted from the exception.

classifyException @HttpException $ \(HttpException status _body) ->
  ExceptionResponse ErrorException
    (HashMap.fromList [("http.response.status_code", toAttribute (statusCode status))])

Since: 0.4.0.0

Common handlers

exitSuccessHandler :: ExceptionHandler Source #

Handler that classifies ExitSuccess as ignored. ExitFailure is left unhandled (falls through to the next handler or the default).

This is a common handler to register globally:

opts = emptyTracerProviderOptions
  { tracerProviderOptionsExceptionHandlers = [exitSuccessHandler]
  }

Since: 0.4.0.0

Resolution

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