module OpenTelemetry.Context.ThreadLocal.Propagation (
propagateContext,
tracedForkIO,
tracedAsync,
tracedWithAsync,
tracedConcurrently,
tracedMapConcurrently,
tracedForConcurrently,
) where
import Control.Concurrent (ThreadId, forkIO)
import Control.Concurrent.Async (Async, async, concurrently, forConcurrently, mapConcurrently, withAsync)
import OpenTelemetry.Context (Context)
import OpenTelemetry.Context.ThreadLocal (attachContext, getContext)
propagateContext :: IO a -> IO (IO a)
propagateContext :: forall a. IO a -> IO (IO a)
propagateContext IO a
action = do
ctx <- IO Context
forall (m :: * -> *). MonadIO m => m Context
getContext
pure (installContext ctx >> action)
{-# INLINE propagateContext #-}
tracedForkIO :: IO () -> IO ThreadId
tracedForkIO :: IO () -> IO ThreadId
tracedForkIO IO ()
action = do
wrapped <- IO () -> IO (IO ())
forall a. IO a -> IO (IO a)
propagateContext IO ()
action
forkIO wrapped
{-# INLINE tracedForkIO #-}
tracedAsync :: IO a -> IO (Async a)
tracedAsync :: forall a. IO a -> IO (Async a)
tracedAsync IO a
action = do
wrapped <- IO a -> IO (IO a)
forall a. IO a -> IO (IO a)
propagateContext IO a
action
async wrapped
{-# INLINE tracedAsync #-}
tracedWithAsync :: IO a -> (Async a -> IO b) -> IO b
tracedWithAsync :: forall a b. IO a -> (Async a -> IO b) -> IO b
tracedWithAsync IO a
action Async a -> IO b
k = do
wrapped <- IO a -> IO (IO a)
forall a. IO a -> IO (IO a)
propagateContext IO a
action
withAsync wrapped k
{-# INLINE tracedWithAsync #-}
tracedConcurrently :: IO a -> IO b -> IO (a, b)
tracedConcurrently :: forall a b. IO a -> IO b -> IO (a, b)
tracedConcurrently IO a
left IO b
right = do
l <- IO a -> IO (IO a)
forall a. IO a -> IO (IO a)
propagateContext IO a
left
r <- propagateContext right
concurrently l r
{-# INLINE tracedConcurrently #-}
tracedMapConcurrently :: (Traversable t) => (a -> IO b) -> t a -> IO (t b)
tracedMapConcurrently :: forall (t :: * -> *) a b.
Traversable t =>
(a -> IO b) -> t a -> IO (t b)
tracedMapConcurrently a -> IO b
f t a
ta = do
ctx <- IO Context
forall (m :: * -> *). MonadIO m => m Context
getContext
mapConcurrently (\a
a -> Context -> IO ()
installContext Context
ctx IO () -> IO b -> IO b
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> IO b
f a
a) ta
{-# INLINE tracedMapConcurrently #-}
tracedForConcurrently :: (Traversable t) => t a -> (a -> IO b) -> IO (t b)
tracedForConcurrently :: forall (t :: * -> *) a b.
Traversable t =>
t a -> (a -> IO b) -> IO (t b)
tracedForConcurrently t a
ta a -> IO b
f = do
ctx <- IO Context
forall (m :: * -> *). MonadIO m => m Context
getContext
forConcurrently ta (\a
a -> Context -> IO ()
installContext Context
ctx IO () -> IO b -> IO b
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> IO b
f a
a)
{-# INLINE tracedForConcurrently #-}
installContext :: Context -> IO ()
installContext :: Context -> IO ()
installContext Context
ctx = do
_ <- Context -> IO Token
forall (m :: * -> *). MonadIO m => Context -> m Token
attachContext Context
ctx
pure ()
{-# INLINE installContext #-}