Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable bucket for latency metric #69

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions wai-middleware-prometheus/src/Network/Wai/Middleware/Prometheus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Network.Wai.Middleware.Prometheus
, Default.def
, instrumentHandlerValue
, instrumentHandlerValueWithFilter
, instrumentHandlerValueWithHistogramAndFilter
, ignoreRawResponses
, instrumentApp
, instrumentIO
Expand Down Expand Up @@ -80,7 +81,16 @@ instrumentHandlerValueWithFilter ::
-> (Wai.Request -> Text) -- ^ The function used to derive the "handler" value in Prometheus
-> Wai.Application -- ^ The app to instrument
-> Wai.Application -- ^ The instrumented app
instrumentHandlerValueWithFilter resFilter f app req respond = do
instrumentHandlerValueWithFilter =
instrumentHandlerValueWithHistogramAndFilter requestLatency

instrumentHandlerValueWithHistogramAndFilter ::
Prom.Vector Prom.Label3 Prom.Histogram
-> (Wai.Response -> Maybe Wai.Response) -- ^ Response filter
-> (Wai.Request -> Text) -- ^ The function used to derive the "handler" value in Prometheus
-> Wai.Application -- ^ The app to instrument
-> Wai.Application -- ^ The instrumented app
instrumentHandlerValueWithHistogramAndFilter histogram resFilter f app req respond = do
start <- getTime Monotonic
app req $ \res -> do
case resFilter res of
Expand All @@ -89,7 +99,7 @@ instrumentHandlerValueWithFilter resFilter f app req respond = do
end <- getTime Monotonic
let method = Just $ decodeUtf8 (Wai.requestMethod req)
let status = Just $ T.pack (show (HTTP.statusCode (Wai.responseStatus res')))
observeSeconds (f req) method status start end
observeSecondsWithHistogram histogram (f req) method status start end
respond res

-- | 'Wai.ResponseRaw' values have two parts: an action that can be executed to construct a
Expand Down Expand Up @@ -149,10 +159,21 @@ observeSeconds :: Text -- ^ handler label
-> TimeSpec -- ^ start time
-> TimeSpec -- ^ end time
-> IO ()
observeSeconds handler method status start end = do
observeSeconds = do
observeSecondsWithHistogram requestLatency

-- | Record an event to the middleware metric.
observeSecondsWithHistogram :: Prom.Vector Prom.Label3 Prom.Histogram
-> Text -- ^ handler label
-> Maybe Text -- ^ method
-> Maybe Text -- ^ status
-> TimeSpec -- ^ start time
-> TimeSpec -- ^ end time
-> IO ()
observeSecondsWithHistogram histograms handler method status start end = do
let latency :: Double
latency = fromRational $ toRational (toNanoSecs (end `diffTimeSpec` start) % 1000000000)
Prom.withLabel requestLatency
Prom.withLabel histograms
(handler, fromMaybe "" method, fromMaybe "" status)
(flip Prom.observe latency)

Expand Down