Skip to content
EKG and StatsD

Export Haskell metrics with ekg-statsd

Use EKG to collect runtime metrics and ekg-statsd to send them to the StatsD endpoint available to Clever Cloud applications. Applications built on WAI can also use wai-middleware-metrics to record request counts, response status codes and latency distributions.

Configure metrics

Add these packages to the executable’s build-depends in your Cabal file:

  • ekg-core
  • ekg-statsd
  • scotty
  • wai-middleware-metrics

Enable the runtime statistics required by registerGcMetrics in the executable configuration:

ghc-options: -threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T

Create the metric store, start the StatsD reporter and install the WAI middleware:

{-# LANGUAGE OverloadedStrings #-}
import           Control.Monad                   (when)
import           Data.Maybe                      (fromMaybe)
import           Network.Wai.Metrics             (WaiMetrics, metrics,
                                                  registerWaiMetrics)
import           System.Environment              (lookupEnv)
import           System.Metrics                  (newStore, registerGcMetrics)
import           System.Remote.Monitoring.Statsd (defaultStatsdOptions,
                                                  forkStatsd)
import           Web.Scotty

handleMetrics :: IO WaiMetrics
handleMetrics = do
  store <- newStore
  registerGcMetrics store
  waiMetrics <- registerWaiMetrics store
  sendMetrics <- maybe False (== "true") <$> lookupEnv "ENABLE_METRICS"
  when sendMetrics $ do
    putStrLn "statsd reporting enabled"
    _ <- forkStatsd defaultStatsdOptions store
    pure ()
  pure waiMetrics

main :: IO ()
main = do
  waiMetrics <- handleMetrics
  port <- read . fromMaybe "8080" <$> lookupEnv "PORT"
  scotty port $ do
    middleware $ metrics waiMetrics
    get "/" $ html "Hello world"

Enable reporting in the application environment, then deploy:

clever env set ENABLE_METRICS true
clever deploy

The reporter’s default host and port match the local StatsD endpoint exposed to the application, so no endpoint configuration is required.

Last updated on