Skip to content

Latest commit

 

History

History
68 lines (52 loc) · 1.97 KB

README.md

File metadata and controls

68 lines (52 loc) · 1.97 KB

log-promise-metrics

Build Status

Promise based metrics library to dump metrics in a format consumable by datadog and librato drains

Installation

npm install @plan3-relate/log-promise-metrics

Usage

const metricsFactory = require('@plan3-relate/log-promise-metrics')(console.log, 'baseName');

const metrics = metricsFactory();

Measure time passed during promise execution

const getAllValues = require('request-promise')({url: ...});
metrics.timed('getAllValues', getAllValues)
    // some then's after
    .then(() => metrics.dropToLogs());
// measure#baseName.getAllValues.success_duration=10ms
// or
// measure#baseName.getAllValues.error_duration=10ms

Increment count by given value

const sessionCountPromise = Promise.resolve(['0000', '0001', '0002']);
metrics.increment('sessionsCount', v => v.length, sessionCountPromise)
    // some then's after
    .then(() => metrics.dropToLogs());
// count#baseName.sessionsCount=3

Increment count by 1

metrics.increment('visitorsCount')
    // some then's after
    .then(() => metrics.dropToLogs());

Use custom sample

const elementsInBasketPromise = require('pg-promise')({..}).query('SELECT ...');
metrics.sample('elementsInBasket', v => v, elementsInBasketPromise);
    // some then's after
    .then(() => metrics.dropToLogs());
// sample#baseName.elementsInBasket=10

... or, to get a histogram of those values:

const elementsInBasketPromise = require('pg-promise')({..}).query('SELECT ...');
metrics.sample('elementsInBasket', v => v, elementsInBasketPromise);
    // some then's after
    .then(() => metrics.dropToLogs());
// measure#elementsInBasket=42