Zhao Yi Zhao Yi - 3 years ago 316
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

server with below configuration:

# my global config
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).
monitor: 'codelab-monitor'

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

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

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

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

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

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

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

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

when I go to
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
was called 18 times. Now I go to
graph page, I am able to see the graph for
. 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