Skip to content
InfluxDB backend integration for metrics
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
LICENSE
README.md
package-lock.json
package.json

README.md

InfluxDB reporter for metrics

Build Status Dependency Status devDependency Status

A node.js InfluxDB v0.9 and higher reporting backend for metrics. (Tested at least with InfluxDB 0.9 and 0.12.)

Installation

$ npm install metrics-influxdb

Usage

"use strict";

var InfluxMetrics = require('metrics-influxdb');

var reporter = new InfluxMetrics.Reporter({ protocol: 'udp', tags: { 'server': 'one' } });
var c = new InfluxMetrics.Counter();
reporter.addMetric('test.counter', c);
c.inc();

var g = new InfluxMetrics.Gauge();
reporter.addMetric('test.gauge', g);
g.set(10);

var h = new InfluxMetrics.Histogram();
reporter.addMetric('test.histogram', h);
h.update(50);

var m = new InfluxMetrics.Meter();
reporter.addMetric('test.meter', m);
m.mark(1);

var t = new InfluxMetrics.Timer();
reporter.addMetric('test.timer', t);
t.update(50);

reporter.report(); // Send metrics to InfluxDB
reporter.report(false); // Force flush of all metrics in buffer

reporter.start(1000) // Schedule report to be run every 1000 ms (also available through options)
reporter.stop() // Stop scheduled reporter 

Configuration

The options object accepts the following fields:

ParameterTypeDefaultDescription
host string 127.0.0.1 InfluxDB host
port number 8089(udp) / 8086(http) InfluxDB port
protocol string udp InfluxDB protocol (udp / http / https)
tags object {} Tags to add to influxdb measurements
tagger function none Function invoked with the metric key and expected to return the tags for it in the form {tag1: value1, tag2: value2, ...}
namer function identity Function invoked with the metric key and expected to return the desired name of the metric in InfluxDB
metricReportedHook function none Function invoked with `(metric key, Metric)` after it has been reported - so that the user can f.ex. `.clear()` counters.
fieldFilter function none Function invoked with `(metric key, Metric, fieldName, fieldValue)`. The field is only sent to InfluxDB if the function returns `true` for it.
precision string ms n/u/ms/s/m/h
bufferSize number 0 Number of points to keep before sending to InfluxDB
scheduleInterval number null This is the time in ms to flush any buffered metrics
skipIdleMetrics boolean false Suppress sending of metrics if there has been no new updates from previous report
callback function none A standard Node callback invoked with the result of the InfluxDB call (nothing or an `Error`).

The udp protocol accepts the following additional options:

ParameterTypeDefaultDescription
maxPacketSize integer 1024 Maximum safe UDP packet size to use

The http and https protocols accept the following additional options:

ParameterTypeDefaultDescription
username string null InfluxDB username
password string null InfluxDB password
database string null InfluxDB database
consistency string null one/quorum/all/any
httpTimeout number 200 http Timeout ms
httpAgent object undefined https Agent configuration

A more complex example

const InfluxMetrics = require('metrics-influxdb');
const options = {
    host: "my.influxdb.example.com",
    port: 8086,
    protocol: "http",
    username: "the monitor",
    password: "super sercret",
    database: "app_metrics",
    tags: {
        app: "my-awesome-app",
        environment: "production"
    },
    callback(error) {
        if (error) {
            console.log("Sending data to InfluxDB failed: ", error);
        }
    },
    /* Given a key like "type=system.area=memory.metric=heapUsed"
     * produce `{ type: "system", area: "memory", metric: "heapUsed" }`
     * so that these tags will be stored with the measure in InfluxDB
     */
    tagger: (metricKey) => _.chain(metricKey.split("."))
        .map((tagVal) => {
            const [_str, key, val] = tagVal.match(/(\w+)=(.+)/) || [];
            if (!key || !val) {
                console.log(`Expected format 'key=value[.key2=val2...]' but got the non-conforming part '${tagVal}'.`);
                return undefined;
            }
            else return [key, val];
        })
        .reject(_.isUndefined)
        .zipObject()
        /* Replace " ,=" - see https://docs.influxdata.com/influxdb/v0.13/write_protocols/write_syntax/#escaping-characters */
        .mapValues((value) => value.replace(/([ ,=])/g, "\\$1");)
        .value(),
    namer: (metricKey, tags) => {
        // We need a user-friendly name for the metrics ("measurements" in InfluxDB) that do not contain dots
        // as Grafana seems to have issues with those
        if (tags.type && tags.metric) {
            return tags.type + "_" + tags.metric;
        }
        return metricKey;
    },
    metricReportedHook: (key, metric) => {
        // Reset metrics as InfluxDB will do the aggregation and we should thus only send it new values
        metric.clear();
    },
    fieldFilter: (key, metric, fieldName, fieldValue) => {
        // Don't send unnecessary data to the DB to save CPU and disk space:
        // don't send 0s (likely many, since we clear metrics after send) and
        // various aggregated measures (percentiles etc.) since the DB will 
        // aggregate for us (well, as best as possible given 1 min-summarized data)
        if (metric.type === "histogram") {
            return metric.count > 0 && ["count", "min", "max", "mean", "median"].includes(fieldName);
        }
        if (metric.type === "timer") {
            return metric.duration.count > 0 && ["count", "min", "max", "mean", "median"].includes(fieldName);
        }
        if (metric.type === "counter") {
            return fieldValue > 0;
        }
        return true;
    }
};

const reporter = new InfluxMetrics.Reporter(options);

const h = new InfluxMetrics.Histogram();
reporter.addMetric('type=system.area=memory.metric=heapUsed', c);
h.update(process.memoryUsage().heapUsed);

reporter.start(1000);

Credits

This module is based on metrics-influxdb by dropwizard.

License

The MIT License (MIT)

Copyright (c) 2015 Brandon Hamilton

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You can’t perform that action at this time.