1.0.5 • Published 12 months ago

pm2-prom-client v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

PM2 Prometheus metric

Metrics collector (including custom counter/gauge) from pm2 processes using prom-client.

Features

  • Default metrics from each process
  • Custom increment counters
  • Custom set/inc/dec gauge value
  • Exporting default process metrics to Prometheus/Zabbix and further into grafana to get dashboards like pm2 keymetrics.io

Quick Start

Install

npm install pm2-prom-client

Custom metrics usage

In any pm2 process call custom metrics update methods:

import metric from "pm2-prom-client"

metric.incCounter("my_counter") // by 1
metric.incCounter("my_counter", 10) // by 10
metric.setGauge("my_gauge", 100)
metric.incGauge("my_gauge", 2)
metric.decGauge("my_gauge", 1)

Create a dedicated PM2 process that will consume and serving metrics from your applications

ecosystem.config.js

{
  name: "Metric",
  script: "build/metric.js"
}

metric.js

import metric from "pm2-prom-client"
import http, { ServerOptions, ServerResponse } from "http"

const startHttpServer = () => {
  const requestListener = async (_: RequestOptions, res: ServerResponse) => {

    const metrics = await metric.registry.metrics() // Read as plain text

    res.writeHead(200)
    res.end(metrics)
  }

  const server = http.createServer(requestListener)
  server.listen(9999, "0.0.0.0")
}

metric.startAgent() // Start metric agent
startHttpServer() // Http serve

Default metrics

Default metrics enabled by default. You can disable default metrics by

metric.startAgent({ defaultMetrics: false })

Response example:

# HELP my_counter my_counter
# TYPE my_counter counter
my_counter 123

# HELP my_gauge my_gauge
# TYPE my_gauge gauge
my_gauge 100

... default metrics
1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago