prometheus-wai 0.0.0.0 → 0.0.0.1
raw patch · 3 files changed
+52/−16 lines, 3 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ System.Metrics.Prometheus.Wai.Middleware: WaiMetrics :: !Map Int Counter -> !Histogram -> WaiMetrics
+ System.Metrics.Prometheus.Wai.Middleware: [waiMetricsDuration] :: WaiMetrics -> !Histogram
+ System.Metrics.Prometheus.Wai.Middleware: [waiMetricsStatusCode] :: WaiMetrics -> !Map Int Counter
+ System.Metrics.Prometheus.Wai.Middleware: data WaiMetrics
+ System.Metrics.Prometheus.Wai.Middleware: metricsEndpointAtMiddlewareWithHook :: IO () -> ByteString -> Registry -> Middleware
Files
- CHANGELOG.md +9/−0
- prometheus-wai.cabal +3/−1
- src/System/Metrics/Prometheus/Wai/Middleware.hs +40/−15
+ CHANGELOG.md view
@@ -0,0 +1,9 @@+# Changelog++## [0.0.0.1] - 2025-07-14++### Added++- `--skip-passed`: Skip passing tests and rerun once the entire suite is+ skipped.+
prometheus-wai.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: prometheus-wai-version: 0.0.0.0+version: 0.0.0.1 description: Prometheus metrics for WAI applications homepage: https://github.com/NorfairKing/prometheus-wai#readme bug-reports: https://github.com/NorfairKing/prometheus-wai/issues@@ -14,6 +14,8 @@ copyright: Copyright (c) 2025 Tom Sydney Kerckhove license: MIT build-type: Simple+extra-source-files:+ CHANGELOG.md source-repository head type: git
src/System/Metrics/Prometheus/Wai/Middleware.hs view
@@ -4,9 +4,11 @@ module System.Metrics.Prometheus.Wai.Middleware ( registerWaiMetrics,+ WaiMetrics (..), instrumentWaiMiddleware, metricsEndpointMiddleware, metricsEndpointAtMiddleware,+ metricsEndpointAtMiddlewareWithHook, ) where @@ -31,8 +33,6 @@ import qualified System.Metrics.Prometheus.MetricId as Labels import qualified System.Metrics.Prometheus.MetricId as Prometheus (Labels (..)) --- TODO ignore websocket requests when gathering info- data WaiMetrics = WaiMetrics { waiMetricsStatusCode :: !(Map Int Prometheus.Counter), waiMetricsDuration :: !Prometheus.Histogram@@ -74,29 +74,54 @@ -- | Record the given Wai metrics in a middleware. instrumentWaiMiddleware :: WaiMetrics -> Wai.Middleware-instrumentWaiMiddleware WaiMetrics {..} application request sendResponse = do- begin <- getMonotonicTimeNSec- application request $ \response -> do- end <- getMonotonicTimeNSec+instrumentWaiMiddleware WaiMetrics {..} application request sendResponse =+ let isWebSocketsReq =+ lookup "upgrade" (Wai.requestHeaders request) == Just "websocket"+ shouldInstrument =+ -- Don't instrument WebSocket requests because they don't have a+ -- response but some libraries still pretend that it does and will+ -- give it a 500 status code.+ -- Moreover, the 'latency' will be 'how long the connection was open'+ -- which is also useless.+ not isWebSocketsReq+ in if shouldInstrument+ then do+ begin <- getMonotonicTimeNSec+ application request $ \response -> do+ end <- getMonotonicTimeNSec - -- Count the status code- mapM_ Counter.inc (M.lookup (HTTP.statusCode (Wai.responseStatus response)) waiMetricsStatusCode)+ -- Count the status code+ mapM_ Counter.inc (M.lookup (HTTP.statusCode (Wai.responseStatus response)) waiMetricsStatusCode) - -- Count the application response duration- let nanos = end - begin- millis = fromIntegral nanos / 1_000_000- Histogram.observe millis waiMetricsDuration+ -- Count the application response duration+ let nanos = end - begin+ millis = fromIntegral nanos / 1_000_000+ Histogram.observe millis waiMetricsDuration - sendResponse response+ sendResponse response+ else application request sendResponse --- | Add a metrics endpoint using the given industry.+-- | Add a metrics endpoint using the given registry. metricsEndpointMiddleware :: Registry -> Wai.Middleware metricsEndpointMiddleware = metricsEndpointAtMiddleware "/metrics" +-- | Add a metrics endpoint at a given path using the given registry. metricsEndpointAtMiddleware :: ByteString -> Registry -> Wai.Middleware-metricsEndpointAtMiddleware path registry application request sendResponse =+metricsEndpointAtMiddleware =+ metricsEndpointAtMiddlewareWithHook (pure ())++-- | Add a metrics endpoint at a given path and last-minute hook using the given registry.+--+-- This lets you set metrics that are evaluated when the /metrics endpoint is hit.+metricsEndpointAtMiddlewareWithHook ::+ IO () ->+ ByteString ->+ Registry ->+ Wai.Middleware+metricsEndpointAtMiddlewareWithHook lastMinuteHook path registry application request sendResponse = if Request.rawPathInfo request == path then do+ lastMinuteHook s <- Registry.sample registry sendResponse $ Wai.responseBuilder