Client for prometheus that provides comprehensive Prometheus metrics collection and exposition for Node.js applications
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
prom-client provides automated collection of Node.js runtime metrics that are recommended by Prometheus for monitoring application health. These metrics include CPU usage, memory consumption, event loop lag, garbage collection statistics, and other process-level information.
The collectDefaultMetrics function automatically sets up collection of standard Node.js runtime metrics. Metrics are collected on scrape (when registry.metrics() is called), not on an interval.
/**
* Configure default metrics collection
*/
const collectDefaultMetrics: {
/**
* Configure default metrics
* @param config Configuration object for default metrics collector
*/
<T extends RegistryContentType>(
config?: DefaultMetricsCollectorConfiguration<T>
): void;
/** All available default metrics */
metricsList: string[];
};
interface DefaultMetricsCollectorConfiguration<T extends RegistryContentType> {
register?: Registry<T>;
prefix?: string;
gcDurationBuckets?: number[];
eventLoopMonitoringPrecision?: number;
labels?: object;
}Usage Examples:
import { collectDefaultMetrics, register, Registry } from "prom-client";
// Collect default metrics with default configuration
collectDefaultMetrics();
// Collect default metrics with custom configuration
collectDefaultMetrics({
prefix: "myapp_",
labels: { service: "api-server", version: "1.0.0" },
eventLoopMonitoringPrecision: 10,
});
// Use a custom registry
const customRegistry = new Registry();
collectDefaultMetrics({ register: customRegistry });
// Get all available default metric names
console.log(collectDefaultMetrics.metricsList);By default, metrics are registered to the global registry. You can specify a custom registry to keep default metrics separate from application metrics.
import { collectDefaultMetrics, Registry } from "prom-client";
const metricsRegistry = new Registry();
collectDefaultMetrics({ register: metricsRegistry });
// Only default metrics will be in this registry
const defaultMetrics = await metricsRegistry.metrics();Add a prefix to all default metric names to avoid naming conflicts or to group metrics by application.
import { collectDefaultMetrics } from "prom-client";
// All default metrics will be prefixed with "myservice_"
collectDefaultMetrics({ prefix: "myservice_" });
// Results in metrics like:
// myservice_process_cpu_user_seconds_total
// myservice_nodejs_heap_size_total_bytes
// myservice_nodejs_eventloop_lag_secondsApply common labels to all default metrics, useful for multi-service environments or deployment tagging.
import { collectDefaultMetrics } from "prom-client";
collectDefaultMetrics({
labels: {
service: "user-api",
environment: "production",
region: "us-west-2",
instance: process.env.HOSTNAME,
},
});Customize the histogram buckets for garbage collection duration metrics.
import { collectDefaultMetrics, linearBuckets } from "prom-client";
// Default GC buckets: [0.001, 0.01, 0.1, 1, 2, 5]
collectDefaultMetrics({
gcDurationBuckets: linearBuckets(0.001, 0.001, 10), // More granular buckets
});Configure the sampling rate for event loop lag monitoring in milliseconds.
import { collectDefaultMetrics } from "prom-client";
collectDefaultMetrics({
eventLoopMonitoringPrecision: 5, // Check every 5ms (more precise but higher overhead)
});The metricsList property contains all available default metric names:
/**
* All available default metrics
*/
metricsList: string[];Default metrics include:
process_cpu_user_seconds_total, process_cpu_system_seconds_totalprocess_start_time_secondsprocess_open_fds, process_max_fds (Linux only)nodejs_eventloop_lag_seconds, nodejs_eventloop_lag_min_seconds, nodejs_eventloop_lag_max_seconds, nodejs_eventloop_lag_mean_seconds, nodejs_eventloop_lag_stddev_seconds, nodejs_eventloop_lag_p50_seconds, nodejs_eventloop_lag_p90_seconds, nodejs_eventloop_lag_p99_secondsnodejs_active_handles, nodejs_active_handles_totalnodejs_active_requests, nodejs_active_requests_totalnodejs_heap_size_total_bytes, nodejs_heap_size_used_bytes, nodejs_external_memory_bytesnodejs_heap_space_size_total_bytes, nodejs_heap_space_size_used_bytes, nodejs_heap_space_size_available_bytesnodejs_version_infonodejs_gc_duration_secondsprocess_resident_memory_bytes, process_virtual_memory_bytes, process_heap_bytes (Linux only)nodejs_active_resources, nodejs_active_resources_total (if available)Usage Example:
import { collectDefaultMetrics } from "prom-client";
// Enable default metrics
collectDefaultMetrics();
// List all available default metrics
console.log("Available default metrics:");
collectDefaultMetrics.metricsList.forEach(metricName => {
console.log(`- ${metricName}`);
});Some metrics are only available on specific platforms:
process_open_fds, process_max_fds) and OS memory heap metricsprocess.getActiveResourcesInfo function existsDefault metrics collection is designed to be lightweight:
registry.metrics() is calledeventLoopMonitoringPrecisionExample with performance tuning:
import { collectDefaultMetrics } from "prom-client";
// Optimize for high-throughput applications
collectDefaultMetrics({
eventLoopMonitoringPrecision: 100, // Less frequent sampling
gcDurationBuckets: [0.001, 0.01, 0.1, 1, 5], // Fewer buckets
});import express from "express";
import { collectDefaultMetrics, register } from "prom-client";
const app = express();
// Enable default metrics collection
collectDefaultMetrics();
// Expose metrics endpoint
app.get("/metrics", async (req, res) => {
res.set("Content-Type", register.contentType);
const metrics = await register.metrics();
res.end(metrics);
});
app.listen(3000);interface DefaultMetricsCollectorConfiguration<T extends RegistryContentType> {
register?: Registry<T>;
prefix?: string;
gcDurationBuckets?: number[];
eventLoopMonitoringPrecision?: number;
labels?: object;
}
type RegistryContentType = PrometheusContentType | OpenMetricsContentType;Install with Tessl CLI
npx tessl i tessl/npm-prom-client