Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set exception details when using WAI and Yesod instrumentation together #121

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions instrumentation/yesod/src/OpenTelemetry/Instrumentation/Yesod.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
Expand All @@ -22,6 +23,7 @@ module OpenTelemetry.Instrumentation.Yesod (
handlerEnvL,
) where

import Control.Monad (when)
import qualified Data.HashMap.Strict as H
import Data.List (intercalate)
import Data.Maybe (catMaybes)
Expand All @@ -32,8 +34,6 @@ import Language.Haskell.TH.Syntax
import Lens.Micro
import Network.Wai (requestHeaders)
import qualified OpenTelemetry.Context as Context
import OpenTelemetry.Context.ThreadLocal
import OpenTelemetry.Contrib.SpanTraversals
import OpenTelemetry.Instrumentation.Wai (requestContext)
import OpenTelemetry.SemanticsConfig
import OpenTelemetry.Trace.Core hiding (inSpan, inSpan', inSpan'')
Expand All @@ -57,7 +57,7 @@ rheSiteL = lens rheSite (\rhe new -> rhe {rheSite = new})
instance MonadTracer (HandlerFor site) where
getTracer = do
tp <- getGlobalTracerProvider
OpenTelemetry.Trace.Core.getTracer tp "hs-opentelemetry-instrumentation-yesod" tracerOptions
pure $ makeTracer tp "hs-opentelemetry-instrumentation-yesod" tracerOptions


{- | Template Haskell to generate a function named routeToRendererFunction.
Expand Down Expand Up @@ -181,11 +181,9 @@ renderPattern FlatResource {..} =
pieces -> pieces
, case frDispatch of
Methods {..} ->
concat
[ case methodsMulti of
Nothing -> []
Just t -> ["/+", t]
]
case methodsMulti of
Nothing -> []
Just t -> ["/+", t]
Subsite {} -> []
]
where
Expand Down Expand Up @@ -249,18 +247,29 @@ openTelemetryYesodMiddleware rr m = do
}
case mspan of
Nothing -> do
eResult <- inSpan' (maybe "notFound" (\r -> nameRender rr r) mr) args $ \_s -> do
eResult <- inSpan' (maybe "notFound" (nameRender rr) mr) args $ \_s -> do
catch (Right <$> m) $ \e -> do
-- We want to mark the span as an error if it's an InternalError,
-- the other HCError values are 4xx status codes which don't
-- really count as a server error in OpenTelemetry spec parlance.
case e of
HCError (InternalError _) -> throwIO e
_ -> pure ()
when (isInternalError e) $ throwIO e
pure (Left (e :: HandlerContents))
case eResult of
Left hc -> throwIO hc
Right normal -> pure normal
Just waiSpan -> do
addAttributes waiSpan sharedAttributes
m
withException m $ \ex -> do
when (shouldMarkException ex) $ do
recordException waiSpan mempty Nothing ex
setStatus waiSpan $ Error $ T.pack $ displayException ex


shouldMarkException :: SomeException -> Bool
shouldMarkException = maybe True isInternalError . fromException


-- We want to mark the span as an error if it's an InternalError, the other
-- HCError values are 4xx status codes which don't really count as a server
-- error in OpenTelemetry spec parlance.
isInternalError :: HandlerContents -> Bool
isInternalError = \case
HCError (InternalError _) -> True
_ -> False