{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module OpenTelemetry.Trace.Core (
TracerProvider,
createTracerProvider,
shutdownTracerProvider,
forceFlushTracerProvider,
getTracerProviderResources,
getTracerProviderPropagators,
getGlobalTracerProvider,
setGlobalTracerProvider,
emptyTracerProviderOptions,
TracerProviderOptions (..),
Tracer,
tracerName,
HasTracer (..),
makeTracer,
getTracer,
getImmutableSpanTracer,
getTracerTracerProvider,
InstrumentationLibrary (..),
TracerOptions (..),
tracerOptions,
Span,
ImmutableSpan (..),
SpanContext (..),
TraceFlags,
traceFlagsValue,
traceFlagsFromWord8,
defaultTraceFlags,
isSampled,
setSampled,
unsetSampled,
inSpan,
inSpan',
inSpan'',
createSpan,
createSpanWithoutCallStack,
wrapSpanContext,
SpanKind (..),
defaultSpanArguments,
SpanArguments (..),
NewLink (..),
Link (..),
Event (..),
NewEvent (..),
addEvent,
updateName,
OpenTelemetry.Trace.Core.addAttribute,
OpenTelemetry.Trace.Core.addAttributes,
spanGetAttributes,
Attribute (..),
ToAttribute (..),
PrimitiveAttribute (..),
ToPrimitiveAttribute (..),
recordException,
setStatus,
SpanStatus (..),
endSpan,
getSpanContext,
isRecording,
isValid,
spanIsRemote,
Timestamp,
getTimestamp,
timestampNanoseconds,
unsafeReadSpan,
whenSpanIsRecording,
SpanLimits (..),
defaultSpanLimits,
bracketError,
) where
import Control.Applicative
import Control.Concurrent (myThreadId)
import Control.Concurrent.Async
import Control.Exception (Exception (..), SomeException (..), try)
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.IO.Unlift
import Data.Coerce
import Data.IORef
import Data.Maybe (fromMaybe, isJust, isNothing)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Typeable
import qualified Data.Vector as V
import Data.Word (Word64)
import GHC.Stack
import Network.HTTP.Types
import OpenTelemetry.Attributes
import qualified OpenTelemetry.Attributes as A
import OpenTelemetry.Common
import OpenTelemetry.Context
import OpenTelemetry.Context.ThreadLocal
import OpenTelemetry.Internal.Trace.Types
import qualified OpenTelemetry.Internal.Trace.Types as Types
import OpenTelemetry.Logging.Core (Log)
import OpenTelemetry.Propagator (Propagator)
import OpenTelemetry.Resource
import OpenTelemetry.Trace.Id
import OpenTelemetry.Trace.Id.Generator
import OpenTelemetry.Trace.Id.Generator.Dummy
import OpenTelemetry.Trace.Sampler
import qualified OpenTelemetry.Trace.TraceState as TraceState
import OpenTelemetry.Util
import System.Clock
import System.IO.Unsafe
import System.Timeout (timeout)
createSpan ::
(MonadIO m, HasCallStack) =>
Tracer ->
Context ->
Text ->
SpanArguments ->
m Span
createSpan :: forall (m :: * -> *).
(MonadIO m, HasCallStack) =>
Tracer -> Context -> Text -> SpanArguments -> m Span
createSpan Tracer
t Context
c Text
n SpanArguments
args = do
forall (m :: * -> *).
MonadIO m =>
Tracer -> Context -> Text -> SpanArguments -> m Span
createSpanWithoutCallStack Tracer
t Context
c Text
n forall a b. (a -> b) -> a -> b
$ case CallStack -> [([Char], SrcLoc)]
getCallStack HasCallStack => CallStack
callStack of
[] -> SpanArguments
args
([Char]
_, SrcLoc
loc) : [([Char], SrcLoc)]
rest ->
let addFunction :: [(Text, Attribute)] -> [(Text, Attribute)]
addFunction = case [([Char], SrcLoc)]
rest of
([Char]
fn, SrcLoc
_) : [([Char], SrcLoc)]
_ -> ((Text
"code.function", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack [Char]
fn) forall a. a -> [a] -> [a]
:)
[] -> forall a. a -> a
id
in SpanArguments
args
{ attributes :: [(Text, Attribute)]
attributes =
[(Text, Attribute)] -> [(Text, Attribute)]
addFunction forall a b. (a -> b) -> a -> b
$
(Text
"code.namespace", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ SrcLoc -> [Char]
srcLocModule SrcLoc
loc)
forall a. a -> [a] -> [a]
: (Text
"code.filepath", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ SrcLoc -> [Char]
srcLocFile SrcLoc
loc)
forall a. a -> [a] -> [a]
: (Text
"code.lineno", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ SrcLoc -> Int
srcLocStartLine SrcLoc
loc)
forall a. a -> [a] -> [a]
: (Text
"code.package", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ SrcLoc -> [Char]
srcLocPackage SrcLoc
loc)
forall a. a -> [a] -> [a]
: SpanArguments -> [(Text, Attribute)]
attributes SpanArguments
args
}
createSpanWithoutCallStack ::
MonadIO m =>
Tracer ->
Context ->
Text ->
SpanArguments ->
m Span
createSpanWithoutCallStack :: forall (m :: * -> *).
MonadIO m =>
Tracer -> Context -> Text -> SpanArguments -> m Span
createSpanWithoutCallStack Tracer
t Context
ctxt Text
n args :: SpanArguments
args@SpanArguments {[(Text, Attribute)]
[NewLink]
Maybe Timestamp
SpanKind
startTime :: SpanArguments -> Maybe Timestamp
links :: SpanArguments -> [NewLink]
kind :: SpanArguments -> SpanKind
startTime :: Maybe Timestamp
links :: [NewLink]
attributes :: [(Text, Attribute)]
kind :: SpanKind
attributes :: SpanArguments -> [(Text, Attribute)]
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
SpanId
sId <- forall (m :: * -> *). MonadIO m => IdGenerator -> m SpanId
newSpanId forall a b. (a -> b) -> a -> b
$ TracerProvider -> IdGenerator
tracerProviderIdGenerator forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t
let parent :: Maybe Span
parent = Context -> Maybe Span
lookupSpan Context
ctxt
TraceId
tId <- case Maybe Span
parent of
Maybe Span
Nothing -> forall (m :: * -> *). MonadIO m => IdGenerator -> m TraceId
newTraceId forall a b. (a -> b) -> a -> b
$ TracerProvider -> IdGenerator
tracerProviderIdGenerator forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t
Just (Span IORef ImmutableSpan
s) ->
SpanContext -> TraceId
traceId forall b c a. (b -> c) -> (a -> b) -> a -> c
. ImmutableSpan -> SpanContext
Types.spanContext forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IORef a -> IO a
readIORef IORef ImmutableSpan
s
Just (FrozenSpan SpanContext
s) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ SpanContext -> TraceId
traceId SpanContext
s
Just (Dropped SpanContext
s) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ SpanContext -> TraceId
traceId SpanContext
s
if forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall a b. (a -> b) -> a -> b
$ TracerProvider -> Vector Processor
tracerProviderProcessors forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t
then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ SpanContext -> Span
Dropped forall a b. (a -> b) -> a -> b
$ TraceFlags
-> Bool -> TraceId -> SpanId -> TraceState -> SpanContext
SpanContext TraceFlags
defaultTraceFlags Bool
False TraceId
tId SpanId
sId TraceState
TraceState.empty
else do
(SamplingResult
samplingOutcome, [(Text, Attribute)]
attrs, TraceState
samplingTraceState) <- case Maybe Span
parent of
Just (Dropped SpanContext
_) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (SamplingResult
Drop, [], TraceState
TraceState.empty)
Maybe Span
_ ->
Sampler
-> Context
-> TraceId
-> Text
-> SpanArguments
-> IO (SamplingResult, [(Text, Attribute)], TraceState)
shouldSample
(TracerProvider -> Sampler
tracerProviderSampler forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t)
Context
ctxt
TraceId
tId
Text
n
SpanArguments
args
let ctxtForSpan :: SpanContext
ctxtForSpan =
SpanContext
{ traceFlags :: TraceFlags
traceFlags = case SamplingResult
samplingOutcome of
SamplingResult
Drop -> TraceFlags
defaultTraceFlags
SamplingResult
RecordOnly -> TraceFlags
defaultTraceFlags
SamplingResult
RecordAndSample -> TraceFlags -> TraceFlags
setSampled TraceFlags
defaultTraceFlags
, isRemote :: Bool
isRemote = Bool
False
, traceState :: TraceState
traceState = TraceState
samplingTraceState
, spanId :: SpanId
spanId = SpanId
sId
, traceId :: TraceId
traceId = TraceId
tId
}
mkRecordingSpan :: IO Span
mkRecordingSpan = do
Timestamp
st <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *). MonadIO m => m Timestamp
getTimestamp forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Timestamp
startTime
ThreadId
tid <- IO ThreadId
myThreadId
let additionalInfo :: [(Text, Attribute)]
additionalInfo = [(Text
"thread.id", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ ThreadId -> Int
getThreadId ThreadId
tid)]
is :: ImmutableSpan
is =
ImmutableSpan
{ spanName :: Text
spanName = Text
n
, spanContext :: SpanContext
spanContext = SpanContext
ctxtForSpan
, spanParent :: Maybe Span
spanParent = Maybe Span
parent
, spanKind :: SpanKind
spanKind = SpanKind
kind
, spanAttributes :: Attributes
spanAttributes =
forall a.
ToAttribute a =>
AttributeLimits -> Attributes -> [(Text, a)] -> Attributes
A.addAttributes
(Tracer -> (SpanLimits -> Maybe Int) -> AttributeLimits
limitBy Tracer
t SpanLimits -> Maybe Int
spanAttributeCountLimit)
Attributes
emptyAttributes
(forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[(Text, Attribute)]
additionalInfo, [(Text, Attribute)]
attrs, [(Text, Attribute)]
attributes])
, spanLinks :: FrozenBoundedCollection Link
spanLinks =
let limitedLinks :: Int
limitedLinks = forall a. a -> Maybe a -> a
fromMaybe Int
128 (SpanLimits -> Maybe Int
linkCountLimit forall a b. (a -> b) -> a -> b
$ TracerProvider -> SpanLimits
tracerProviderSpanLimits forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t)
in forall (f :: * -> *) a.
Foldable f =>
Int -> f a -> FrozenBoundedCollection a
frozenBoundedCollection Int
limitedLinks forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NewLink -> Link
freezeLink [NewLink]
links
, spanEvents :: AppendOnlyBoundedCollection Event
spanEvents = forall a. Int -> AppendOnlyBoundedCollection a
emptyAppendOnlyBoundedCollection forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe Int
128 (SpanLimits -> Maybe Int
eventCountLimit forall a b. (a -> b) -> a -> b
$ TracerProvider -> SpanLimits
tracerProviderSpanLimits forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t)
, spanStatus :: SpanStatus
spanStatus = SpanStatus
Unset
, spanStart :: Timestamp
spanStart = Timestamp
st
, spanEnd :: Maybe Timestamp
spanEnd = forall a. Maybe a
Nothing
, spanTracer :: Tracer
spanTracer = Tracer
t
}
IORef ImmutableSpan
s <- forall a. a -> IO (IORef a)
newIORef ImmutableSpan
is
Either SomeException ()
eResult <- forall e a. Exception e => IO a -> IO (Either e a)
try forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\Processor
processor -> Processor -> IORef ImmutableSpan -> Context -> IO ()
processorOnStart Processor
processor IORef ImmutableSpan
s Context
ctxt) forall a b. (a -> b) -> a -> b
$ TracerProvider -> Vector Processor
tracerProviderProcessors forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t
case Either SomeException ()
eResult of
Left SomeException
err -> forall a. Show a => a -> IO ()
print (SomeException
err :: SomeException)
Right ()
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ IORef ImmutableSpan -> Span
Span IORef ImmutableSpan
s
case SamplingResult
samplingOutcome of
SamplingResult
Drop -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ SpanContext -> Span
Dropped SpanContext
ctxtForSpan
SamplingResult
RecordOnly -> IO Span
mkRecordingSpan
SamplingResult
RecordAndSample -> IO Span
mkRecordingSpan
where
freezeLink :: NewLink -> Link
freezeLink :: NewLink -> Link
freezeLink NewLink {[(Text, Attribute)]
SpanContext
linkAttributes :: NewLink -> [(Text, Attribute)]
linkContext :: NewLink -> SpanContext
linkAttributes :: [(Text, Attribute)]
linkContext :: SpanContext
..} =
Link
{ frozenLinkContext :: SpanContext
frozenLinkContext = SpanContext
linkContext
, frozenLinkAttributes :: Attributes
frozenLinkAttributes = forall a.
ToAttribute a =>
AttributeLimits -> Attributes -> [(Text, a)] -> Attributes
A.addAttributes (Tracer -> (SpanLimits -> Maybe Int) -> AttributeLimits
limitBy Tracer
t SpanLimits -> Maybe Int
linkAttributeCountLimit) Attributes
A.emptyAttributes [(Text, Attribute)]
linkAttributes
}
inSpan ::
(MonadUnliftIO m, HasCallStack) =>
Tracer ->
Text ->
SpanArguments ->
m a ->
m a
inSpan :: forall (m :: * -> *) a.
(MonadUnliftIO m, HasCallStack) =>
Tracer -> Text -> SpanArguments -> m a -> m a
inSpan Tracer
t Text
n SpanArguments
args m a
m = forall (m :: * -> *) a.
(MonadUnliftIO m, HasCallStack) =>
Tracer
-> CallStack -> Text -> SpanArguments -> (Span -> m a) -> m a
inSpan'' Tracer
t HasCallStack => CallStack
callStack Text
n SpanArguments
args (forall a b. a -> b -> a
const m a
m)
inSpan' ::
(MonadUnliftIO m, HasCallStack) =>
Tracer ->
Text ->
SpanArguments ->
(Span -> m a) ->
m a
inSpan' :: forall (m :: * -> *) a.
(MonadUnliftIO m, HasCallStack) =>
Tracer -> Text -> SpanArguments -> (Span -> m a) -> m a
inSpan' Tracer
t = forall (m :: * -> *) a.
(MonadUnliftIO m, HasCallStack) =>
Tracer
-> CallStack -> Text -> SpanArguments -> (Span -> m a) -> m a
inSpan'' Tracer
t HasCallStack => CallStack
callStack
inSpan'' ::
(MonadUnliftIO m, HasCallStack) =>
Tracer ->
CallStack ->
Text ->
SpanArguments ->
(Span -> m a) ->
m a
inSpan'' :: forall (m :: * -> *) a.
(MonadUnliftIO m, HasCallStack) =>
Tracer
-> CallStack -> Text -> SpanArguments -> (Span -> m a) -> m a
inSpan'' Tracer
t CallStack
cs Text
n SpanArguments
args Span -> m a
f = do
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c
bracketError
( forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Context
ctx <- forall (m :: * -> *). MonadIO m => m Context
getContext
Span
s <- forall (m :: * -> *).
MonadIO m =>
Tracer -> Context -> Text -> SpanArguments -> m Span
createSpanWithoutCallStack Tracer
t Context
ctx Text
n SpanArguments
args
forall (m :: * -> *). MonadIO m => (Context -> Context) -> m ()
adjustContext (Span -> Context -> Context
insertSpan Span
s)
forall (m :: * -> *). MonadIO m => Span -> m () -> m ()
whenSpanIsRecording Span
s forall a b. (a -> b) -> a -> b
$ do
case CallStack -> [([Char], SrcLoc)]
getCallStack CallStack
cs of
[] -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
([Char]
fn, SrcLoc
loc) : [([Char], SrcLoc)]
_ -> do
forall (m :: * -> *).
MonadIO m =>
Span -> [(Text, Attribute)] -> m ()
OpenTelemetry.Trace.Core.addAttributes
Span
s
[ (Text
"code.function", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack [Char]
fn)
, (Text
"code.namespace", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ SrcLoc -> [Char]
srcLocModule SrcLoc
loc)
, (Text
"code.filepath", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ SrcLoc -> [Char]
srcLocFile SrcLoc
loc)
, (Text
"code.lineno", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ SrcLoc -> Int
srcLocStartLine SrcLoc
loc)
, (Text
"code.package", forall a. ToAttribute a => a -> Attribute
toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ SrcLoc -> [Char]
srcLocPackage SrcLoc
loc)
]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Context -> Maybe Span
lookupSpan Context
ctx, Span
s)
)
( \Maybe SomeException
e (Maybe Span
parent, Span
s) -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe SomeException
e forall a b. (a -> b) -> a -> b
$ \(SomeException e
inner) -> do
forall (m :: * -> *). MonadIO m => Span -> SpanStatus -> m ()
setStatus Span
s forall a b. (a -> b) -> a -> b
$ Text -> SpanStatus
Error forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall e. Exception e => e -> [Char]
displayException e
inner
forall (m :: * -> *) e.
(MonadIO m, Exception e) =>
Span -> [(Text, Attribute)] -> Maybe Timestamp -> e -> m ()
recordException Span
s [] forall a. Maybe a
Nothing e
inner
forall (m :: * -> *). MonadIO m => Span -> Maybe Timestamp -> m ()
endSpan Span
s forall a. Maybe a
Nothing
forall (m :: * -> *). MonadIO m => (Context -> Context) -> m ()
adjustContext forall a b. (a -> b) -> a -> b
$ \Context
ctx ->
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Context -> Context
removeSpan Context
ctx) (Span -> Context -> Context
`insertSpan` Context
ctx) Maybe Span
parent
)
(\(Maybe Span
_, Span
s) -> Span -> m a
f Span
s)
isRecording :: MonadIO m => Span -> m Bool
isRecording :: forall (m :: * -> *). MonadIO m => Span -> m Bool
isRecording (Span IORef ImmutableSpan
s) = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. Maybe a -> Bool
isNothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. ImmutableSpan -> Maybe Timestamp
spanEnd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IORef a -> IO a
readIORef IORef ImmutableSpan
s)
isRecording (FrozenSpan SpanContext
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
isRecording (Dropped SpanContext
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
addAttribute ::
(MonadIO m, A.ToAttribute a) =>
Span ->
Text ->
a ->
m ()
addAttribute :: forall (m :: * -> *) a.
(MonadIO m, ToAttribute a) =>
Span -> Text -> a -> m ()
addAttribute (Span IORef ImmutableSpan
s) Text
k a
v = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef ImmutableSpan
s forall a b. (a -> b) -> a -> b
$ \(!ImmutableSpan
i) ->
ImmutableSpan
i
{ spanAttributes :: Attributes
spanAttributes =
forall a.
ToAttribute a =>
AttributeLimits -> Attributes -> Text -> a -> Attributes
OpenTelemetry.Attributes.addAttribute
(Tracer -> (SpanLimits -> Maybe Int) -> AttributeLimits
limitBy (ImmutableSpan -> Tracer
spanTracer ImmutableSpan
i) SpanLimits -> Maybe Int
spanAttributeCountLimit)
(ImmutableSpan -> Attributes
spanAttributes ImmutableSpan
i)
Text
k
a
v
}
addAttribute (FrozenSpan SpanContext
_) Text
_ a
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
addAttribute (Dropped SpanContext
_) Text
_ a
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
addAttributes :: MonadIO m => Span -> [(Text, A.Attribute)] -> m ()
addAttributes :: forall (m :: * -> *).
MonadIO m =>
Span -> [(Text, Attribute)] -> m ()
addAttributes (Span IORef ImmutableSpan
s) [(Text, Attribute)]
attrs = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef ImmutableSpan
s forall a b. (a -> b) -> a -> b
$ \(!ImmutableSpan
i) ->
ImmutableSpan
i
{ spanAttributes :: Attributes
spanAttributes =
forall a.
ToAttribute a =>
AttributeLimits -> Attributes -> [(Text, a)] -> Attributes
OpenTelemetry.Attributes.addAttributes
(Tracer -> (SpanLimits -> Maybe Int) -> AttributeLimits
limitBy (ImmutableSpan -> Tracer
spanTracer ImmutableSpan
i) SpanLimits -> Maybe Int
spanAttributeCountLimit)
(ImmutableSpan -> Attributes
spanAttributes ImmutableSpan
i)
[(Text, Attribute)]
attrs
}
addAttributes (FrozenSpan SpanContext
_) [(Text, Attribute)]
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
addAttributes (Dropped SpanContext
_) [(Text, Attribute)]
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
addEvent :: MonadIO m => Span -> NewEvent -> m ()
addEvent :: forall (m :: * -> *). MonadIO m => Span -> NewEvent -> m ()
addEvent (Span IORef ImmutableSpan
s) NewEvent {[(Text, Attribute)]
Maybe Timestamp
Text
newEventTimestamp :: NewEvent -> Maybe Timestamp
newEventAttributes :: NewEvent -> [(Text, Attribute)]
newEventName :: NewEvent -> Text
newEventTimestamp :: Maybe Timestamp
newEventAttributes :: [(Text, Attribute)]
newEventName :: Text
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Timestamp
t <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *). MonadIO m => m Timestamp
getTimestamp forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Timestamp
newEventTimestamp
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef ImmutableSpan
s forall a b. (a -> b) -> a -> b
$ \(!ImmutableSpan
i) ->
ImmutableSpan
i
{ spanEvents :: AppendOnlyBoundedCollection Event
spanEvents =
forall a.
AppendOnlyBoundedCollection a -> a -> AppendOnlyBoundedCollection a
appendToBoundedCollection (ImmutableSpan -> AppendOnlyBoundedCollection Event
spanEvents ImmutableSpan
i) forall a b. (a -> b) -> a -> b
$
Event
{ eventName :: Text
eventName = Text
newEventName
, eventAttributes :: Attributes
eventAttributes =
forall a.
ToAttribute a =>
AttributeLimits -> Attributes -> [(Text, a)] -> Attributes
A.addAttributes
(Tracer -> (SpanLimits -> Maybe Int) -> AttributeLimits
limitBy (ImmutableSpan -> Tracer
spanTracer ImmutableSpan
i) SpanLimits -> Maybe Int
eventAttributeCountLimit)
Attributes
emptyAttributes
[(Text, Attribute)]
newEventAttributes
, eventTimestamp :: Timestamp
eventTimestamp = Timestamp
t
}
}
addEvent (FrozenSpan SpanContext
_) NewEvent
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
addEvent (Dropped SpanContext
_) NewEvent
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
setStatus :: MonadIO m => Span -> SpanStatus -> m ()
setStatus :: forall (m :: * -> *). MonadIO m => Span -> SpanStatus -> m ()
setStatus (Span IORef ImmutableSpan
s) SpanStatus
st = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef ImmutableSpan
s forall a b. (a -> b) -> a -> b
$ \(!ImmutableSpan
i) ->
ImmutableSpan
i
{ spanStatus :: SpanStatus
spanStatus =
if SpanStatus
st forall a. Ord a => a -> a -> Bool
> ImmutableSpan -> SpanStatus
spanStatus ImmutableSpan
i
then SpanStatus
st
else ImmutableSpan -> SpanStatus
spanStatus ImmutableSpan
i
}
setStatus (FrozenSpan SpanContext
_) SpanStatus
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
setStatus (Dropped SpanContext
_) SpanStatus
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateName ::
MonadIO m =>
Span ->
Text ->
m ()
updateName :: forall (m :: * -> *). MonadIO m => Span -> Text -> m ()
updateName (Span IORef ImmutableSpan
s) Text
n = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef ImmutableSpan
s forall a b. (a -> b) -> a -> b
$ \(!ImmutableSpan
i) -> ImmutableSpan
i {spanName :: Text
spanName = Text
n}
updateName (FrozenSpan SpanContext
_) Text
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
updateName (Dropped SpanContext
_) Text
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
endSpan ::
MonadIO m =>
Span ->
Maybe Timestamp ->
m ()
endSpan :: forall (m :: * -> *). MonadIO m => Span -> Maybe Timestamp -> m ()
endSpan (Span IORef ImmutableSpan
s) Maybe Timestamp
mts = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Timestamp
ts <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *). MonadIO m => m Timestamp
getTimestamp forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Timestamp
mts
(Bool
alreadyFinished, ImmutableSpan
frozenS) <- forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef ImmutableSpan
s forall a b. (a -> b) -> a -> b
$ \(!ImmutableSpan
i) ->
let ref :: ImmutableSpan
ref = ImmutableSpan
i {spanEnd :: Maybe Timestamp
spanEnd = ImmutableSpan -> Maybe Timestamp
spanEnd ImmutableSpan
i forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. a -> Maybe a
Just Timestamp
ts}
in (ImmutableSpan
ref, (forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ ImmutableSpan -> Maybe Timestamp
spanEnd ImmutableSpan
i, ImmutableSpan
ref))
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
alreadyFinished forall a b. (a -> b) -> a -> b
$ do
Either SomeException ()
eResult <- forall e a. Exception e => IO a -> IO (Either e a)
try forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Processor -> IORef ImmutableSpan -> IO ()
`processorOnEnd` IORef ImmutableSpan
s) forall a b. (a -> b) -> a -> b
$ TracerProvider -> Vector Processor
tracerProviderProcessors forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider forall a b. (a -> b) -> a -> b
$ ImmutableSpan -> Tracer
spanTracer ImmutableSpan
frozenS
case Either SomeException ()
eResult of
Left SomeException
err -> forall a. Show a => a -> IO ()
print (SomeException
err :: SomeException)
Right ()
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
endSpan (FrozenSpan SpanContext
_) Maybe Timestamp
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
endSpan (Dropped SpanContext
_) Maybe Timestamp
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
recordException :: (MonadIO m, Exception e) => Span -> [(Text, Attribute)] -> Maybe Timestamp -> e -> m ()
recordException :: forall (m :: * -> *) e.
(MonadIO m, Exception e) =>
Span -> [(Text, Attribute)] -> Maybe Timestamp -> e -> m ()
recordException Span
s [(Text, Attribute)]
attrs Maybe Timestamp
ts e
e = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
[[Char]]
cs <- forall a. a -> IO [[Char]]
whoCreated e
e
let message :: Text
message = [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
show e
e
forall (m :: * -> *). MonadIO m => Span -> NewEvent -> m ()
addEvent Span
s forall a b. (a -> b) -> a -> b
$
NewEvent
{ newEventName :: Text
newEventName = Text
"exception"
, newEventAttributes :: [(Text, Attribute)]
newEventAttributes =
[(Text, Attribute)]
attrs
forall a. [a] -> [a] -> [a]
++ [ (Text
"exception.type", forall a. ToAttribute a => a -> Attribute
A.toAttribute forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
show forall a b. (a -> b) -> a -> b
$ forall a. Typeable a => a -> TypeRep
typeOf e
e)
, (Text
"exception.message", forall a. ToAttribute a => a -> Attribute
A.toAttribute Text
message)
, (Text
"exception.stacktrace", forall a. ToAttribute a => a -> Attribute
A.toAttribute forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map [Char] -> Text
T.pack [[Char]]
cs)
]
, newEventTimestamp :: Maybe Timestamp
newEventTimestamp = Maybe Timestamp
ts
}
isValid :: SpanContext -> Bool
isValid :: SpanContext -> Bool
isValid SpanContext
sc =
Bool -> Bool
not
(TraceId -> Bool
isEmptyTraceId (SpanContext -> TraceId
traceId SpanContext
sc) Bool -> Bool -> Bool
&& SpanId -> Bool
isEmptySpanId (SpanContext -> SpanId
spanId SpanContext
sc))
spanIsRemote :: MonadIO m => Span -> m Bool
spanIsRemote :: forall (m :: * -> *). MonadIO m => Span -> m Bool
spanIsRemote (Span IORef ImmutableSpan
s) = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
ImmutableSpan
i <- forall a. IORef a -> IO a
readIORef IORef ImmutableSpan
s
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ SpanContext -> Bool
Types.isRemote forall a b. (a -> b) -> a -> b
$ ImmutableSpan -> SpanContext
Types.spanContext ImmutableSpan
i
spanIsRemote (FrozenSpan SpanContext
c) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ SpanContext -> Bool
Types.isRemote SpanContext
c
spanIsRemote (Dropped SpanContext
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
unsafeReadSpan :: MonadIO m => Span -> m ImmutableSpan
unsafeReadSpan :: forall (m :: * -> *). MonadIO m => Span -> m ImmutableSpan
unsafeReadSpan = \case
Span IORef ImmutableSpan
ref -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef ImmutableSpan
ref
FrozenSpan SpanContext
_s -> forall a. HasCallStack => [Char] -> a
error [Char]
"This span is from another process"
Dropped SpanContext
_s -> forall a. HasCallStack => [Char] -> a
error [Char]
"This span was dropped"
wrapSpanContext :: SpanContext -> Span
wrapSpanContext :: SpanContext -> Span
wrapSpanContext = SpanContext -> Span
FrozenSpan
spanGetAttributes :: MonadIO m => Span -> m A.Attributes
spanGetAttributes :: forall (m :: * -> *). MonadIO m => Span -> m Attributes
spanGetAttributes = \case
Span IORef ImmutableSpan
ref -> do
ImmutableSpan
s <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef ImmutableSpan
ref
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ImmutableSpan -> Attributes
spanAttributes ImmutableSpan
s
FrozenSpan SpanContext
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Attributes
A.emptyAttributes
Dropped SpanContext
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Attributes
A.emptyAttributes
getTimestamp :: MonadIO m => m Timestamp
getTimestamp :: forall (m :: * -> *). MonadIO m => m Timestamp
getTimestamp = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce @(IO TimeSpec) @(IO Timestamp) forall a b. (a -> b) -> a -> b
$ Clock -> IO TimeSpec
getTime Clock
Realtime
limitBy ::
Tracer ->
(SpanLimits -> Maybe Int) ->
AttributeLimits
limitBy :: Tracer -> (SpanLimits -> Maybe Int) -> AttributeLimits
limitBy Tracer
t SpanLimits -> Maybe Int
countF =
AttributeLimits
{ attributeCountLimit :: Maybe Int
attributeCountLimit = Maybe Int
countLimit
, attributeLengthLimit :: Maybe Int
attributeLengthLimit = Maybe Int
lengthLimit
}
where
countLimit :: Maybe Int
countLimit =
SpanLimits -> Maybe Int
countF (TracerProvider -> SpanLimits
tracerProviderSpanLimits forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AttributeLimits -> Maybe Int
attributeCountLimit
(TracerProvider -> AttributeLimits
tracerProviderAttributeLimits forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t)
lengthLimit :: Maybe Int
lengthLimit =
SpanLimits -> Maybe Int
spanAttributeValueLengthLimit (TracerProvider -> SpanLimits
tracerProviderSpanLimits forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AttributeLimits -> Maybe Int
attributeLengthLimit
(TracerProvider -> AttributeLimits
tracerProviderAttributeLimits forall a b. (a -> b) -> a -> b
$ Tracer -> TracerProvider
tracerProvider Tracer
t)
globalTracer :: IORef TracerProvider
globalTracer :: IORef TracerProvider
globalTracer = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
TracerProvider
p <-
forall (m :: * -> *).
MonadIO m =>
[Processor] -> TracerProviderOptions -> m TracerProvider
createTracerProvider
[]
TracerProviderOptions
emptyTracerProviderOptions
forall a. a -> IO (IORef a)
newIORef TracerProvider
p
{-# NOINLINE globalTracer #-}
data TracerProviderOptions = TracerProviderOptions
{ TracerProviderOptions -> IdGenerator
tracerProviderOptionsIdGenerator :: IdGenerator
, TracerProviderOptions -> Sampler
tracerProviderOptionsSampler :: Sampler
, TracerProviderOptions -> MaterializedResources
tracerProviderOptionsResources :: MaterializedResources
, TracerProviderOptions -> AttributeLimits
tracerProviderOptionsAttributeLimits :: AttributeLimits
, TracerProviderOptions -> SpanLimits
tracerProviderOptionsSpanLimits :: SpanLimits
, TracerProviderOptions
-> Propagator Context RequestHeaders RequestHeaders
tracerProviderOptionsPropagators :: Propagator Context RequestHeaders ResponseHeaders
, TracerProviderOptions -> Log Text -> IO ()
tracerProviderOptionsLogger :: Log Text -> IO ()
}
emptyTracerProviderOptions :: TracerProviderOptions
emptyTracerProviderOptions :: TracerProviderOptions
emptyTracerProviderOptions =
IdGenerator
-> Sampler
-> MaterializedResources
-> AttributeLimits
-> SpanLimits
-> Propagator Context RequestHeaders RequestHeaders
-> (Log Text -> IO ())
-> TracerProviderOptions
TracerProviderOptions
IdGenerator
dummyIdGenerator
(ParentBasedOptions -> Sampler
parentBased forall a b. (a -> b) -> a -> b
$ Sampler -> ParentBasedOptions
parentBasedOptions Sampler
alwaysOn)
MaterializedResources
emptyMaterializedResources
AttributeLimits
defaultAttributeLimits
SpanLimits
defaultSpanLimits
forall a. Monoid a => a
mempty
(\Log Text
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
createTracerProvider :: MonadIO m => [Processor] -> TracerProviderOptions -> m TracerProvider
createTracerProvider :: forall (m :: * -> *).
MonadIO m =>
[Processor] -> TracerProviderOptions -> m TracerProvider
createTracerProvider [Processor]
ps TracerProviderOptions
opts = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
let g :: IdGenerator
g = TracerProviderOptions -> IdGenerator
tracerProviderOptionsIdGenerator TracerProviderOptions
opts
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
Vector Processor
-> IdGenerator
-> Sampler
-> MaterializedResources
-> AttributeLimits
-> SpanLimits
-> Propagator Context RequestHeaders RequestHeaders
-> (Log Text -> IO ())
-> TracerProvider
TracerProvider
(forall a. [a] -> Vector a
V.fromList [Processor]
ps)
IdGenerator
g
(TracerProviderOptions -> Sampler
tracerProviderOptionsSampler TracerProviderOptions
opts)
(TracerProviderOptions -> MaterializedResources
tracerProviderOptionsResources TracerProviderOptions
opts)
(TracerProviderOptions -> AttributeLimits
tracerProviderOptionsAttributeLimits TracerProviderOptions
opts)
(TracerProviderOptions -> SpanLimits
tracerProviderOptionsSpanLimits TracerProviderOptions
opts)
(TracerProviderOptions
-> Propagator Context RequestHeaders RequestHeaders
tracerProviderOptionsPropagators TracerProviderOptions
opts)
(TracerProviderOptions -> Log Text -> IO ()
tracerProviderOptionsLogger TracerProviderOptions
opts)
getGlobalTracerProvider :: MonadIO m => m TracerProvider
getGlobalTracerProvider :: forall (m :: * -> *). MonadIO m => m TracerProvider
getGlobalTracerProvider = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef TracerProvider
globalTracer
setGlobalTracerProvider :: MonadIO m => TracerProvider -> m ()
setGlobalTracerProvider :: forall (m :: * -> *). MonadIO m => TracerProvider -> m ()
setGlobalTracerProvider = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IORef a -> a -> IO ()
writeIORef IORef TracerProvider
globalTracer
getTracerProviderResources :: TracerProvider -> MaterializedResources
getTracerProviderResources :: TracerProvider -> MaterializedResources
getTracerProviderResources = TracerProvider -> MaterializedResources
tracerProviderResources
getTracerProviderPropagators :: TracerProvider -> Propagator Context RequestHeaders ResponseHeaders
getTracerProviderPropagators :: TracerProvider -> Propagator Context RequestHeaders RequestHeaders
getTracerProviderPropagators = TracerProvider -> Propagator Context RequestHeaders RequestHeaders
tracerProviderPropagators
newtype TracerOptions = TracerOptions
{ TracerOptions -> Maybe Text
tracerSchema :: Maybe Text
}
tracerOptions :: TracerOptions
tracerOptions :: TracerOptions
tracerOptions = Maybe Text -> TracerOptions
TracerOptions forall a. Maybe a
Nothing
class HasTracer s where
tracerL :: Lens' s Tracer
makeTracer :: TracerProvider -> InstrumentationLibrary -> TracerOptions -> Tracer
makeTracer :: TracerProvider -> InstrumentationLibrary -> TracerOptions -> Tracer
makeTracer TracerProvider
tp InstrumentationLibrary
n TracerOptions {} = InstrumentationLibrary -> TracerProvider -> Tracer
Tracer InstrumentationLibrary
n TracerProvider
tp
getTracer :: MonadIO m => TracerProvider -> InstrumentationLibrary -> TracerOptions -> m Tracer
getTracer :: forall (m :: * -> *).
MonadIO m =>
TracerProvider
-> InstrumentationLibrary -> TracerOptions -> m Tracer
getTracer TracerProvider
tp InstrumentationLibrary
n TracerOptions {} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ InstrumentationLibrary -> TracerProvider -> Tracer
Tracer InstrumentationLibrary
n TracerProvider
tp
{-# DEPRECATED getTracer "use makeTracer" #-}
getImmutableSpanTracer :: ImmutableSpan -> Tracer
getImmutableSpanTracer :: ImmutableSpan -> Tracer
getImmutableSpanTracer = ImmutableSpan -> Tracer
spanTracer
getTracerTracerProvider :: Tracer -> TracerProvider
getTracerTracerProvider :: Tracer -> TracerProvider
getTracerTracerProvider = Tracer -> TracerProvider
tracerProvider
defaultSpanArguments :: SpanArguments
defaultSpanArguments :: SpanArguments
defaultSpanArguments =
SpanArguments
{ kind :: SpanKind
kind = SpanKind
Internal
, attributes :: [(Text, Attribute)]
attributes = []
, links :: [NewLink]
links = []
, startTime :: Maybe Timestamp
startTime = forall a. Maybe a
Nothing
}
shutdownTracerProvider :: MonadIO m => TracerProvider -> m ()
shutdownTracerProvider :: forall (m :: * -> *). MonadIO m => TracerProvider -> m ()
shutdownTracerProvider TracerProvider {Vector Processor
AttributeLimits
Propagator Context RequestHeaders RequestHeaders
MaterializedResources
IdGenerator
SpanLimits
Sampler
Log Text -> IO ()
tracerProviderLogger :: TracerProvider -> Log Text -> IO ()
tracerProviderLogger :: Log Text -> IO ()
tracerProviderPropagators :: Propagator Context RequestHeaders RequestHeaders
tracerProviderSpanLimits :: SpanLimits
tracerProviderAttributeLimits :: AttributeLimits
tracerProviderResources :: MaterializedResources
tracerProviderSampler :: Sampler
tracerProviderIdGenerator :: IdGenerator
tracerProviderProcessors :: Vector Processor
tracerProviderPropagators :: TracerProvider -> Propagator Context RequestHeaders RequestHeaders
tracerProviderResources :: TracerProvider -> MaterializedResources
tracerProviderAttributeLimits :: TracerProvider -> AttributeLimits
tracerProviderSpanLimits :: TracerProvider -> SpanLimits
tracerProviderSampler :: TracerProvider -> Sampler
tracerProviderProcessors :: TracerProvider -> Vector Processor
tracerProviderIdGenerator :: TracerProvider -> IdGenerator
..} = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Vector (Async ShutdownResult)
asyncShutdownResults <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Vector Processor
tracerProviderProcessors forall a b. (a -> b) -> a -> b
$ \Processor
processor -> do
Processor -> IO (Async ShutdownResult)
processorShutdown Processor
processor
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall a. Async a -> IO a
wait Vector (Async ShutdownResult)
asyncShutdownResults
forceFlushTracerProvider ::
MonadIO m =>
TracerProvider ->
Maybe Int ->
m FlushResult
forceFlushTracerProvider :: forall (m :: * -> *).
MonadIO m =>
TracerProvider -> Maybe Int -> m FlushResult
forceFlushTracerProvider TracerProvider {Vector Processor
AttributeLimits
Propagator Context RequestHeaders RequestHeaders
MaterializedResources
IdGenerator
SpanLimits
Sampler
Log Text -> IO ()
tracerProviderLogger :: Log Text -> IO ()
tracerProviderPropagators :: Propagator Context RequestHeaders RequestHeaders
tracerProviderSpanLimits :: SpanLimits
tracerProviderAttributeLimits :: AttributeLimits
tracerProviderResources :: MaterializedResources
tracerProviderSampler :: Sampler
tracerProviderIdGenerator :: IdGenerator
tracerProviderProcessors :: Vector Processor
tracerProviderLogger :: TracerProvider -> Log Text -> IO ()
tracerProviderPropagators :: TracerProvider -> Propagator Context RequestHeaders RequestHeaders
tracerProviderResources :: TracerProvider -> MaterializedResources
tracerProviderAttributeLimits :: TracerProvider -> AttributeLimits
tracerProviderSpanLimits :: TracerProvider -> SpanLimits
tracerProviderSampler :: TracerProvider -> Sampler
tracerProviderProcessors :: TracerProvider -> Vector Processor
tracerProviderIdGenerator :: TracerProvider -> IdGenerator
..} Maybe Int
mtimeout = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Vector (Async ())
jobs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Vector Processor
tracerProviderProcessors forall a b. (a -> b) -> a -> b
$ \Processor
processor -> forall a. IO a -> IO (Async a)
async forall a b. (a -> b) -> a -> b
$ do
Processor -> IO ()
processorForceFlush Processor
processor
Maybe FlushResult
mresult <-
forall a. Int -> IO a -> IO (Maybe a)
timeout (forall a. a -> Maybe a -> a
fromMaybe Int
5_000_000 Maybe Int
mtimeout) forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM
( \FlushResult
status Async ()
action -> do
Either SomeException ()
res <- forall a. Async a -> IO (Either SomeException a)
waitCatch Async ()
action
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! case Either SomeException ()
res of
Left SomeException
_err -> FlushResult
FlushError
Right ()
_ok -> FlushResult
status
)
FlushResult
FlushSuccess
Vector (Async ())
jobs
case Maybe FlushResult
mresult of
Maybe FlushResult
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure FlushResult
FlushTimeout
Just FlushResult
res -> forall (f :: * -> *) a. Applicative f => a -> f a
pure FlushResult
res
whenSpanIsRecording :: MonadIO m => Span -> m () -> m ()
whenSpanIsRecording :: forall (m :: * -> *). MonadIO m => Span -> m () -> m ()
whenSpanIsRecording (Span IORef ImmutableSpan
ref) m ()
m = do
ImmutableSpan
span_ <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO a
readIORef IORef ImmutableSpan
ref
case ImmutableSpan -> Maybe Timestamp
spanEnd ImmutableSpan
span_ of
Maybe Timestamp
Nothing -> m ()
m
Just Timestamp
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
whenSpanIsRecording (FrozenSpan SpanContext
_) m ()
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
whenSpanIsRecording (Dropped SpanContext
_) m ()
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
timestampNanoseconds :: Timestamp -> Word64
timestampNanoseconds :: Timestamp -> Word64
timestampNanoseconds (Timestamp TimeSpec {Int64
sec :: TimeSpec -> Int64
nsec :: TimeSpec -> Int64
nsec :: Int64
sec :: Int64
..}) = forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64
sec forall a. Num a => a -> a -> a
* Int64
1_000_000_000) forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
nsec