• Stars
    star
    265
  • Rank 154,577 (Top 4 %)
  • Language
    JavaScript
  • Created almost 10 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

(DEPRECATED) use @pm2/io instead (drop-in replacement) https://github.com/keymetrics/pm2-io-apm

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 : '[email protected]'
});

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

More Repositories

1

pm2-logrotate

Automatically rotate all applications logs managed by PM2
JavaScript
1,243
star
2

docker-pm2

🐳 Official Docker Image for PM2 runtime
Dockerfile
476
star
3

pm2-server-monit

Monitor server CPU / Memory / Process / Zombie Process / Disk size / Security Packages / Network Input / Network Output
JavaScript
261
star
4

pm2-io-apm

PM2.io APM for Node.JS
TypeScript
147
star
5

pm2-auto-pull

This app will automatically pull all applications managed by PM2 to the latest version (compatible git)
JavaScript
136
star
6

vizion

Git/Subversion/Mercurial repository metadata parser
JavaScript
39
star
7

cli-tableau

Pretty unicode tables for the CLI with Node.JS
JavaScript
31
star
8

doc-pm2

DEPRECATED
CSS
28
star
9

pm2-io-js-api

PM2.io API Client for Javascript
JavaScript
28
star
10

pm2-plus-tech-questions

http://docs.keymetrics.io/docs/pages/faq-troubleshooting/
27
star
11

pm2-redis

Module to monitor redis
JavaScript
23
star
12

event-loop-inspector

(DEPRECATED) Dump event loop's current stats.
JavaScript
21
star
13

pm2-ebs-demo

Demo for PM2/Keymetrics integration for AWS with simple express server
JavaScript
17
star
14

app-playground

Code level metrics with Keymetrics
JavaScript
17
star
15

pm2-io-apm-go

Golang APM for PM2 Enterprise
Go
16
star
16

on-premise

Documentation for the on-premise version of PM2 Enterprise
Mustache
13
star
17

pm2-logs

Pm2 cli logs interface
JavaScript
12
star
18

pm2-elasticsearch

PM2.io Module to monitor Elasticsearch nodes
JavaScript
12
star
19

node-runtime-stats

A module that forwards stats from NodeJS runtime
C++
9
star
20

pm2-io-agent

PM2.io NodeJS Agent embed inside PM2
JavaScript
8
star
21

pm2-io-agent-node

PM2.io Agent Standalone for NodeJS
JavaScript
8
star
22

keymetrics.github.io

Keymetrics documentation website
CSS
7
star
23

interdb

👷 Local and shared database
JavaScript
6
star
24

synergy-office-control-iot

How we control our whole Paris Office with our solutions
JavaScript
5
star
25

pm2-cluster-benchmark

Benchmarking PM2 cluster vs Raw Node vs Nginx vs Haproxy
Nginx
5
star
26

keymetrics-widget

JavaScript
5
star
27

nginx-interface

Module to interface NGINX with PM2
C
5
star
28

fullstack-workshop

FullStack London 2015 - PM2 & Keymetrics workshop
JavaScript
4
star
29

install

Script to install NVM + Node + pm2 (script install.sh on app.keymetrics.io)
Shell
3
star
30

pm2-workshop

Pm2 Workshop repository
JavaScript
3
star
31

docker-interface

Node.js interface wrapper around key commands of docker and docker-compose
2
star
32

pm2-masterclass

JavaScript
2
star
33

deep-metrics

Retrieve metrics about popular modules automatically
JavaScript
2
star
34

pm2-io

SCSS
2
star
35

k8s-ansible

Ansible tasks to provision a K8S cluster
2
star
36

pm2-generate-metrics

Generate multiple graphs
JavaScript
2
star
37

branding

Keymetrics/PM2 Branding
2
star
38

pm2-io-apm-python

Python Integration for PM2.io
Python
2
star
39

trassingue

(DEPRECATED)
JavaScript
2
star
40

sample-apps

Tracing
JavaScript
1
star
41

origa

JavaScript
1
star
42

envision-sdk

🏗️ SDK to build module for Envision
JavaScript
1
star
43

envision-video

📹 Upload video for infinite replay
JavaScript
1
star
44

km_universe

JavaScript
1
star
45

pm2-tar-auto-update

JavaScript
1
star
46

tracing

1
star
47

app-showcase

JavaScript
1
star
48

simplicity

R&D blinkstick
JavaScript
1
star
49

interdb-hub

JavaScript
1
star
50

pm2-digital-signage

JavaScript
1
star
51

cpu-search

Retrieve CPU model from CPU benchmark CSV
TypeScript
1
star
52

extrareqp2

JavaScript
1
star
53

app-playground-tracing

An example app to send traces to pm2 enterprise
JavaScript
1
star
54

wakanda-hydroplant

JavaScript
1
star
55

xeon

HTML
1
star