packages feed

http-env-gen-0.0.0.0: test/Network/HTTP/Env/MetricsSpec.hs

{-# LANGUAGE OverloadedStrings #-}

module Network.HTTP.Env.MetricsSpec (spec) where

import Network.HTTP.Env.CircuitBreaker (CircuitReport (..))
import Network.HTTP.Env.FailureBudget (FailureBudgetReport (..), FailureBudgetStatus (..))
import Network.HTTP.Env.HttpClient (HttpEnvReport (..))
import Network.HTTP.Env.Metrics (httpEnvReportValue, httpEnvSample)
import qualified System.Metrics.Prometheus.Metric as Prometheus (MetricSample (..))
import qualified System.Metrics.Prometheus.Metric.Gauge as Prometheus (GaugeSample (..))
import qualified System.Metrics.Prometheus.MetricId as Labels
import qualified System.Metrics.Prometheus.MetricId as Prometheus (MetricId (..), Name (..))
import Test.Syd

spec :: Spec
spec = do
  let within =
        FailureBudgetReport
          { failureBudgetReportFailures = 1,
            failureBudgetReportStatus = FailureBudgetStatusWithin
          }
  let exhausted =
        FailureBudgetReport
          { failureBudgetReportFailures = 5,
            failureBudgetReportStatus = FailureBudgetStatusExhausted
          }
  let report circuit budget =
        HttpEnvReport
          { httpEnvReportCircuit = circuit,
            httpEnvReportFailureBudget = budget
          }

  -- [check:tag CircuitBreakerGaugeScale] A dashboard colours these four
  -- readings with thresholds of its own. A reading that moves, or a fifth
  -- one, needs a threshold step to match.
  describe "httpEnvReportValue" $ do
    it "reads the whole scale from healthy down to rejecting" $
      map
        httpEnvReportValue
        [ report (Just CircuitReportClosed) (Just within),
          report (Just CircuitReportClosed) (Just exhausted),
          report (Just CircuitReportProbing) (Just within),
          report (Just CircuitReportRejecting) (Just within)
        ]
        `shouldBe` [1, 0.75, 0.5, 0]
    it "reads the worse of the breaker and the budget" $
      map
        httpEnvReportValue
        [ report (Just CircuitReportProbing) (Just exhausted),
          report (Just CircuitReportRejecting) (Just exhausted)
        ]
        `shouldBe` [0.5, 0]
    it "reads an env with neither a breaker nor a budget as healthy" $
      httpEnvReportValue (report Nothing Nothing) `shouldBe` 1
    it "reads either half alone" $
      map
        httpEnvReportValue
        [ report Nothing (Just exhausted),
          report (Just CircuitReportRejecting) Nothing
        ]
        `shouldBe` [0.75, 0]

  describe "httpEnvSample" $
    it "reports the value of a report under the name and labels given" $
      case httpEnvSample "some_metric" (Labels.fromList [("dependency", "exhausted-one")]) (report (Just CircuitReportClosed) (Just exhausted)) of
        (metricId, Prometheus.GaugeMetricSample (Prometheus.GaugeSample value)) ->
          ( Prometheus.unName (Prometheus.name metricId),
            Labels.toList (Prometheus.labels metricId),
            value
          )
            `shouldBe` ("some_metric", [("dependency", "exhausted-one")], 0.75)
        _ -> expectationFailure "expected a gauge"