Log metrics with 'key=value'


Log metrics in a simple key=value format for easy parsing. Metric-log uses l2met conventions which are used by Heroku.

npm install --save metric-log

Here are the most important features:

L2met functions

var metric = require("metric-log");
metric("request", 1);
  // measure#request=1 
var metric = require("metric-log");
metric("response_time", 40, "ms");
  // measure#response_time=40ms 
var metric = require("metric-log");
metric("response_time", 40, "ms", {lib:'my-lib'});
  // measure#response_time=40ms lib=my-lib 

Complex objects can also be passed. Any nested objects/arrays will be converted to JSON.

var metric = require("metric-log");
metric({host: "", service: "requests", metric: 5, tags: ["requests", "testing"]});
  // service=requests metric=5 tags="[\"requests\",\"testing\"]" 

You can also setup a default context to be applied to each metric.

var metric = require("metric-log").context({host: ""});
metric("response_time", 12, "ms");
  // measure#response_time=12ms 

You can also inherit from parent contexts

var express = require("express")
  , metric = require("metric-log")
  , parent = metric.context({host: ""});
var app = express();
app.use(function(reqresnext) {
  req.metric = metric.context({request_id: req.get("x-request-id")}).use(parent);
app.get("/", function(reqres) {
  req.metric("home_page", 1); //this use case should probably use metric.count() 
  // request_id=12345 measure#home_page=1 

Helper function to profile processes like calling an api or database.

var metric = require('metric');
var end = metric.profile('my-api-call');
api('id', function(errresult){
  // measure#my-api-call=203ms 

You can also pass some metrics as a second parameter

var end = metric.profile('my-api-call', {at:"info", lib:"my-lib"});
api('id', function(errresult){
  // measure#my-api-call=203ms at=info lib=my-lib err=null 

Only first 2 parameters are required.

Useful for measuring data like latency.

var metric = require("metric-log");
metric.measure("response_time", 20);
  // measure#response_time=20 
metric.measure("response_time", 40, "ms", {lib:'my-lib'});
  // measure#response_time=40ms lib=my-lib 

Only first parameter is required.

Useful for counting business metrics or similar data.

var metric = require("metric-log");
  // count#action.login.success=1 
metric.count("action.login.failure", 5, {at:'home-page'});
  // count#action.login.failure=5 at=home-page 

Useful for sampling data like memory usage.

var metric = require("metric-log");
metric.sample("home.dyno1.load_avg_5", 232, "mb", {lib: 'home-app'});
  // sample#home.dyno1.load_avg_5=232mb lib=home-app 

Useful for logging infrequent or one-off events like deploys.

var metric = require("metric-log");
metric.event("title", "deploy", {"event#start_time": 1234567890, source: "dyno5"});
  // event#title=deploy event#start_time=1234567890 source=dyno5 
npm test

These were some benchmarks run on my MacBook Pro.

$ npm run-script bench
․metric(measure, value) 
   885739.5925597874 metrics/sec
․metric(measure, value, units) 
   787401.5748031496 metrics/sec
   1901140.6844106463 metrics/sec
   344589.9379738112 metrics/sec
․context(measure, value) 
   372023.8095238095 metrics/sec
․context(measure, value, units) 
   318066.15776081424 metrics/sec
   365363.5367190354 metrics/sec
   195694.71624266144 metrics/sec
  8 tests complete (20 seconds)