Log metrics in a simple key=value format for easy parsing. Metric-log uses l2met conventions which are used by Heroku.
Install
npm install --save metric-log
Primary Functions
Here are the most important features:
- metric()
measure#request=1
- metric.profile()
measure#my-api-call=203ms //profile duration
- metric.context() - (set a parent context which logs can inherit from)
- metric.measure()
measure#db.latency=530ms //similar to metric()
- metric.count()
count#action.home_page.login.success=1
- metric.sample()
sample#appName.dyno1.load_avg_5=234mb
- metric.event()
event#title=deploy event#starttime=23456432
API
metric(measure, value)
var metric = ; ; // measure#request=1
metric(measure, value, units)
var metric = ; ; // measure#response_time=40ms
metric(measure, value, units, obj)
var metric = ; ; // measure#response_time=40ms lib=my-lib
metric(obj)
Complex objects can also be passed. Any nested objects/arrays will be converted to JSON.
var metric = ; ; // host=my.host.com service=requests metric=5 tags="[\"requests\",\"testing\"]"
metric.context(obj)
You can also setup a default context to be applied to each metric.
var metric = ; ; // host=my.host.com measure#response_time=12ms
metric.context(obj).use(parentContext)
You can also inherit from parent contexts
var express = metric = parent = metric; var app = ; app; app;
metric.profile(id[, obj])
Helper function to profile processes like calling an api or database.
var metric = ; var end = metricprofile'my-api-call'; ;
You can also pass some metrics as a second parameter
var end = metricprofile'my-api-call' at:"info" lib:"my-lib"; ;
metric.measure(metric, value[, units, obj])
Only first 2 parameters are required.
Useful for measuring data like latency.
var metric = ; metric; // measure#response_time=20 metric; // measure#response_time=40ms lib=my-lib
metric.count(metric[, value, obj])
Only first parameter is required.
Useful for counting business metrics or similar data.
var metric = ; metric; // count#action.login.success=1 metric; // count#action.login.failure=5 at=home-page
metric.sample(metric, value[, units, obj])
Useful for sampling data like memory usage.
var metric = ; metric; // sample#home.dyno1.load_avg_5=232mb lib=home-app
metric.event(metric, value[, obj])
Useful for logging infrequent or one-off events like deploys.
var metric = ; metricevent"title" "deploy" "event#start_time": 1234567890 source: "dyno5"; // event#title=deploy event#start_time=1234567890 source=dyno5
Tests
npm test
Benchmarks
These were some benchmarks run on my MacBook Pro.
$ npm run-script bench ․metric 885739.5925597874 metrics/sec․metric 787401.5748031496 metrics/sec․metric 1901140.6844106463 metrics/sec․metric 344589.9379738112 metrics/sec․context 372023.8095238095 metrics/sec․context 318066.15776081424 metrics/sec․context 365363.5367190354 metrics/sec․context 195694.71624266144 metrics/sec 8 tests complete