http-env-0.0.0.0: src/Network/HTTP/Env/Metrics.hs
{-# LANGUAGE LambdaCase #-}
-- | How the calls to a service's dependencies are going, for Prometheus.
module Network.HTTP.Env.Metrics
( httpEnvReportValue,
httpEnvSample,
)
where
import Network.HTTP.Env.CircuitBreaker (CircuitReport (..))
import Network.HTTP.Env.FailureBudget (FailureBudgetReport (..), FailureBudgetStatus (..))
import Network.HTTP.Env.HttpClient (HttpEnvReport (..))
import System.Metrics.Prometheus.Metric (MetricSample (..))
import System.Metrics.Prometheus.Metric.Gauge (GaugeSample (..))
import System.Metrics.Prometheus.MetricId (Labels, MetricId (MetricId), Name)
-- | The worse of what the breaker and the budget say, from 0 to 1 (healthy).
-- Probing ranks below an exhausted budget, because under probing every call but
-- the probe fails fast.
httpEnvReportValue :: HttpEnvReport -> Double
httpEnvReportValue report =
foldr
min
1
( maybe [] (pure . circuitValue) (httpEnvReportCircuit report)
++ maybe [] (pure . budgetValue . failureBudgetReportStatus) (httpEnvReportFailureBudget report)
)
where
circuitValue :: CircuitReport -> Double
circuitValue = \case
CircuitReportClosed -> 1
CircuitReportProbing -> 0.5
CircuitReportRejecting -> 0
budgetValue :: FailureBudgetStatus -> Double
budgetValue = \case
FailureBudgetStatusWithin -> 1
FailureBudgetStatusExhausted -> 0.75
-- | A gauge of a report, as 'httpEnvReportValue', under the given name and labels.
-- Give each env its own labels, or two envs collapse into one series.
httpEnvSample :: Name -> Labels -> HttpEnvReport -> (MetricId, MetricSample)
httpEnvSample metricName reportLabels report =
( MetricId metricName reportLabels,
GaugeMetricSample (GaugeSample (httpEnvReportValue report))
)