Prometheus (Stats)
- Introduction
- Installing the exporter
- Creating the exporter
- Running Prometheus
- Viewing your metrics
- Project link
Introduction
Prometheus is a monitoring system that collects metrics, by scraping exposed endpoints at regular intervals, evaluating rule expressions. It can also trigger alerts if certain conditions are met.
OpenCensus Node.js has support for this exporter available, distributed through NPM package @opencensus/exporter-prometheus
For assistance setting up Prometheus, Click here for a guided codelab.
Installing the exporter
You can install OpenCensus Prometheus Exporter by running these steps:
npm install @opencensus/core
npm install @opencensus/exporter-prometheus
Creating the exporter
To create the exporter, we’ll need to:
- Import and use the Prometheus exporter package
- Define a namespace that will uniquely identify our metrics when viewed on Prometheus
- Expose a port on which we shall run a
/metrics
endpoint - With the defined port, we’ll need a Promethus configuration file so that Prometheus can scrape from this endpoint
Now let’s use the Prometheus exporter:
const { globalStats } = require('@opencensus/core');
const { PrometheusStatsExporter } = require('@opencensus/exporter-prometheus');
// Add your port and startServer to the Prometheus options
const exporter = new PrometheusStatsExporter({
port: 9464,
startServer: true
});
// Pass the created exporter to Stats
globalStats.registerExporter(exporter);
and then for our corresponding prometheus.yaml
file:
global:
scrape_interval: 10s
external_labels:
monitor: 'demo'
scrape_configs:
- job_name: 'demo'
scrape_interval: 10s
static_configs:
- targets: ['localhost:8888']
Running Prometheus
And then run Prometheus with your configuration
prometheus --config.file=prometheus.yaml
Viewing your metrics
Please visit http://localhost:9090
Project link
You can find out more about the Prometheus project at https://prometheus.io/