npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

metrics-influxdb

v1.0.3

Published

InfluxDB backend integration for metrics

Downloads

3,901

Readme

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:

The udp protocol accepts the following additional options:

The http and https protocols accept the following additional options:

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.