forked from mtth/tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Trace a dynamically dispatched effect (#14)
* Make Trace a dynamically dispatched effect After some thinking I decided that `Trace` should be dynamic since currently it has two interpretations crammed into it based on whether the `Scope` is there or not (another design flaw of the original library). I also renamed `Tracing` to `Trace` to keep naming consistent with TraceT and MonadTrace. I also don't think `addSpanEntry` should do anything in the no-op handler. * Rename * Remove withZipkin and runZipkinTrace withZipkin is Monitor.Tracing.Zipkin.with and runZipkinTrace is somewhat unnecessary.
- Loading branch information
Showing
3 changed files
with
65 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{-# LANGUAGE UndecidableInstances #-} | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
module Effectful.Trace | ||
( | ||
-- * Effect | ||
Trace | ||
|
||
-- * Handler | ||
, runTrace | ||
, runNoTrace | ||
|
||
-- * Reexport | ||
, module Monitor.Tracing | ||
) where | ||
|
||
import Control.Monad.Trace | ||
import Control.Monad.Trace.Class | ||
import Effectful | ||
import Effectful.Dispatch.Dynamic | ||
import Effectful.Reader.Static | ||
import Monitor.Tracing | ||
|
||
-- | Provides the ability to send traces to a backend. | ||
data Trace :: Effect where | ||
Trace :: Builder -> m a -> Trace m a | ||
ActiveSpan :: Trace m (Maybe Span) | ||
AddSpanEntry :: Key -> Value -> Trace m () | ||
|
||
type instance DispatchOf Trace = Dynamic | ||
|
||
-- | Run the 'Trace' effect. | ||
-- | ||
-- /Note:/ this is the @effectful@ version of 'runTraceT'. | ||
runTrace | ||
:: IOE :> es | ||
=> Tracer | ||
-> Eff (Trace : es) a | ||
-> Eff es a | ||
runTrace tracer = reinterpret (runReader initialScope) $ \env -> \case | ||
Trace bldr action -> localSeqUnlift env $ \unlift -> do | ||
scope <- ask | ||
withSeqEffToIO $ \runInIO -> do | ||
traceWith bldr scope $ \childScope -> do | ||
runInIO . local (const childScope) $ unlift action | ||
ActiveSpan -> asks scopeSpan | ||
AddSpanEntry key value -> do | ||
scope <- ask | ||
liftIO $ addSpanEntryWith (Just scope) key value | ||
where | ||
initialScope = Scope tracer Nothing Nothing Nothing | ||
|
||
-- | Run the 'Trace' effect with a dummy handler that does no tracing. | ||
runNoTrace :: IOE :> es => Eff (Trace : es) a -> Eff es a | ||
runNoTrace = interpret $ \env -> \case | ||
Trace _ action -> localSeqUnlift env $ \unlift -> unlift action | ||
ActiveSpan -> pure Nothing | ||
AddSpanEntry _ _ -> pure () | ||
|
||
-- | Orphan, canonical instance. | ||
instance Trace :> es => MonadTrace (Eff es) where | ||
trace bldr action = send (Trace bldr action) | ||
activeSpan = send ActiveSpan | ||
addSpanEntry key value = send (AddSpanEntry key value) |
This file was deleted.
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