wai-middleware-metrics-0.2.1: A WAI middleware to collect EKG request metrics

LicenseBSD3
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Network.Wai.Metrics

Description

A WAI middleware to collect the following EKG metrics from compatible web servers:

  • number of requests (counter wai.request_count)
  • number of server errors (counter wai.server_error_count)
  • latency distribution (distribution wai.latency_distribution)

Here's an example of reading these metrics from a Scotty server, and displaying them with EKG.

-- Compile with GHC option `-with-rtsopts=-T` for GC metrics
import Web.Scotty
import Control.Applicative
import System.Remote.Monitoring (serverMetricStore, forkServer)
import Network.Wai.Metrics

main :: IO()
main = do
  store <- serverMetricStore <$> forkServer "localhost" 8000
  waiMetrics <- registerWaiMetrics store
  scotty 3000 $ do
    middleware (metrics waiMetrics)
    get "/" $ html "Ping"

Now have a look at your local EKG instance and display the request count by clicking on 'wai.request_count'.

WAI metrics can also be stored in a bare EKG store, with no UI and no GC metrics. Use ekg-core's newStore function.

Compatible web servers include the following:

  • Yesod
  • Scotty
  • Spock
  • Servant
  • Warp

Synopsis

Documentation

registerWaiMetrics :: Store -> IO WaiMetrics Source

Register in EKG a number of metrics related to web server activity.

  • wai.request_count
  • wai.server_error_count
  • wai.latency_distribution

data WaiMetrics Source

The metrics to feed in WAI and register in EKG.

metrics :: WaiMetrics -> Middleware Source

Create a middleware to be added to a WAI-based webserver.