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

fh-component-metrics

v2.7.0

Published

influx db metrics wrapper

Downloads

160

Readme

fh-component-metrics

This component is used to gather metrics such as CPU and memory usage. Using InfluxDB and Grafana you can easily visualise the metrics.

The following docker image was used to build the necessary infrastructure to investigate this: https://github.com/StackPointCloud/docker-influxdb

Influxdb Version

The 2.x version of this component is compatible with 0.10 release of Influxdb. It is using the line protocol to send metrics data over UDP port. Please make sure UDP is enabled in the Influxdb configurations.

Usage Guide

Here are the steps to use this module in an existing RHMAP component:

  1. add this module as a dependency:

    npm install fh-component-metrics --save
  2. Then you can capture CPU & memory usage in the component use this code:

    var fhComponentMetrics = require('fh-component-metrics');
    var metricsConf = {enabled: true, host: '1.2.3.4', port: 2003, baseTags: {appId: process.env.APPID}};
    var metrics = fhComponentMetrics(metricsConf);
    var TITLE = 'myTestComponent';
    metrics.memory(TITLE, { interval: 2000 }, function(err) {
      if (err) logger.warn(err);
    });
    
    metrics.cpu(TITLE, { interval: 1000 }, function(err) {
      if (err) logger.warn(err);
    });
  3. Send metrics data to multiple backends. By default, if you only need to send metrics data to an Influxdb backend, you can specify the configuration as the example above. But it also supports some other backends like Statsd and it can send the metrics data to multiple backends at the same time. To do that, you just need to change the configuration and replace the host and port value with an array called backends:

    var fhComponentMetrics = require('fh-component-metrics');
    var metricsConf = {enabled: true, backends:[{type: 'influxdb', host: '1.2.3.4', port: 2003}, {type: 'statsd', host: '1.2.3.4', port: 2004}], baseTags: {appId: process.env.APPID}};
    var metrics = fhComponentMetrics(metricsConf);
    //the metrics data will be sent to both the influxdb and statsd backend

At the moment, it supports Influxdb, Statsd and redis, so the valid options for the type field are influxdb, statsd and redis. See the next section for more details about the available options for each type.

  1. To capture API time, you can add the timingMiddleware to an existing express app like this:

    var fhComponentMetrics = require('fh-component-metrics');
    var metricsConf = {enabled: true, host: '1.2.3.4', port: 2003, baseTags: {appId: process.env.APPID}};
    var app = express();
    app.use(fhComponentMetrics.timingMiddleware('myExpressApp', metricsConf));
  2. It's better the add the metrics configuration into the component's configuation file. E.g.

    {
      ...,
      "component_metrics": {
        "enabled": true,
        "host": "1.2.3.4",
        "port": 2003
      },
      ...
    }

Configuration Options for Each Backend Type

Influxdb

  • host: the host of the influxdb
  • port: the port of the influxdb. It has to be a UDP port.

Statsd

  • host: the host of the statsd server
  • port: the port of the statsd server. It has to be a UDP port.
  • keyBuilder: a function that will allow override how the metric key is being built. If not provided, a default function will be provided. See statsd.js for more details.

NOTE: each metric will be formatted as described in this spec before sending to the server.

Redis

  • redisClient: an instance of https://github.com/NodeRedis/node_redis#rediscreateclient.
  • recordsToKeep: how many records to keep in redis
  • namespace: a prefix that will be added to each metric key

NOTE: each record will be added to a list in Redis. The key for the list is the metric name. Each record will is a stringified JSON object, with the follow fields:

  • tags
  • fields
  • ts

Limitations

CPU metrics are supported only on Linux based operating systems.