Basic Metrics
This library is supposed to be the foundation for reporting metrics. It shouldn't matter what stack
you decided to go with whether it be StatsD, Prometheus or New Relic. We all implement metrics the
same way in our internal libraries. This library was created so we could have metrics gathered without
opinions. Meaning your metric naming can be gathered for whatever stack you decided to go with. No naming
convention is enforced.
Notes during unstable release
Right now this library is unstable as we figure out what the format this should be in. As of today I think
Counters and Gauges solve the current problems that I need to solve for my use cases. Summaries and histograms
will need additional work. Contracts may change depending on the needs of the community or as problems are discovered.
Looking forward
The main idea for this is to have two options of getting metrics.
The first use case would be up to the user to implement their own interval for reporting. This library will
expose the necessary endpoints for you to gather the metrics that have been accounted for.
The second use case is a little more advanced. I would like this lib to be able to account for burst metrics.
As an example, if the user sets a limit of 200 metrics per summary, and if we have more than 200 metrics coming in
for a given interval, metrics will be lost. This would mean the library could emit when it is time to announce metrics
to a given platform. And also increase that frequency in the case of burst requests/metrics so that way we don't lose metrics.
This of course would be configurable and left up to the user to decide what best fits their scenario.
Getting started
npm i -S basic-metrics
yarn add basic-metrics
const { createMetricClient } = require('basic-metrics');
const client = createMetricClient({
name: 'my-metric-client',
attributes: {
host: process.env.HOST
}
});
setInterval(() => {
const payload = client.report();
axios.post('newrelic.com', payload);
}, 1000);
module.exports = client;
const client = require('./some-metric-file');
const attributes = { DOH: 'https://will.ruin.net/sarcasm' };
const requestSuccess = client.createCounter({ name: 'axios.post.success', attributes });
const requestFail = client.createCounter({ name: 'axios.post.failed', attributes });
exports.makeHypotheticalDestructiveRequest = async () => {
try {
const result = await axios.post(attributes.DOH, 'Whereis Google.com');
requestSuccess.increment();
return result;
} catch (e) {
requestFail.increment();
console.error(e);
throw e;
}
};
Api
createMetricClient
const { createMetricClient } = require('basic-metrics');
const client = createMetricClient({
name: ''
attributes: {}
})
client.report();
client.createCounter
const { createMetricClient } = require('basic-metrics');
const client = createMetricClient();
const counter = client.createCounter({
name: 'my.counter.success',
attributes: {}
reportSettings: {
resetValueOnReport: false
}
});
counter.increment((num = 1));
counter.report();
Counter Report Object
{
name: 'my.counter',
value: 3,
type: 'count',
startTime: 123456,
endTime: 53211,
}
client.createGauge
const { createMetricClient } = require('basic-metrics');
const client = createMetricClient();
const gauge = client.createGuage({
name: 'my.gauge.variable',
attributes: {}
});
gauge.increment((num = 1));
gauge.decrement((num = 1));
gauge.set();
gauge.report();
Gauge Report Object
{
name: 'my.gauge',
value: 3,
type: 'gauge',
startTime: 123456,
endTime: 53211,
}
client.createSummary
const { createMetricClient } = require('basic-metrics');
const client = createMetricClient();
const sum = client.createSummary({
name: 'my.summary',
attributes: {}
});
sum.observe();
sum.report();
Summary Report Object
{
name: 'my.summary',
type: 'summary',
startTime: 123556,
endTime: 12357,
breakdown: {
count: 0,
min: 0,
max: 0,
sum: 0,
p99: 0,
p95: 0
},
attributes: {}
}