Using ekg-statsd to push custom metrics


In Haskell, metrics are usually gathered with EKG.

The package ekg-statsd allows to push EKG metrics over statsd. EKG allows you to have access to GC metrics, make sure you compile your application with "-with-rtsopts=-T -N" to enable profiling.

If you’re using warp, you can use wai-middleware-metrics to report request distributions (request count, responses count aggregated by status code, responses latency distribution).

Configure logs for your Haskell application

You need the following packages in your dependencies:

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

Then add this Hello World code to your application and modify it to fit your needs:

{-# LANGUAGE OverloadedStrings #-}
import           Control.Monad                   (when)
import           Network.Wai.Metrics             (WaiMetrics, metrics,
import           System.Metrics                  (newStore, registerGcMetrics)
import           System.Remote.Monitoring.Statsd (defaultStatsdOptions,
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
    return ()
  return waiMetrics

main = do
  waiMetrics <- handleMetrics
  scotty 8080 $ do
     middleware $ metrics waiMetrics
     get "/" $
       html $ "Hello world"