aggregate metrics in memory for logging
npm install @emartech/aggregate-metric-loggerAggregate metrics in memory and send them to GCP them every minute.
Set METRIC_LOGGER_ENABLED env variable to true for aggregate-metric-logger to
start collecting and logging measurements.
Set MONITORING_PROJECT_ID and GCP_CREDENTIALS env variables. HOSTNAME env variable also has to be
set, but for a pod running on kubernetes this should already be set.
Add Monitoring Metrics Writer Role to the service account in the GCP project.
``js
const metricLogger = require('@emartech/aggregate-metric-logger')
metricLogger.count('etwas-went-wrong', { customer_id: 11 });
metricLogger.count('etwas-went-wrong', { customer_id: 11 });
// will aggregate these to a single metric
`
If you want aggregate measurements about something you need to simply call the measure method
for each value:
`js
const metricLogger = require('@emartech/aggregate-metric-logger')
metricLogger.measure('thing-to-measure', 14, { customer_id: 12 })
metricLogger.measure('thing-to-measure', 20, { customer_id: 44 })
`
For duration measurements, there are two convenient methods: start, and stop;
You start the measurement with start, the counted value for aggregation will be the durationstart
between this and the matching stop call;
`js``
function exampleIOHeavyFunction() {
const measurement = metricLogger.start('tag-for-the-measuement', { customer_id: 23 });
const result = db.findItems();
metricLogger.stop(measurement);
return result;
}