Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pmx

keymetrics936.9kMIT1.6.7

PM2/Keymetrics advanced API

cli, fault tolerant, sysadmin, tools, pm2, logs, log, json, express, hapi, kraken, reload, microservice, programmatic, harmony, node-pm2, production, keymetrics, node.js monitoring, strong-pm, deploy, deployment, daemon, supervisor, nodemon, pm2.io, ghost, ghost production, monitoring, process manager, forever, profiling, probes, process-metrics, process metrics, metrics, custom actions, forever-monitor, keep process alive, process configuration, clustering, cluster cli, cluster, cron, devops, dev ops

readme


PM2 programmatic integration



PMX allows you to create advanced interactions with PM2 and Keymetrics.io.

Table of Contents

Installation

Install pmx with npm:

$ npm install pmx --save

Expose Metrics: Measure anything

PMX allows you to expose code metrics from your code to the PM2 monit command or the Keymetrics Dashboard, in realtime and over time.

4 measurements are available:

  • Simple metrics: Values that can be read instantly
    • eg. Monitor variable value
  • Counter: Things that increment or decrement
    • eg. Downloads being processed, user connected
  • Meter: Things that are measured as events / interval
    • eg. Request per minute for a http server
  • Histogram: Keeps a reservoir of statistically relevant values biased towards the last 5 minutes to explore their distribution
    • eg. Monitor the mean of execution of a query into database

Metric: Simple value reporting

This allow to expose values that can be read instantly.

var probe = pmx.probe();

// Here the value function will be called each second to get the value
// returned by Object.keys(users).length
var metric = probe.metric({
  name    : 'Realtime user',
  value   : function() {
    return Object.keys(users).length;
  }
});

// Here we are going to call valvar.set() to set the new value
var metric_2 = probe.metric({
  name    : 'Realtime Value'
});

metric_2.set(23);

Options

  • name: Probe name
  • value: (optional) function that allows to monitor a global variable

Counter: Sequential value change

Things that increment or decrement.

var probe = pmx.probe();

// The counter will start at 0
var counter = probe.counter({
  name : 'Current req processed'
});

http.createServer(function(req, res) {
  // Increment the counter, counter will eq 1
  counter.inc();
  req.on('end', function() {
    // Decrement the counter, counter will eq 0
    counter.dec();
  });
});

Options

  • name: Probe name

Meter: Average calculated values

Things that are measured as events / interval.

var probe = pmx.probe();

var meter = probe.meter({
  name      : 'req/sec',
  samples   : 1  // This is per second. To get per min set this value to 60
});

http.createServer(function(req, res) {
  meter.mark();
  res.end({success:true});
});

Options

  • name: Probe name
  • samples: (optional)(default: 1) Rate unit. Defaults to 1 sec.
  • timeframe: (optional)(default: 60) timeframe over which events will be analyzed. Defaults to 60 sec.

Histogram

Keeps a resevoir of statistically relevant values biased towards the last 5 minutes to explore their distribution.

var probe = pmx.probe();

var histogram = probe.histogram({
  name        : 'latency',
  measurement : 'mean'
});

var latency = 0;

setInterval(function() {
  latency = Math.round(Math.random() * 100);
  histogram.update(latency);
}, 100);

Options

  • name: Probe name
  • agg_type : (optional)(default: none) Can be sum, max, min, avg (default) or none. It will impact the way the probe data are aggregated within the Keymetrics backend. Use none if this is irrelevant (eg: constant or string value).
  • alert : (optional)(default: null) For Meter and Counter probes. Creates an alert object (see below).

Expose Functions: Trigger Functions remotely

Remotely trigger functions from Keymetrics. These metrics takes place in the main Keymetrics Dashboard page under the Custom Action section.

Simple actions

Simple action allows to trigger a function from Keymetrics. The function takes a function as a parameter (reply here) and need to be called once the job is finished.

Example:

var pmx = require('pmx');

pmx.action('db:clean', function(reply) {
  clean.db(function() {
    /**
     * reply() must be called at the end of the action
     */
     reply({success : true});
  });
});

Scoped actions (beta)

Scoped Actions are advanced remote actions that can be also triggered from Keymetrics.

Two arguments are passed to the function, data (optional data sent from Keymetrics) and res that allows to emit log data and to end the scoped action.

Example:

pmx.scopedAction('long running lsof', function(data, res) {
  var child = spawn('lsof', []);

  child.stdout.on('data', function(chunk) {
    chunk.toString().split('\n').forEach(function(line) {
      res.send(line); // This send log to Keymetrics to be saved (for tracking)
    });
  });

  child.stdout.on('end', function(chunk) {
    res.end('end'); // This end the scoped action
  });

  child.on('error', function(e) {
    res.error(e);  // This report an error to Keymetrics
  });

});

Alert System for Custom Metrics

(Specific to Keymetrics)

This alert system can monitor a Probe value and launch an exception when hitting a particular value.

Example for a cpu_usage variable:

var metric = probe.metric({
  name  : 'CPU usage',
  value : function() {
    return cpu_usage;
  },
  alert : {
    mode  : 'threshold',
    value : 95,
    msg   : 'Detected over 95% CPU usage', // optional
    func  : function() { //optional
      console.error('Detected over 95% CPU usage');
    },
    cmp   : "<" // optional
  }
});

Options

  • mode : threshold, threshold-avg.
  • value : Value that will be used for the exception check.
  • msg : String used for the exception.
  • func : optional. Function declenched when exception reached.
  • cmp : optional. If current Probe value is not <, >, = to Threshold value the exception is launched. Can also be a function used for exception check taking 2 arguments and returning a bool.
  • interval : optional, threshold-avg mode. Sample length for monitored value (180 seconds default).
  • timeout : optional, threshold-avg mode. Time after which mean comparison starts (30 000 milliseconds default).

Report Alerts: Errors / Uncaught Exceptions

(Specific to Keymetrics)

By default once PM2 is linked to Keymetrics, you will be alerted of any uncaught exception. These errors are accessible in the Issue tab of Keymetrics.

Custom alert notification

If you need to alert about any critical errors you can do it programmatically:

var pmx = require('pmx');

pmx.notify({ success : false });

pmx.notify('This is an error');

pmx.notify(new Error('This is an error'));

Add Verbosity to an Alert: Express Error handler

When an uncaught exception is happening you can track from which routes it has been thrown. To do that you have to attach the middleware pmx.expressErrorHandler at then end of your routes mounting:

var pmx = require('pmx');

// All my routes
app.get('/' ...);
app.post(...);
// All my routes

// Here I attach the middleware to get more verbosity on exception thrown
app.use(pmx.expressErrorHandler());

Emit Events

Emit events and get historical and statistics. This is available in the Events page of Keymetrics.

var pmx = require('pmx');

pmx.emit('user:register', {
  user : 'Alex registered',
  email : 'thorustor@gmail.com'
});

Application level network traffic monitoring / Display used ports

You can monitor the network usage of a specific application by adding the option network: true when initializing PMX. If you enable the flag ports: true when you init pmx it will show which ports your app is listenting on.

These metrics will be shown in the Keymetrics Dashboard in the Custom Metrics section.

Example:

pmx.init({
  [...]
  network : true, // Allow application level network monitoring
  ports   : true  // Display ports used by the application
});

Advanced PMX configuration

var pmx = require('pmx').init({
  network       : true, // (default: false) Network monitoring at the application level
  ports         : true, // (default: false) Shows which ports your app is listening on
  // can be 'express', 'hapi', 'http', 'restify'
  excludedHooks: []
});

License

MIT

GA

changelog

1.6.7

  • test : fix some testsa about dependencies

1.6.6

  • hotfix : set collectDependencies to false by default

1.6.5

  • fix : allow to disable dependencies collection

1.6.3

  • feature : add deep metrics (mysql, mongo, mqtt, ws, ...)
  • feature : create probe only when receiving first data
  • fix : protect v8 module import, for old nodejs version
  • fix : add unit in probes

1.5.6

  • add process.getActiveRequests() and process.getActiveHandles() custom metrics
  • avoid console windows popping up on Windows 10 #105

1.5.5

  • Run test against node@9
  • #101 Initially suport es6 native modules @vpotseluyko
  • collect dependencies information + broadcast to pm2 via type : 'application:dependencies'

1.5.4

  • Fix support for Node.js 0.10

1.5.3

Profiling fix

1.5.2

Latest Stable Version

1.1.0

  • new tracing system

1.0.3

  • alias samples to seconds
  • Http probe req/s -> req/min

1.0.2

  • add stackframe to exception

1.0.1

  • Include transaction tracing system
  • Test on Node v7

0.6.8

  • Final unhandledRejection warning message

0.6.3

  • Add defaut custom action getEnv (display process environment variables)

0.6.2

  • [#68] Add unref to all background jobs to free the event loop
  • [#67] Stop clearing main module's path
  • New smart probe algorithm
  • Smart probes fixed (no null value anymore)

0.5.6

  • [#58] Clear all intervals when receiving kill signal from PM2 to free event loop
  • [#37] Don't mess with incoming error
  • right pmx version injected into module
  • if pmx cannot find package.json in current folder, try to require in parent folder
  • disable some error messages

0.5.4

  • axm_options now has PMX version (but only the value in the package.json)

0.5.3

  • Fix alert system
  • Change <>

0.5.0

  • Auto initialize Configuration
  • Enable alerts by default

0.4.0

  • Hide password once it's set
  • Do not force app keep alive when calling configureModule (already done when using probes)
  • alias action attribute to func in alert system + pass value
  • Attach auto alerts to all probes
  • append alert configuration to probes (subfield alert, attaching value threshold and interval (for threshold-avg)
  • Add autocast object system for configuration (WARNING!!! STRING WITH ONLY NUMBER WILL BE CAST TO INT)
  • BUG FIX: pmx.notify JSON | STRING create separated alerts (before it was not working because the stack trace was the same, coming from new Error in notify.js

Notes:

  • <input checked="" disabled="" type="checkbox"> for app, configuration is loaded depending on the application name declared in package.json
  • <input disabled="" type="checkbox"> configuration must be injected into raw Node.js applications
  • <input disabled="" type="checkbox"> uncomment Configuration.init(opts) in index.js for PMX.init

0.3.30

  • add alert_enabled field for .init() / .initModule()

0.3.29

  • Mode thresold-avg via binary heap
  • Alert system for counter
  • Better algorithm

0.3.28

  • Allow not passing any value to Metric probe

0.3.27

  • Declare var for Alert

0.3.26

  • Fix uncaught exception fork (allow override)

0.3.25

  • pmx.getConf() to get module configuration
  • add smart probes
  • fix null null null when passing length in error message
  • add field axm_monitor.module_conf (km hot display)
  • Scoped actions

0.2.27

  • Remove debug message
  • Rename module
  • Auto instanciation

0.2.25

  • Add ip address on each transaction

0.2.24

  • Add unit option for Histogram and Meter

0.2.23

  • Include Counter, Meter, Metric and Histogram