{-# LANGUAGE OverloadedStrings #-}
module Instana.SDK.Internal.Metrics.Deltas
( deltaKeyList
, enrichWithDeltas
) where
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text)
import qualified Data.Text as T
import Instana.SDK.Internal.Metrics.Sample (InstanaMetricValue,
TimedSample)
import qualified Instana.SDK.Internal.Metrics.Sample as Sample
deltaKeyList :: [Text]
deltaKeyList =
[ "rts.gc.bytes_allocated"
, "rts.gc.num_gcs"
, "rts.gc.num_bytes_usage_samples"
, "rts.gc.cumulative_bytes_used"
, "rts.gc.bytes_copied"
, "rts.gc.init_cpu_ms"
, "rts.gc.init_wall_ms"
, "rts.gc.mutator_cpu_ms"
, "rts.gc.mutator_wall_ms"
, "rts.gc.gc_cpu_ms"
, "rts.gc.gc_wall_ms"
, "rts.gc.cpu_ms"
, "rts.gc.wall_ms"
]
enrichWithDeltas :: TimedSample -> TimedSample -> TimedSample
enrichWithDeltas previousSample currentSample =
let
currentTimestamp = Sample.timestamp currentSample
previousTimestamp = Sample.timestamp previousSample
deltaT = currentTimestamp - previousTimestamp
previousMetrics = Sample.sample previousSample
currentMetrics = Sample.sample currentSample
metricsWithDeltas =
HashMap.foldrWithKey
(addDeltaToSample deltaT previousMetrics)
currentMetrics
currentMetrics
in
Sample.mkTimedSample metricsWithDeltas currentTimestamp
addDeltaToSample ::
Int
-> HashMap Text InstanaMetricValue
-> Text
-> InstanaMetricValue
-> HashMap Text InstanaMetricValue
-> HashMap Text InstanaMetricValue
addDeltaToSample deltaT previousMetrics metricKey currentMetricValue currentMetrics =
if not (elem metricKey deltaKeyList) then
currentMetrics
else
let
previousMetricValue = HashMap.lookup metricKey previousMetrics
in
case (previousMetricValue, currentMetricValue) of
(Just (Sample.IntegralValue previousValue),
Sample.IntegralValue currentValue) ->
addNormalizedDelta
metricKey
deltaT
(currentValue - previousValue)
currentMetrics
(Nothing, Sample.IntegralValue currentValue) ->
addNormalizedDelta
metricKey
deltaT
currentValue
currentMetrics
_ ->
currentMetrics
addNormalizedDelta ::
Text
-> Int
-> Int
-> HashMap Text InstanaMetricValue
-> HashMap Text InstanaMetricValue
addNormalizedDelta metricKey deltaT deltaV metrics =
let
deltaKey = T.append metricKey "_delta"
normalizedDeltaV =
Sample.FractionalValue $
(fromIntegral deltaV / fromIntegral deltaT) * 1000
in
HashMap.insert
deltaKey
normalizedDeltaV
metrics