module Ecluse.Runtime.Telemetry.Batched.Internal (
batchMeterProvider,
Batch (..),
newBatch,
batchedProvider,
flushBatch,
) where
import Data.Atomics (atomicModifyIORefCAS_)
import Data.HashMap.Strict qualified as HashMap
import GHC.Clock (getMonotonicTimeNSec)
import OpenTelemetry.Attributes (Attributes)
import OpenTelemetry.Context (insertSpan, removeSpan)
import OpenTelemetry.Context.ThreadLocal (adjustContext, getContext)
import OpenTelemetry.Internal.Common.Types (InstrumentationLibrary)
import OpenTelemetry.Metric.Core (
AdvisoryParameters (advisoryAttributeKeys, advisoryExplicitBucketBoundaries, advisoryHistogramAggregation),
Counter (counterAdd, counterEnabled),
Gauge (gaugeEnabled, gaugeRecord),
Histogram (histogramEnabled, histogramRecord),
HistogramAggregation,
Meter (meterCreateCounterInt64, meterCreateGaugeInt64, meterCreateHistogram, meterCreateObservableGaugeInt64, meterCreateUpDownCounterInt64, meterInstrumentationScope),
MeterProvider (meterProviderGetMeter),
UpDownCounter (upDownCounterAdd, upDownCounterEnabled),
defaultAdvisoryParameters,
getMeter,
)
import OpenTelemetry.Trace.Core (SpanContext, getActiveSpanContext, isValid, wrapSpanContext)
import UnliftIO.Concurrent (threadDelay)
import UnliftIO.Exception (catchAny, finally, tryAny, uninterruptibleMask_)
import UnliftIO.MVar (modifyMVar, withMVar)
import Ecluse.Core.Telemetry.Span (ecluseScope)
import Ecluse.Runtime.Telemetry.Batched.Cells (drainCells, modifyOwnCell, newCells)
batchMeterProvider :: (SomeException -> IO ()) -> MeterProvider -> IO (MeterProvider, IO ())
batchMeterProvider :: (SomeException -> IO ())
-> MeterProvider -> IO (MeterProvider, IO ())
batchMeterProvider SomeException -> IO ()
report MeterProvider
sdk = do
batch <- IO Word64 -> (SomeException -> IO ()) -> IO Batch
newBatch IO Word64
getMonotonicTimeNSec SomeException -> IO ()
report
wrapped <- batchedProvider batch sdk
pure (wrapped, threadDelay flushIntervalMicros >> flushBatch batch)
flushIntervalMicros :: Int
flushIntervalMicros :: Int
flushIntervalMicros = Int
1_000_000
data Batch = Batch
{ Batch -> MVar Instruments
batchInstruments :: MVar Instruments
, Batch -> IORef [IO ()]
batchFlushes :: IORef [IO ()]
, Batch -> MVar ()
batchLock :: MVar ()
, Batch -> IO Word64
batchClock :: IO Word64
, Batch -> SomeException -> IO ()
batchReport :: SomeException -> IO ()
}
newBatch :: IO Word64 -> (SomeException -> IO ()) -> IO Batch
newBatch :: IO Word64 -> (SomeException -> IO ()) -> IO Batch
newBatch IO Word64
clock SomeException -> IO ()
report = do
instruments <- Instruments -> IO (MVar Instruments)
forall (m :: * -> *) a. MonadIO m => a -> m (MVar a)
newMVar (HashMap InstrumentKey (Counter Int64)
-> HashMap InstrumentKey (UpDownCounter Int64)
-> HashMap InstrumentKey (Gauge Int64)
-> HashMap InstrumentKey Histogram
-> Instruments
Instruments HashMap InstrumentKey (Counter Int64)
forall k v. HashMap k v
HashMap.empty HashMap InstrumentKey (UpDownCounter Int64)
forall k v. HashMap k v
HashMap.empty HashMap InstrumentKey (Gauge Int64)
forall k v. HashMap k v
HashMap.empty HashMap InstrumentKey Histogram
forall k v. HashMap k v
HashMap.empty)
flushes <- newIORef []
lock <- newMVar ()
pure Batch{batchInstruments = instruments, batchFlushes = flushes, batchLock = lock, batchClock = clock, batchReport = report}
batchedProvider :: Batch -> MeterProvider -> IO MeterProvider
batchedProvider :: Batch -> MeterProvider -> IO MeterProvider
batchedProvider Batch
batch MeterProvider
sdk = do
hookMeter <- MeterProvider -> InstrumentationLibrary -> IO Meter
getMeter MeterProvider
sdk InstrumentationLibrary
forall s. IsString s => s
ecluseScope
void (meterCreateObservableGaugeInt64 hookMeter "ecluse.metric_flush" Nothing Nothing defaultAdvisoryParameters [const (flushBatch batch `catchAny` batchReport batch)])
pure sdk{meterProviderGetMeter = fmap (batchedMeter batch) . meterProviderGetMeter sdk}
flushBatch :: Batch -> IO ()
flushBatch :: Batch -> IO ()
flushBatch Batch
batch = MVar () -> (() -> IO ()) -> IO ()
forall (m :: * -> *) a b.
MonadUnliftIO m =>
MVar a -> (a -> m b) -> m b
withMVar (Batch -> MVar ()
batchLock Batch
batch) ((() -> IO ()) -> IO ()) -> (() -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \() -> IO () -> IO ()
forall (m :: * -> *) a. MonadUnliftIO m => m a -> m a
uninterruptibleMask_ (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
flushes <- IORef [IO ()] -> IO [IO ()]
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef (Batch -> IORef [IO ()]
batchFlushes Batch
batch)
original <- getContext
sequence_ (reverse flushes) `finally` adjustContext (const original)
registerFlush :: Batch -> IO () -> IO ()
registerFlush :: Batch -> IO () -> IO ()
registerFlush Batch
batch IO ()
flush = IORef [IO ()] -> ([IO ()] -> [IO ()]) -> IO ()
forall t. IORef t -> (t -> t) -> IO ()
atomicModifyIORefCAS_ (Batch -> IORef [IO ()]
batchFlushes Batch
batch) (IO ()
flush IO () -> [IO ()] -> [IO ()]
forall a. a -> [a] -> [a]
:)
replaySeries :: Batch -> IO () -> IO ()
replaySeries :: Batch -> IO () -> IO ()
replaySeries Batch
batch IO ()
replay = IO () -> IO (Either SomeException ())
forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> m (Either SomeException a)
tryAny IO ()
replay IO (Either SomeException ())
-> (Either SomeException () -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SomeException -> IO ())
-> (() -> IO ()) -> Either SomeException () -> IO ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Batch -> SomeException -> IO ()
batchReport Batch
batch) () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
type InstrumentKey = (InstrumentationLibrary, Text, Maybe Text, Maybe Text, (Maybe [Double], Maybe [Text], Maybe HistogramAggregation))
data Instruments = Instruments
{ Instruments -> HashMap InstrumentKey (Counter Int64)
knownCounters :: HashMap.HashMap InstrumentKey (Counter Int64)
, Instruments -> HashMap InstrumentKey (UpDownCounter Int64)
knownUpDownCounters :: HashMap.HashMap InstrumentKey (UpDownCounter Int64)
, Instruments -> HashMap InstrumentKey (Gauge Int64)
knownGauges :: HashMap.HashMap InstrumentKey (Gauge Int64)
, Instruments -> HashMap InstrumentKey Histogram
knownHistograms :: HashMap.HashMap InstrumentKey Histogram
}
batchedMeter :: Batch -> Meter -> Meter
batchedMeter :: Batch -> Meter -> Meter
batchedMeter Batch
batch Meter
meter =
Meter
meter
{ meterCreateCounterInt64 = \Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice ->
Batch
-> (Instruments -> HashMap InstrumentKey (Counter Int64))
-> (HashMap InstrumentKey (Counter Int64)
-> Instruments -> Instruments)
-> InstrumentKey
-> IO (Counter Int64)
-> IO (Counter Int64)
forall a.
Batch
-> (Instruments -> HashMap InstrumentKey a)
-> (HashMap InstrumentKey a -> Instruments -> Instruments)
-> InstrumentKey
-> IO a
-> IO a
createOnce Batch
batch Instruments -> HashMap InstrumentKey (Counter Int64)
knownCounters (\HashMap InstrumentKey (Counter Int64)
known Instruments
i -> Instruments
i{knownCounters = known}) (Text
-> Maybe Text -> Maybe Text -> AdvisoryParameters -> InstrumentKey
forall {b} {c} {d}.
b
-> c
-> d
-> AdvisoryParameters
-> (InstrumentationLibrary, b, c, d,
(Maybe [Double], Maybe [Text], Maybe HistogramAggregation))
keyOf Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice) (IO (Counter Int64) -> IO (Counter Int64))
-> IO (Counter Int64) -> IO (Counter Int64)
forall a b. (a -> b) -> a -> b
$ do
inner <- Meter
-> Text
-> Maybe Text
-> Maybe Text
-> AdvisoryParameters
-> IO (Counter Int64)
meterCreateCounterInt64 Meter
meter Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice
whenEnabled (counterEnabled inner) inner ((\Int64 -> Attributes -> IO ()
add -> Counter Int64
inner{counterAdd = add}) <$> batchedSum batch (counterAdd inner))
, meterCreateUpDownCounterInt64 = \Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice ->
Batch
-> (Instruments -> HashMap InstrumentKey (UpDownCounter Int64))
-> (HashMap InstrumentKey (UpDownCounter Int64)
-> Instruments -> Instruments)
-> InstrumentKey
-> IO (UpDownCounter Int64)
-> IO (UpDownCounter Int64)
forall a.
Batch
-> (Instruments -> HashMap InstrumentKey a)
-> (HashMap InstrumentKey a -> Instruments -> Instruments)
-> InstrumentKey
-> IO a
-> IO a
createOnce Batch
batch Instruments -> HashMap InstrumentKey (UpDownCounter Int64)
knownUpDownCounters (\HashMap InstrumentKey (UpDownCounter Int64)
known Instruments
i -> Instruments
i{knownUpDownCounters = known}) (Text
-> Maybe Text -> Maybe Text -> AdvisoryParameters -> InstrumentKey
forall {b} {c} {d}.
b
-> c
-> d
-> AdvisoryParameters
-> (InstrumentationLibrary, b, c, d,
(Maybe [Double], Maybe [Text], Maybe HistogramAggregation))
keyOf Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice) (IO (UpDownCounter Int64) -> IO (UpDownCounter Int64))
-> IO (UpDownCounter Int64) -> IO (UpDownCounter Int64)
forall a b. (a -> b) -> a -> b
$ do
inner <- Meter
-> Text
-> Maybe Text
-> Maybe Text
-> AdvisoryParameters
-> IO (UpDownCounter Int64)
meterCreateUpDownCounterInt64 Meter
meter Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice
whenEnabled (upDownCounterEnabled inner) inner ((\Int64 -> Attributes -> IO ()
add -> UpDownCounter Int64
inner{upDownCounterAdd = add}) <$> batchedSum batch (upDownCounterAdd inner))
, meterCreateGaugeInt64 = \Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice ->
Batch
-> (Instruments -> HashMap InstrumentKey (Gauge Int64))
-> (HashMap InstrumentKey (Gauge Int64)
-> Instruments -> Instruments)
-> InstrumentKey
-> IO (Gauge Int64)
-> IO (Gauge Int64)
forall a.
Batch
-> (Instruments -> HashMap InstrumentKey a)
-> (HashMap InstrumentKey a -> Instruments -> Instruments)
-> InstrumentKey
-> IO a
-> IO a
createOnce Batch
batch Instruments -> HashMap InstrumentKey (Gauge Int64)
knownGauges (\HashMap InstrumentKey (Gauge Int64)
known Instruments
i -> Instruments
i{knownGauges = known}) (Text
-> Maybe Text -> Maybe Text -> AdvisoryParameters -> InstrumentKey
forall {b} {c} {d}.
b
-> c
-> d
-> AdvisoryParameters
-> (InstrumentationLibrary, b, c, d,
(Maybe [Double], Maybe [Text], Maybe HistogramAggregation))
keyOf Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice) (IO (Gauge Int64) -> IO (Gauge Int64))
-> IO (Gauge Int64) -> IO (Gauge Int64)
forall a b. (a -> b) -> a -> b
$ do
inner <- Meter
-> Text
-> Maybe Text
-> Maybe Text
-> AdvisoryParameters
-> IO (Gauge Int64)
meterCreateGaugeInt64 Meter
meter Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice
whenEnabled (gaugeEnabled inner) inner ((\Int64 -> Attributes -> IO ()
record -> Gauge Int64
inner{gaugeRecord = record}) <$> batchedGauge batch (gaugeRecord inner))
, meterCreateHistogram = \Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice ->
Batch
-> (Instruments -> HashMap InstrumentKey Histogram)
-> (HashMap InstrumentKey Histogram -> Instruments -> Instruments)
-> InstrumentKey
-> IO Histogram
-> IO Histogram
forall a.
Batch
-> (Instruments -> HashMap InstrumentKey a)
-> (HashMap InstrumentKey a -> Instruments -> Instruments)
-> InstrumentKey
-> IO a
-> IO a
createOnce Batch
batch Instruments -> HashMap InstrumentKey Histogram
knownHistograms (\HashMap InstrumentKey Histogram
known Instruments
i -> Instruments
i{knownHistograms = known}) (Text
-> Maybe Text -> Maybe Text -> AdvisoryParameters -> InstrumentKey
forall {b} {c} {d}.
b
-> c
-> d
-> AdvisoryParameters
-> (InstrumentationLibrary, b, c, d,
(Maybe [Double], Maybe [Text], Maybe HistogramAggregation))
keyOf Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice) (IO Histogram -> IO Histogram) -> IO Histogram -> IO Histogram
forall a b. (a -> b) -> a -> b
$ do
inner <- Meter
-> Text
-> Maybe Text
-> Maybe Text
-> AdvisoryParameters
-> IO Histogram
meterCreateHistogram Meter
meter Text
name Maybe Text
unit Maybe Text
description AdvisoryParameters
advice
whenEnabled (histogramEnabled inner) inner ((\Double -> Attributes -> IO ()
record -> Histogram
inner{histogramRecord = record}) <$> batchedHistogram batch (histogramRecord inner))
}
where
keyOf :: b
-> c
-> d
-> AdvisoryParameters
-> (InstrumentationLibrary, b, c, d,
(Maybe [Double], Maybe [Text], Maybe HistogramAggregation))
keyOf b
name c
unit d
description AdvisoryParameters
advice =
( Meter -> InstrumentationLibrary
meterInstrumentationScope Meter
meter
, b
name
, c
unit
, d
description
, (AdvisoryParameters -> Maybe [Double]
advisoryExplicitBucketBoundaries AdvisoryParameters
advice, AdvisoryParameters -> Maybe [Text]
advisoryAttributeKeys AdvisoryParameters
advice, AdvisoryParameters -> Maybe HistogramAggregation
advisoryHistogramAggregation AdvisoryParameters
advice)
)
createOnce ::
Batch ->
(Instruments -> HashMap.HashMap InstrumentKey a) ->
(HashMap.HashMap InstrumentKey a -> Instruments -> Instruments) ->
InstrumentKey ->
IO a ->
IO a
createOnce :: forall a.
Batch
-> (Instruments -> HashMap InstrumentKey a)
-> (HashMap InstrumentKey a -> Instruments -> Instruments)
-> InstrumentKey
-> IO a
-> IO a
createOnce Batch
batch Instruments -> HashMap InstrumentKey a
known HashMap InstrumentKey a -> Instruments -> Instruments
remember InstrumentKey
key IO a
create = MVar Instruments -> (Instruments -> IO (Instruments, a)) -> IO a
forall (m :: * -> *) a b.
MonadUnliftIO m =>
MVar a -> (a -> m (a, b)) -> m b
modifyMVar (Batch -> MVar Instruments
batchInstruments Batch
batch) ((Instruments -> IO (Instruments, a)) -> IO a)
-> (Instruments -> IO (Instruments, a)) -> IO a
forall a b. (a -> b) -> a -> b
$ \Instruments
instruments ->
case InstrumentKey -> HashMap InstrumentKey a -> Maybe a
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup InstrumentKey
key (Instruments -> HashMap InstrumentKey a
known Instruments
instruments) of
Just a
existing -> (Instruments, a) -> IO (Instruments, a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Instruments
instruments, a
existing)
Maybe a
Nothing -> IO a
create IO a -> (a -> (Instruments, a)) -> IO (Instruments, a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
made -> (HashMap InstrumentKey a -> Instruments -> Instruments
remember (InstrumentKey
-> a -> HashMap InstrumentKey a -> HashMap InstrumentKey a
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert InstrumentKey
key a
made (Instruments -> HashMap InstrumentKey a
known Instruments
instruments)) Instruments
instruments, a
made)
whenEnabled :: IO Bool -> a -> IO a -> IO a
whenEnabled :: forall a. IO Bool -> a -> IO a -> IO a
whenEnabled IO Bool
enabled a
inner IO a
batched = IO Bool
enabled IO Bool -> (Bool -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO a -> IO a -> Bool -> IO a
forall a. a -> a -> Bool -> a
bool (a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
inner) IO a
batched
data Traced = Traced Word64 SpanContext
data Pending = Pending Int64 (Maybe Traced)
data Reading = Reading Word64 Int64 (Maybe Traced)
data Sample = Sample Word64 Double (Maybe SpanContext)
batchedSum :: Batch -> (Int64 -> Attributes -> IO ()) -> IO (Int64 -> Attributes -> IO ())
batchedSum :: Batch
-> (Int64 -> Attributes -> IO ())
-> IO (Int64 -> Attributes -> IO ())
batchedSum Batch
batch Int64 -> Attributes -> IO ()
add = do
cells <- HashMap Attributes Pending
-> IO (Cells (HashMap Attributes Pending))
forall a. a -> IO (Cells a)
newCells HashMap Attributes Pending
forall k v. HashMap k v
HashMap.empty
registerFlush batch $ do
totals <- foldl' (HashMap.unionWith addPending) HashMap.empty <$> drainCells cells
for_ (HashMap.toList totals) $ \(Attributes
attrs, Pending Int64
delta Maybe Traced
traced) ->
Batch -> IO () -> IO ()
replaySeries Batch
batch (Maybe SpanContext -> IO () -> IO ()
underSpan (Traced -> SpanContext
tracedSpan (Traced -> SpanContext) -> Maybe Traced -> Maybe SpanContext
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Traced
traced) (Int64 -> Attributes -> IO ()
add Int64
delta Attributes
attrs))
pure $ \Int64
delta Attributes
attrs -> do
recorded <- IO (Maybe SpanContext)
recordedSpan
at <- batchClock batch
modifyOwnCell cells (HashMap.insertWith addPending attrs (Pending delta (stamp at recorded)))
where
addPending :: Pending -> Pending -> Pending
addPending (Pending Int64
a Maybe Traced
tracedA) (Pending Int64
b Maybe Traced
tracedB) = Int64 -> Maybe Traced -> Pending
Pending (Int64
a Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
b) (Maybe Traced -> Maybe Traced -> Maybe Traced
newest Maybe Traced
tracedA Maybe Traced
tracedB)
batchedGauge :: Batch -> (Int64 -> Attributes -> IO ()) -> IO (Int64 -> Attributes -> IO ())
batchedGauge :: Batch
-> (Int64 -> Attributes -> IO ())
-> IO (Int64 -> Attributes -> IO ())
batchedGauge Batch
batch Int64 -> Attributes -> IO ()
record = do
cells <- HashMap Attributes Reading
-> IO (Cells (HashMap Attributes Reading))
forall a. a -> IO (Cells a)
newCells HashMap Attributes Reading
forall k v. HashMap k v
HashMap.empty
flushedAt <- newIORef HashMap.empty
registerFlush batch $ do
latest <- foldl' (HashMap.unionWith newerReading) HashMap.empty <$> drainCells cells
before <- readIORef flushedAt
for_ (HashMap.toList latest) $ \(Attributes
attrs, Reading Word64
at Int64
value Maybe Traced
traced) ->
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool -> (Word64 -> Bool) -> Maybe Word64 -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Word64
at Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<) (Attributes -> HashMap Attributes Word64 -> Maybe Word64
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Attributes
attrs HashMap Attributes Word64
before)) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
Batch -> IO () -> IO ()
replaySeries Batch
batch (Maybe SpanContext -> IO () -> IO ()
underSpan (Traced -> SpanContext
tracedSpan (Traced -> SpanContext) -> Maybe Traced -> Maybe SpanContext
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Traced
traced) (Int64 -> Attributes -> IO ()
record Int64
value Attributes
attrs))
writeIORef flushedAt (HashMap.unionWith max (readingAt <$> latest) before)
pure $ \Int64
value Attributes
attrs -> do
recorded <- IO (Maybe SpanContext)
recordedSpan
at <- batchClock batch
modifyOwnCell cells (HashMap.insertWith newerReading attrs (Reading at value (stamp at recorded)))
where
newerReading :: Reading -> Reading -> Reading
newerReading (Reading Word64
atA Int64
valueA Maybe Traced
tracedA) (Reading Word64
atB Int64
valueB Maybe Traced
tracedB)
| Word64
atA Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
atB = Word64 -> Int64 -> Maybe Traced -> Reading
Reading Word64
atA Int64
valueA (Maybe Traced -> Maybe Traced -> Maybe Traced
newest Maybe Traced
tracedA Maybe Traced
tracedB)
| Bool
otherwise = Word64 -> Int64 -> Maybe Traced -> Reading
Reading Word64
atB Int64
valueB (Maybe Traced -> Maybe Traced -> Maybe Traced
newest Maybe Traced
tracedA Maybe Traced
tracedB)
readingAt :: Reading -> Word64
readingAt (Reading Word64
at Int64
_ Maybe Traced
_) = Word64
at
batchedHistogram :: Batch -> (Double -> Attributes -> IO ()) -> IO (Double -> Attributes -> IO ())
batchedHistogram :: Batch
-> (Double -> Attributes -> IO ())
-> IO (Double -> Attributes -> IO ())
batchedHistogram Batch
batch Double -> Attributes -> IO ()
record = do
cells <- HashMap Attributes [Sample]
-> IO (Cells (HashMap Attributes [Sample]))
forall a. a -> IO (Cells a)
newCells HashMap Attributes [Sample]
forall k v. HashMap k v
HashMap.empty
registerFlush batch $ do
series <- foldl' (HashMap.unionWith (<>)) HashMap.empty <$> drainCells cells
for_ (HashMap.toList series) $ \(Attributes
attrs, [Sample]
samples) ->
Batch -> IO () -> IO ()
replaySeries Batch
batch (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
[Sample] -> (Sample -> IO ()) -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ ((Sample -> Word64) -> [Sample] -> [Sample]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortWith Sample -> Word64
sampleAt [Sample]
samples) ((Sample -> IO ()) -> IO ()) -> (Sample -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
\(Sample Word64
_ Double
value Maybe SpanContext
recorded) -> Maybe SpanContext -> IO () -> IO ()
underSpan Maybe SpanContext
recorded (Double -> Attributes -> IO ()
record Double
value Attributes
attrs)
pure $ \Double
value Attributes
attrs -> do
recorded <- IO (Maybe SpanContext)
recordedSpan
at <- batchClock batch
let !sample = Word64 -> Double -> Maybe SpanContext -> Sample
Sample Word64
at Double
value Maybe SpanContext
recorded
modifyOwnCell cells (HashMap.insertWith (const (sample :)) attrs [sample])
where
sampleAt :: Sample -> Word64
sampleAt (Sample Word64
at Double
_ Maybe SpanContext
_) = Word64
at
recordedSpan :: IO (Maybe SpanContext)
recordedSpan :: IO (Maybe SpanContext)
recordedSpan = IO (Maybe SpanContext)
forall (m :: * -> *). MonadIO m => m (Maybe SpanContext)
getActiveSpanContext IO (Maybe SpanContext)
-> (Maybe SpanContext -> Maybe SpanContext)
-> IO (Maybe SpanContext)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Maybe SpanContext
-> (SpanContext -> Maybe SpanContext) -> Maybe SpanContext
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \SpanContext
active -> if SpanContext -> Bool
isValid SpanContext
active then SpanContext -> Maybe SpanContext
forall a. a -> Maybe a
Just SpanContext
active else Maybe SpanContext
forall a. Maybe a
Nothing)
stamp :: Word64 -> Maybe SpanContext -> Maybe Traced
stamp :: Word64 -> Maybe SpanContext -> Maybe Traced
stamp Word64
at = \case
Maybe SpanContext
Nothing -> Maybe Traced
forall a. Maybe a
Nothing
Just SpanContext
recorded -> Traced -> Maybe Traced
forall a. a -> Maybe a
Just (Traced -> Maybe Traced) -> Traced -> Maybe Traced
forall a b. (a -> b) -> a -> b
$! Word64 -> SpanContext -> Traced
Traced Word64
at SpanContext
recorded
newest :: Maybe Traced -> Maybe Traced -> Maybe Traced
newest :: Maybe Traced -> Maybe Traced -> Maybe Traced
newest Maybe Traced
a Maybe Traced
b = case (Maybe Traced
a, Maybe Traced
b) of
(Just (Traced Word64
atA SpanContext
_), Just (Traced Word64
atB SpanContext
_)) -> if Word64
atA Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
atB then Maybe Traced
a else Maybe Traced
b
(Maybe Traced, Maybe Traced)
_ -> Maybe Traced
a Maybe Traced -> Maybe Traced -> Maybe Traced
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Traced
b
tracedSpan :: Traced -> SpanContext
tracedSpan :: Traced -> SpanContext
tracedSpan (Traced Word64
_ SpanContext
recorded) = SpanContext
recorded
underSpan :: Maybe SpanContext -> IO () -> IO ()
underSpan :: Maybe SpanContext -> IO () -> IO ()
underSpan Maybe SpanContext
recorded IO ()
call = (Context -> Context) -> IO ()
forall (m :: * -> *). MonadIO m => (Context -> Context) -> m ()
adjustContext ((Context -> Context)
-> (SpanContext -> Context -> Context)
-> Maybe SpanContext
-> Context
-> Context
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Context -> Context
removeSpan (Span -> Context -> Context
insertSpan (Span -> Context -> Context)
-> (SpanContext -> Span) -> SpanContext -> Context -> Context
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SpanContext -> Span
wrapSpanContext) Maybe SpanContext
recorded) IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
call