packages feed

wai-middleware-prometheus 0.1.1 → 0.2.0

raw patch · 2 files changed

+17/−16 lines, 2 filesdep +clockdep −timePVP ok

version bump matches the API change (PVP)

Dependencies added: clock

Dependencies removed: time

API changes (from Hackage documentation)

Files

src/Network/Wai/Middleware/Prometheus.hs view
@@ -11,15 +11,16 @@ ,   metricsApp ) where -import Data.Time.Clock (UTCTime, diffUTCTime, getCurrentTime) import qualified Data.ByteString.Builder as BS import qualified Data.ByteString.Char8 as BS import qualified Data.Default as Default import Data.Maybe (fromMaybe)+import Data.Ratio ((%)) import qualified Data.Text as T import qualified Network.HTTP.Types as HTTP import qualified Network.Wai as Wai import qualified Prometheus as Prom+import System.Clock (Clock(..), TimeSpec, diffTimeSpec, getTime, toNanoSecs)   -- | Settings that control the behavior of the Prometheus middleware.@@ -48,11 +49,11 @@ {-# NOINLINE requestLatency #-} -- XXX: https://prometheus.io/docs/practices/naming/ says this should be -- _seconds, not _microseconds.-requestLatency :: Prom.Metric (Prom.Vector Prom.Label3 Prom.Summary)+requestLatency :: Prom.Metric (Prom.Vector Prom.Label3 Prom.Histogram) requestLatency = Prom.unsafeRegisterIO $ Prom.vector ("handler", "method", "status_code")-                                       $ Prom.summary info Prom.defaultQuantiles-    where info = Prom.Info "http_request_duration_microseconds"-                           "The HTTP request latencies in microseconds."+                                       $ Prom.histogram info Prom.defaultBuckets+    where info = Prom.Info "http_request_duration_seconds"+                           "The HTTP request latencies in seconds."  -- | Instrument a WAI app with the default WAI metrics. --@@ -63,12 +64,12 @@               -> Wai.Application  -- ^ The app to instrument               -> Wai.Application  -- ^ The instrumented app instrumentApp handler app req respond = do-    start <- getCurrentTime+    start <- getTime Monotonic     app req $ \res -> do-        end <- getCurrentTime+        end <- getTime Monotonic         let method = Just $ BS.unpack (Wai.requestMethod req)         let status = Just $ show (HTTP.statusCode (Wai.responseStatus res))-        observeMicroSeconds handler method status start end+        observeSeconds handler method status start end         respond res  -- | Instrument an IO action with timing metrics. This function can be used if@@ -82,15 +83,15 @@              -> IO a    -- ^ The IO action to instrument              -> IO a    -- ^ The instrumented app instrumentIO label io = do-    start  <- getCurrentTime+    start  <- getTime Monotonic     result <- io-    end    <- getCurrentTime-    observeMicroSeconds label Nothing Nothing start end+    end    <- getTime Monotonic+    observeSeconds label Nothing Nothing start end     return result -observeMicroSeconds :: String -> Maybe String -> Maybe String -> UTCTime -> UTCTime -> IO ()-observeMicroSeconds handler method status start end = do-    let latency = fromRational $ toRational (end `diffUTCTime` start) * 1000000+observeSeconds :: String -> Maybe String -> Maybe String -> TimeSpec -> TimeSpec -> IO ()+observeSeconds handler method status start end = do+    let latency = fromRational $ toRational (toNanoSecs (end `diffTimeSpec` start) % 1000000000)     Prom.withLabel (handler, fromMaybe "" method, fromMaybe "" status)                    (Prom.observe latency)                    requestLatency
wai-middleware-prometheus.cabal view
@@ -1,5 +1,5 @@ name:                wai-middleware-prometheus-version:             0.1.1+version:             0.2.0 synopsis:     WAI middlware for exposing http://prometheus.io metrics. description:@@ -26,11 +26,11 @@   build-depends:       base               >=4.7 && <5     , bytestring         >=0.9+    , clock     , data-default     , http-types     , prometheus-client     , text               >=0.11-    , time     , wai                >=3.0   ghc-options: -Wall