Tuesday, April 17, 2012

The ekg package can now export string labels

I'm happy to announce a new minor release of the ekg package. The ekg package lets you monitor running executables using your web browser, or any monitoring tool that can speak HTTP. New in this release is support for exporting labels.

A label is an arbitrary text string, set dynamically by the running executable. Labels are more flexible than counters and gauges (which can only represent numerical values.) Typical uses of labels include exporting the command line arguments the executable was started with, the host it's running on, etc. Since labels are arbitrary strings you can use labels to export any type of internal state that can be represented as a string. Here's a simple example:

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Concurrent
import Control.Exception
import qualified Data.Text as T
import System.Environment
import qualified System.Remote.Label as Label
import System.Remote.Monitoring

mean :: Fractional a => [a] -> a
mean xs = sum xs / fromIntegral (length xs)

main :: IO ()
main = do
    handle <- forkServer "localhost" 8000
    label <- getLabel "args" handle
    args <- getArgs
    Label.set label $ T.intercalate " " $ map T.pack args
    -- Busy work:
    let loop n = do
            evaluate $ mean [1..n]
            threadDelay 2000
            loop n
    loop 1000000

This functionality was contributed by Iustin Pop.

No comments:

Post a Comment