
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
prometheus-api-metrics
Advanced tools
API and process monitoring with Prometheus for Node.js micro-service
API and process monitoring with Prometheus for Node.js micro-service
Note: Prometheus (prom-client
) is a peer dependency since 1.x version
${path}.json
)const apiMetrics = require('prometheus-api-metrics');
app.use(apiMetrics())
default: /metrics
default: 10000
default: [0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5]
default: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
default: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
To get the metrics in Prometheus format use:
curl http[s]://<host>:[port]/metrics
To get the metrics in JSON format use:
curl http[s]://<host>:[port]/metrics.json
Note:
If you pass to the middleware the metricsPath
option the path will be the one that you chose.
If you are using express framework and no route was found for the request (e.g: 404 status code), the request will not be collected. that's because we'll risk memory leak since the route is not a pattern but a hardcoded string.
You can expand the API metrics with more metrics that you would like to expose. All you have to do is:
Require prometheus client
const Prometheus = require('prom-client');
Create new metric from the kind that you like
const checkoutsTotal = new Prometheus.Counter({
name: 'checkouts_total',
help: 'Total number of checkouts',
labelNames: ['payment_method']
});
Update it:
checkoutsTotal.inc({
payment_method: paymentMethod
})
The custom metrics will be exposed under the same endpoint as the API metrics.
For more info about the Node.js Prometheus client you can read here
This will work only if you use the default Prometheus registry - do not use new Prometheus.Registry()
This feature enables you to easily process the result of Request.js timings feature.
####Initialize You can choose to initialized this functionality as a Class or not
Class:
const HttpMetricsCollector = require('prometheus-api-metrics').HttpMetricsCollector;
const collector = new HttpMetricsCollector();
collector.init();
Singelton:
const HttpMetricsCollector = require('prometheus-api-metrics').HttpMetricsCollector;
HttpMetricsCollector.init();
For Example:
request({ url: 'http://www.google.com', time: true }, (err, response) => {
Collector.collect(err || response);
});
return requestPromise({ method: 'POST', url: 'http://www.mocky.io/v2/5bd9984b2f00006d0006d1fd', route: 'v2/:id', time: true, resolveWithFullResponse: true }).then((response) => {
Collector.collect(response);
}).catch((error) => {
Collector.collect(error);
});
Notes:
{ time: true }
as part of your request configuration and then pass to the collector the response or error you got.resolveWithFullResponse: true
route
and target
attribute instead of taking them from the request object. In order to do that you should set a metrics
object on your request with those attribute:request({ method: 'POST', url: 'http://www.mocky.io/v2/5bd9984b2f00006d0006d1fd', metrics: { target: 'www.google.com', route: 'v2/:id' }, time: true }, (err, response) => {...};
});
This package supports koa server that uses koa-router
and koa-bodyparser
npm test
(sum(rate(http_request_duration_seconds_bucket{<SERVICE_LABLE_FIELD>="<SERVICE_LABEL>">, route="<ROUTE_NAME>", le="0.05"}[10m])) by (<SERVICE_LABLE_FIELD>) + sum(rate(http_request_duration_seconds_bucket{<SERVICE_LABLE_FIELD>="<SERVICE_LABEL>", route="<ROUTE_NAME>", le="0.1"}[10m])) by (<SERVICE_LABLE_FIELD>)) / 2 / sum(rate(http_request_duration_seconds_count{<SERVICE_LABLE_FIELD>="<SERVICE_LABEL>", route="<ROUTE_NAME>"}[10m])) by (<SERVICE_LABLE_FIELD>)
histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket{<SERVICE_LABLE_FIELD>="<SERVICE_LABEL>", route="<ROUTE_NAME>", code="200"}[10m])) by (le))
histogram_quantile(0.50, sum(rate(http_request_duration_seconds_bucket{<SERVICE_LABLE_FIELD>="<SERVICE_LABEL>"}[10m])) by (le))
histogram_quantile(0.50, sum(rate(http_request_size_bytes_bucket{<SERVICE_LABLE_FIELD>="<SERVICE_LABEL>"}[10m])) by (le))
histogram_quantile(0.50, sum(rate(http_response_size_bytes_bucket{<SERVICE_LABLE_FIELD>="<SERVICE_LABEL>"}[10m])) by (le))
avg(nodejs_external_memory_bytes / 1024 / 1024) by (<SERVICE_LABLE_FIELD)
avg(nodejs_eventloop_lag_seconds) by (<SERVICE_LABLE_FIELD)
FAQs
API and process monitoring with Prometheus for Node.js micro-service
The npm package prometheus-api-metrics receives a total of 26,831 weekly downloads. As such, prometheus-api-metrics popularity was classified as popular.
We found that prometheus-api-metrics demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.