Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-prometheus-middleware

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-prometheus-middleware - npm Package Compare versions

Comparing version 0.9.4 to 0.9.5

7

CHANGELOG.md

@@ -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 @@

2

package.json
{
"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 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc