Instrument nodejs (nextjs) application to push MEM and CPU usage? #6442
-
|
Based on: https://opentelemetry.io/docs/languages/js/exporters/#usage-with-nodejs I am trying to instrument a nodejs/nextjs application to ship the application CPU and MEM usage. I have: instrumentation.ts and: instrumentation-node.ts I am running the otel-collector on localhost in a docker container and I am scraping the metrics endpoint using prometheus with: But when I go to prometheus at http://0.0.0.0:9090/query I just see a lof of
not sure which of these I should pick in e.g. grafana that indicates the correct memory usage of my app. Also I don't see any metrics related to CPU usage. I did also look at: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/host-metrics#readme but as I understand thats metrics for the host machine and not the MEM/CPU usage for the nodejs app. Do I need some additional configuration in my app or the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Great question — there are two separate things going on here that are worth clarifying. Why you only see
|
| Metric | Description |
|---|---|
process.cpu.time |
Total CPU seconds (user + system) |
process.cpu.utilization |
CPU usage (0–1 ratio) |
process.memory.usage |
Physical memory (RSS) in bytes |
system.cpu.utilization |
Host-level CPU utilization |
system.memory.usage |
Host-level memory usage |
So it gives you exactly what you need.
How to add it to your setup
- Install the package:
npm install --save @opentelemetry/host-metrics- Update your
instrumentation-node.tsto startHostMetricsafter initializing the SDK:
import * as opentelemetry from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { HostMetrics } from '@opentelemetry/host-metrics';
const sdk = new opentelemetry.NodeSDK({
traceExporter: new OTLPTraceExporter({ headers: {} }),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({ headers: {} }),
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
// Start host metrics AFTER sdk.start()
const hostMetrics = new HostMetrics();
hostMetrics.start();Important:
HostMetricsmust be started aftersdk.start()so it can pick up the registered MeterProvider.
What to use in Grafana for memory
Once host-metrics is running, in Prometheus/Grafana use:
process_memory_usage— Physical RSS memory of the Node.js process (this is what you want for app memory)process_cpu_utilization— CPU utilization of the process (0–1, multiply by 100 for percentage)
The v8js_memory_heap_used metric you already see is useful too — it tells you how much of the V8 heap is actively used (helps identify JS memory leaks), but process_memory_usage gives the true process footprint including native allocations.
Hope this helps!
Beta Was this translation helpful? Give feedback.

Great question — there are two separate things going on here that are worth clarifying.
Why you only see
v8js_memory*metricsThe
v8js_*metrics come from@opentelemetry/instrumentation-runtime-node, which is included insidegetNodeAutoInstrumentations(). These metrics reflect V8 heap memory (the JavaScript engine's managed memory), not the full process RSS memory or CPU usage. That's expected behavior from auto-instrumentation.Getting process CPU and Memory (RSS) metrics
You were on the right track with
@opentelemetry/host-metrics— but there's a misconception: this package reports both host/system-level metrics AND process-level metrics for the Node.js process itself. Specifically, it …