express-prometheus-middleware
Advanced tools
Comparing version 0.9.4 to 0.9.5
@@ -5,2 +5,9 @@ # Changelog | ||
### [0.9.5](https://github.com/joao-fontenele/express-prometheus-middleware/compare/v0.9.4...v0.9.5) (2020-05-15) | ||
### Features | ||
* add posibility to add custom labels ([7e48758](https://github.com/joao-fontenele/express-prometheus-middleware/commit/7e487584d8ebe073de4f87bb478eba1e8cdd2205)) | ||
### [0.9.4](https://github.com/joao-fontenele/express-prometheus-middleware/compare/v0.9.3...v0.9.4) (2020-05-13) | ||
@@ -7,0 +14,0 @@ |
{ | ||
"name": "express-prometheus-middleware", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"description": "RED/USE metrics for express applications", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -34,3 +34,4 @@ # Express Prometheus Middleware | ||
| prefix | Optional prefix for the metrics name | no prefix added | | ||
| customLabels | Optional Array containing extra labels, used together with `transformLabels` | no extra labels: `[]` | | ||
| transformLabels | Optional `function(labels, req, res)` adds to the labels object dynamic values for each label in `customLabels` | `null` | | ||
### Example | ||
@@ -66,2 +67,10 @@ | ||
// prefix: 'app_prefix_', | ||
/** | ||
* Can add custom labels with customLabels and transformLabels options | ||
*/ | ||
// customLabels: ['contentType'], | ||
// transformLabels(labels, req) { | ||
// // eslint-disable-next-line no-param-reassign | ||
// labels.contentType = req.headers['content-type']; | ||
// }, | ||
})); | ||
@@ -68,0 +77,0 @@ |
@@ -25,2 +25,4 @@ const express = require('express'); | ||
extraMasks: [], | ||
customLabels: [], | ||
transformLabels: null, | ||
}; | ||
@@ -30,3 +32,5 @@ | ||
const options = { ...defaultOptions, ...userOptions }; | ||
const originalLabels = ['route', 'method', 'status']; | ||
options.customLabels = new Set([...originalLabels, ...options.customLabels]); | ||
options.customLabels = [...options.customLabels]; | ||
const { metricsPath, metricsApp } = options; | ||
@@ -38,4 +42,11 @@ | ||
const requestDuration = requestDurationGenerator(options.requestDurationBuckets, options.prefix); | ||
const requestCount = requestCountGenerator(options.prefix); | ||
const requestDuration = requestDurationGenerator( | ||
options.customLabels, | ||
options.requestDurationBuckets, | ||
options.prefix, | ||
); | ||
const requestCount = requestCountGenerator( | ||
options.customLabels, | ||
options.prefix, | ||
); | ||
@@ -55,7 +66,11 @@ /** | ||
const status = normalizeStatusCode(res.statusCode); | ||
const labels = { route, method, status }; | ||
requestCount.inc({ route, method, status }); | ||
if (typeof options.transformLabels === 'function') { | ||
options.transformLabels(labels, req, res); | ||
} | ||
requestCount.inc(labels); | ||
// observe normalizing to seconds | ||
requestDuration.labels(route, method, status).observe(time / 1000); | ||
requestDuration.observe(labels, time / 1000); | ||
} | ||
@@ -62,0 +77,0 @@ }); |
@@ -7,7 +7,7 @@ const Prometheus = require('prom-client'); | ||
*/ | ||
function requestCountGenerator(prefix = '') { | ||
function requestCountGenerator(labelNames, prefix = '') { | ||
return new Prometheus.Counter({ | ||
name: `${prefix}http_requests_total`, | ||
help: 'Counter for total requests received', | ||
labelNames: ['route', 'method', 'status'], | ||
labelNames, | ||
}); | ||
@@ -21,7 +21,7 @@ } | ||
*/ | ||
function requestDurationGenerator(buckets, prefix = '') { | ||
function requestDurationGenerator(labelNames, buckets, prefix = '') { | ||
return new Prometheus.Histogram({ | ||
name: `${prefix}http_request_duration_seconds`, | ||
help: 'Duration of HTTP requests in seconds', | ||
labelNames: ['route', 'method', 'status'], | ||
labelNames, | ||
buckets, | ||
@@ -28,0 +28,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21786
215
156