PMX allows you to create advanced interactions with PM2 and Keymetrics.io.
Table of Contents
- Installation
- Expose Custom Metrics
- Expose Triggerable Runtime Functions
- Report Exceptions and Alerts
- Report Custom Events
- Monitor network traffic
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; // Here the value function will be called each second to get the value// returned by Object.keys(users).lengthvar metric = probe; // Here we are going to call valvar.set() to set the new valuevar metric_2 = probe; metric_2;
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; // The counter will start at 0var counter = probe; http;
Options
- name: Probe name
Meter: Average calculated values
Things that are measured as events / interval.
var probe = pmx; var meter = probe; http;
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; var histogram = probe; var latency = 0; ;
Options
- name: Probe name
- agg_type : (optional)(default: none) Can be
sum
,max
,min
,avg
(default) ornone
. It will impact the way the probe data are aggregated within the Keymetrics backend. Usenone
if this is irrelevant (eg: constant or string value). - alert : (optional)(default: null) For
Meter
andCounter
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 = ; pmxaction'db:clean' { clean;};
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;
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;
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 = ; pmx; pmx; pmx;
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 = ; // All my routesapp;app;// All my routes // Here I attach the middleware to get more verbosity on exception thrownapp;
Emit Events
Emit events and get historical and statistics. This is available in the Events page of Keymetrics.
var pmx = ; pmx;
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;
Advanced PMX configuration
var pmx = ;
License
MIT