sre-domain-node-metrics

A node library to capture metrics.

Usage no npm install needed!

<script type="module">
  import sreDomainNodeMetrics from 'https://cdn.skypack.dev/sre-domain-node-metrics';
</script>

README

Domain Node Influxdb metrics reporter

This is a node metrics library to push application metrics to influxdb. This library exports two module. One provides a express middleware to calculate response times metrics and other provides a reporter registry to push metrics to influxdb

Installation

yarn add @domain-group/sre-domain-node-metrics

Usage

  const express = require('express');
  const { getMetricsMiddleware, getMetricsReporter } = require('@domain-group/sre-domain-node-metrics');

  const options = {
    host: 'localhost',
    port: 8086,
    protocol: 'http',
    username: 'username',
    password: 'password',
    database: 'database',
    application: process.env.APPLICATION_PREFIX,
    metrics: 'response_times',
  };

  const reporter = getMetricsReporter(options);

/**

   * Creates an Express middleware that reports a http metric responsetime on request data.
   * With this middleware you will get response time all filterable by status codes, http method, and uri paths.
   *
   * @param {getMetricsReporter} reporter
   * @param {winston} logger
   
*/

  const middleware = getMetricsMiddleware(reporter, logger);

  const app = express();
  app.use(middleware);

  // Implement the rest of app

Configuration

The options object accepts the following fields:

ParameterTypeDefaultDescription
host string localhost InfluxDB host
port number 8086 InfluxDB port
protocol string http InfluxDB protocol (http/https)
username string none Username to connect to InfluxDB
password string none Password to connect to InfluxDB
database string none InfluxDB database
application string none Name of the application
metrics string none InfluxDB measurements
interval number 20000 Interval in ms to push data to influx

A complete example with logger

  const winston = require('winston');
  const express = require('express');
  const { getMetricsMiddleware, getMetricsReporter } = require('@domain-group/sre-domain-node-metrics');

  const logger = winston.createLogger({
  transports: [
    new winston.transports.Console({
      level: process.env.LOG_LEVEL,
      handleExceptions: true,
      json: true,
      colorize: process.env.NODE_ENV !== 'production',
      timestamp: true,
      humanReadableUnhandledException: true,
    })
  ],
  exitOnError: false,
});

  const options = {
    host: 'localhost',
    port: 8086,
    protocol: 'http',
    username: 'username',
    password: 'password',
    database: 'database',
    application: process.env.APPLICATION_PREFIX,
    metrics: 'response_times',
    interval: 5000
  };

  const reporter = getMetricsReporter(options);

  const middleware = getMetricsMiddleware(reporter, logger);

  const app = express();
  app.use(middleware);

  // Implement the rest of app