| Copyright | (c) Ian Duncan 2024 |
|---|---|
| License | BSD-3 |
| Maintainer | Ian Duncan |
| Stability | experimental |
| Portability | non-portable (GHC extensions) |
| Safe Haskell | None |
| Language | Haskell2010 |
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
- data ExceptionClassification
- data ExceptionResponse = ExceptionResponse {}
- type ExceptionHandler = SomeException -> Maybe ExceptionResponse
- defaultExceptionResponse :: ExceptionResponse
- ignoreExceptionType :: Exception e => ExceptionHandler
- ignoreExceptionMatching :: Exception e => (e -> Bool) -> ExceptionHandler
- recordExceptionType :: Exception e => ExceptionHandler
- recordExceptionMatching :: Exception e => (e -> Bool) -> ExceptionHandler
- classifyException :: Exception e => (e -> ExceptionResponse) -> ExceptionHandler
- exitSuccessHandler :: ExceptionHandler
- resolveException :: Tracer -> SomeException -> ExceptionResponse
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 |
| RecordedException | Record an exception event on the span, but do not set the span status
to |
| IgnoredException | Do not record an exception event and do not set the span status to
|
Instances
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
to provide a classification and optional extra attributes.Just ExceptionResponse
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