Zhao Yi Zhao Yi - 8 months ago 116
Node.js Question

How to use Prometheus to monitor nodejs cpu and memory usage on Mac?

I have a nodejs application on Mac OS and want to monitor its cpu and memory usage. I have setup

Prometheus
server with below configuration:

# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first.rules"
# - "second.rules"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'controller'
scrape_interval: 1s
static_configs:
- targets: ['172.16.123.1:3030']
labels:
group: 'demo'

- job_name: "node"
scrape_interval: "15s"
static_configs:
- targets: ['localhost:9100']


In my nodejs application, I have imported
prom-client
dependency. And created a
/metrics
path which returns the metrics data:

const express = require('express');
const app = express();

app.get('/metrics', (req, res) => {
const m = prom.register.metrics();
console.log(m);
res.end(m);
});


In my service class, I use
prom.Counter
to record the number of request

const counter = new prom.Counter('create_connection', 'The number of requests')
counter.inc()


when I go to
localhost:3030/metrics
link I can read below information:

# HELP nodejs_gc_runs_total Count of total garbage collections.
# TYPE nodejs_gc_runs_total counter

# HELP nodejs_gc_pause_seconds_total Time spent in GC Pause in seconds.
# TYPE nodejs_gc_pause_seconds_total counter

# HELP nodejs_gc_reclaimed_bytes_total Total number of bytes reclaimed by GC.
# TYPE nodejs_gc_reclaimed_bytes_total counter

# HELP create_connection The number of requests
# TYPE create_connection counter
create_connection 18


I can see that
create_connection
was called 18 times. Now I go to
Prometheus
graph page, I am able to see the graph for
create_connection
. But my question is how I can see how much CPU and Memory my nodejs application consumed during a time period. Is there anywhere I need to configure in my app?

Answer Source

You would have to instrument something your self. So, you can take these measurements on your own at a set interval and use something like https://nodejs.org/api/process.html#process_process_memoryusage to get memory usage, update the metrics and they will get collected.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download