prometheus-wai 0.0.0.1 → 0.1.0.0
raw patch · 3 files changed
+46/−28 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- System.Metrics.Prometheus.Wai.Middleware: metricsEndpointAtMiddleware :: ByteString -> Registry -> Middleware
- System.Metrics.Prometheus.Wai.Middleware: metricsEndpointAtMiddlewareWithHook :: IO () -> ByteString -> Registry -> Middleware
+ System.Metrics.Prometheus.Wai.Middleware: MetricsEndpoint :: ByteString -> Registry -> Maybe (IO RegistrySample) -> MetricsEndpoint
+ System.Metrics.Prometheus.Wai.Middleware: [metricsEndpointLastSecond] :: MetricsEndpoint -> Maybe (IO RegistrySample)
+ System.Metrics.Prometheus.Wai.Middleware: [metricsEndpointPath] :: MetricsEndpoint -> ByteString
+ System.Metrics.Prometheus.Wai.Middleware: [metricsEndpointRegistry] :: MetricsEndpoint -> Registry
+ System.Metrics.Prometheus.Wai.Middleware: combineRegistrySample :: RegistrySample -> RegistrySample -> RegistrySample
+ System.Metrics.Prometheus.Wai.Middleware: data MetricsEndpoint
+ System.Metrics.Prometheus.Wai.Middleware: defaultMetricsEndpoint :: Registry -> MetricsEndpoint
- System.Metrics.Prometheus.Wai.Middleware: metricsEndpointMiddleware :: Registry -> Middleware
+ System.Metrics.Prometheus.Wai.Middleware: metricsEndpointMiddleware :: MetricsEndpoint -> Middleware
Files
- CHANGELOG.md +7/−0
- prometheus-wai.cabal +1/−3
- src/System/Metrics/Prometheus/Wai/Middleware.hs +38/−25
CHANGELOG.md view
@@ -1,5 +1,12 @@ # Changelog +## [0.1.0.0] - 2025-07-14++### Changed++* Changed the metricsEndpointMiddleware API to support more bells and whistles+ without breaking the API in the future.+ ## [0.0.0.1] - 2025-07-14 ### Added
prometheus-wai.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: prometheus-wai-version: 0.0.0.1+version: 0.1.0.0 description: Prometheus metrics for WAI applications homepage: https://github.com/NorfairKing/prometheus-wai#readme bug-reports: https://github.com/NorfairKing/prometheus-wai/issues@@ -28,8 +28,6 @@ Paths_prometheus_wai hs-source-dirs: src- build-tool-depends:- autoexporter:autoexporter build-depends: base >=4.7 && <5 , bytestring
src/System/Metrics/Prometheus/Wai/Middleware.hs view
@@ -6,9 +6,10 @@ ( registerWaiMetrics, WaiMetrics (..), instrumentWaiMiddleware,+ combineRegistrySample,+ MetricsEndpoint (..),+ defaultMetricsEndpoint, metricsEndpointMiddleware,- metricsEndpointAtMiddleware,- metricsEndpointAtMiddlewareWithHook, ) where @@ -32,6 +33,7 @@ import qualified System.Metrics.Prometheus.Metric.Histogram as Prometheus (Histogram) import qualified System.Metrics.Prometheus.MetricId as Labels import qualified System.Metrics.Prometheus.MetricId as Prometheus (Labels (..))+import qualified System.Metrics.Prometheus.Registry as Prometheus (RegistrySample (..)) data WaiMetrics = WaiMetrics { waiMetricsStatusCode :: !(Map Int Prometheus.Counter),@@ -64,9 +66,9 @@ let durationBounds = concat [ [1, 2, 3, 5],- [10, 20, 30, 40, 50],- [100, 200, 300, 400, 500],- [1_000, 2_000, 3_000, 4_000, 5_000],+ [10, 20 .. 100],+ [100, 200 .. 900],+ [1_000, 2_000 .. 9_000], [10_000] ] waiMetricsDuration <- Prometheus.registerHistogram "http_request_duration_milliseconds" labels durationBounds registry@@ -101,31 +103,42 @@ sendResponse response else application request sendResponse --- | Add a metrics endpoint using the given registry.-metricsEndpointMiddleware :: Registry -> Wai.Middleware-metricsEndpointMiddleware = metricsEndpointAtMiddleware "/metrics"+combineRegistrySample :: Prometheus.RegistrySample -> Prometheus.RegistrySample -> Prometheus.RegistrySample+combineRegistrySample (Prometheus.RegistrySample a) (Prometheus.RegistrySample b) =+ Prometheus.RegistrySample (M.union a b) --- | Add a metrics endpoint at a given path using the given registry.-metricsEndpointAtMiddleware :: ByteString -> Registry -> Wai.Middleware-metricsEndpointAtMiddleware =- metricsEndpointAtMiddlewareWithHook (pure ())+data MetricsEndpoint = MetricsEndpoint+ { -- | Path to serve metrics on, e.g. @"/metrics"@+ metricsEndpointPath :: ByteString,+ -- | Registry to sample from+ metricsEndpointRegistry :: Registry,+ -- | Extra samples to be collected 'at the last second', to be combined+ -- with the main registry samples+ metricsEndpointLastSecond :: Maybe (IO Prometheus.RegistrySample)+ } --- | 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+defaultMetricsEndpoint :: Registry -> MetricsEndpoint+defaultMetricsEndpoint registry =+ MetricsEndpoint+ { metricsEndpointPath = "/metrics",+ metricsEndpointRegistry = registry,+ metricsEndpointLastSecond = Nothing+ }++metricsEndpointMiddleware :: MetricsEndpoint -> Wai.Middleware+metricsEndpointMiddleware MetricsEndpoint {..} application request sendResponse =+ if Request.rawPathInfo request == metricsEndpointPath then do- lastMinuteHook- s <- Registry.sample registry+ let sampleRegistry = Registry.sample metricsEndpointRegistry+ finalSamples <- case metricsEndpointLastSecond of+ Nothing -> sampleRegistry+ Just lastSecondSample ->+ combineRegistrySample+ <$> sampleRegistry+ <*> lastSecondSample sendResponse $ Wai.responseBuilder HTTP.ok200 [(HTTP.hContentType, "text/plain; version=0.0.4; charset=utf-8")]- $ Prometheus.encodeMetrics s+ $ Prometheus.encodeMetrics finalSamples else application request sendResponse