简体   繁体   中英

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?

Take a look at swagger-stats module. It exposes Prometheus metrics, including memory and CPU usage for node.js process:

  • nodejs_process_memory_rss_bytes
  • nodejs_process_memory_heap_total_bytes
  • nodejs_process_memory_heap_used_bytes
  • nodejs_process_memory_external_bytes
  • nodejs_process_cpu_usage_percentage

It also exposes API metrics, so you can monitor API usage together with CPU/Memory using Prometheus and Grafana.

More details in Documentation

You can use appmetrics-prometheus-client npm package which instruments the code in simple steps.

It exposes following metrics at /metrics endpoint :

  1. CPU:

    • cpu.process
    • cpu.system
  2. Memory:

    • memory.process.private
    • memory.process.physical
    • memory.process.virtual
    • memory.system.used
    • memory.system.total
  3. Event Loop:

    • eventloop.latency.min
    • eventloop.latency.max
    • eventloop.latency.avg
  4. Garbage Collection:

    • gc.size
    • gc.used
    • gc.duration
  5. HTTP Requests:

    • http

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM