-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
237 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
{-# LANGUAGE ScopedTypeVariables, OverloadedLists #-} | ||
module PostgREST.OpenTelemetry (withTracer) where | ||
|
||
import OpenTelemetry.Trace (InstrumentationLibrary (..), Tracer, | ||
initializeGlobalTracerProvider, | ||
makeTracer, shutdownTracerProvider, | ||
tracerOptions, SpanArguments (..), Span, createSpanWithoutCallStack, setStatus, SpanStatus (..), recordException, endSpan) | ||
import PostgREST.Version (prettyVersion) | ||
import qualified Data.HashMap.Strict as H | ||
import Protolude | ||
import OpenTelemetry.Attributes (Attribute, ToAttribute (..)) | ||
import qualified Data.Text as T | ||
import Data.String (String) | ||
import GHC.Stack | ||
import OpenTelemetry.Context.ThreadLocal (getContext, adjustContext) | ||
import OpenTelemetry.Context (insertSpan, lookupSpan, removeSpan) | ||
import qualified Control.Exception as EUnsafe | ||
|
||
|
||
withTracer :: Text -> (Tracer -> IO c) -> IO c | ||
withTracer label f = bracket | ||
initializeGlobalTracerProvider | ||
shutdownTracerProvider | ||
(\tracerProvider -> f $ makeTracer tracerProvider instrumentationLibrary tracerOptions) | ||
where | ||
instrumentationLibrary = InstrumentationLibrary {libraryName = label, libraryVersion = decodeUtf8 prettyVersion} | ||
|
||
|
||
ownCodeAttributes :: (HasCallStack) => H.HashMap Text Attribute | ||
ownCodeAttributes = case getCallStack callStack of | ||
_ : caller : _ -> srcAttributes caller | ||
_ -> mempty | ||
|
||
|
||
callerAttributes :: (HasCallStack) => H.HashMap Text Attribute | ||
callerAttributes = case getCallStack callStack of | ||
_ : _ : caller : _ -> srcAttributes caller | ||
_ -> mempty | ||
|
||
|
||
srcAttributes :: (String, GHC.Stack.SrcLoc) -> H.HashMap Text Attribute | ||
srcAttributes (fn, loc) = H.fromList | ||
[ ("code.function", toAttribute $ T.pack fn) | ||
, ("code.namespace", toAttribute $ T.pack $ srcLocModule loc) | ||
, ("code.filepath", toAttribute $ T.pack $ srcLocFile loc) | ||
, ("code.lineno", toAttribute $ srcLocStartLine loc) | ||
, ("code.package", toAttribute $ T.pack $ srcLocPackage loc) | ||
] | ||
|
||
{- | The simplest function for annotating code with trace information. | ||
@since 0.0.1.0 | ||
-} | ||
inSpan | ||
:: (MonadIO 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 | ||
inSpan t n args m = inSpan'' t n (args {attributes = H.union (attributes args) callerAttributes}) (const m) | ||
|
||
|
||
inSpan' | ||
:: (MonadIO m, HasCallStack) | ||
=> Tracer | ||
-> Text | ||
-- ^ The name of the span. This may be updated later via 'updateName' | ||
-> SpanArguments | ||
-> (Span -> m a) | ||
-> m a | ||
inSpan' t n args = inSpan'' t n (args {attributes = H.union (attributes args) callerAttributes}) | ||
|
||
|
||
inSpan'' | ||
:: (MonadIO m, HasCallStack) | ||
=> Tracer | ||
-> Text | ||
-- ^ The name of the span. This may be updated later via 'updateName' | ||
-> SpanArguments | ||
-> (Span -> m a) | ||
-> m a | ||
inSpan'' t n args f = do | ||
bracketError | ||
( liftIO $ do | ||
ctx <- getContext | ||
s <- createSpanWithoutCallStack t ctx n args | ||
adjustContext (insertSpan s) | ||
pure (lookupSpan ctx, s) | ||
) | ||
( \e (parent, s) -> liftIO $ do | ||
forM_ e $ \(SomeException inner) -> do | ||
setStatus s $ Error $ T.pack $ displayException inner | ||
recordException s [("exception.escaped", toAttribute True)] Nothing inner | ||
endSpan s Nothing | ||
adjustContext $ \ctx -> | ||
maybe (removeSpan ctx) (`insertSpan` ctx) parent | ||
) | ||
(\(_, s) -> f s) | ||
|
||
{- | Like 'Context.Exception.bracket', but provides the @after@ function with information about | ||
uncaught exceptions. | ||
@since 0.1.0.0 | ||
-} | ||
bracketError :: (MonadIO m) => m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c | ||
bracketError before after thing = withRunInIO $ \run -> EUnsafe.mask $ \restore -> do | ||
x <- run before | ||
res1 <- EUnsafe.try $ restore $ run $ thing x | ||
case res1 of | ||
Left (e1 :: SomeException) -> do | ||
-- explicitly ignore exceptions from after. We know that | ||
-- no async exceptions were thrown there, so therefore | ||
-- the stronger exception must come from thing | ||
-- | ||
-- https://github.com/fpco/safe-exceptions/issues/2 | ||
_ :: Either SomeException b <- | ||
EUnsafe.try $ EUnsafe.uninterruptibleMask_ $ run $ after (Just e1) x | ||
EUnsafe.throwIO e1 | ||
Right y -> do | ||
_ <- EUnsafe.uninterruptibleMask_ $ run $ after Nothing x | ||
return y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters