prometheus-api-metrics
Advanced tools
Comparing version
{ | ||
"name": "prometheus-api-metrics", | ||
"version": "2.2.5", | ||
"version": "2.2.6", | ||
"description": "API and process monitoring with Prometheus for Node.js micro-service", | ||
@@ -12,3 +12,4 @@ "author": "Idan Tovi", | ||
"integration-tests": "nyc mocha ./test/**/**/*-test.js --require ts-node/register test/integration-tests/nest-js/*.spec.ts", | ||
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls" | ||
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls", | ||
"lint": "node_modules/eslint/bin/eslint.js src/**/*.js test/**/*.js" | ||
}, | ||
@@ -56,10 +57,10 @@ "main": "src/index.js", | ||
"doctoc": "^1.4.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-chai-friendly": "^0.4.1", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-mocha": "^5.3.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.8.0", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-chai-friendly": "^0.5.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-mocha": "^6.3.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"express": "^4.16.4", | ||
@@ -66,0 +67,0 @@ "koa": "^2.7.0", |
@@ -128,3 +128,3 @@ # Prometheus API Monitoring | ||
### Usage | ||
####Initialize | ||
#### Initialize | ||
You can choose to initialized this functionality as a Class or not | ||
@@ -182,3 +182,8 @@ | ||
```js | ||
const { koaMiddleware } = require('prometheus-api-metrics') | ||
app.use(koaMiddleware()) | ||
``` | ||
## Test | ||
@@ -185,0 +190,0 @@ |
@@ -10,2 +10,3 @@ const Prometheus = require('prom-client'); | ||
} | ||
_collectDefaultServerMetrics(timeout) { | ||
@@ -21,2 +22,3 @@ const NUMBER_OF_CONNECTIONS_METRICS_NAME = 'expressjs_number_of_open_connections'; | ||
} | ||
_getConnections() { | ||
@@ -33,2 +35,3 @@ if (this.setupOptions && this.setupOptions.server) { | ||
} | ||
_handleResponse (req, res) { | ||
@@ -46,2 +49,3 @@ const responseLength = parseInt(res.get('Content-Length')) || 0; | ||
} | ||
_getRoute(req) { | ||
@@ -76,3 +80,3 @@ let route = req.baseUrl; | ||
} | ||
// this condition will evaluate to true only in | ||
@@ -88,2 +92,3 @@ // express framework and no route was found for the request. if we log this metrics | ||
} | ||
middleware(req, res, next) { | ||
@@ -94,3 +99,6 @@ if (!this.setupOptions.server && req.socket) { | ||
} | ||
if (req.url === this.setupOptions.metricsRoute) { | ||
const routeUrl = req.originalUrl || req.url; | ||
if (routeUrl === this.setupOptions.metricsRoute) { | ||
debug('Request to /metrics endpoint'); | ||
@@ -100,3 +108,3 @@ res.set('Content-Type', Prometheus.register.contentType); | ||
} | ||
if (req.url === `${this.setupOptions.metricsRoute}.json`) { | ||
if (routeUrl === `${this.setupOptions.metricsRoute}.json`) { | ||
debug('Request to /metrics endpoint'); | ||
@@ -113,3 +121,3 @@ return res.json(Prometheus.register.getMetricsAsJSON()); | ||
debug(`Set start time and content length for request. url: ${req.url}, method: ${req.method}`); | ||
debug(`Set start time and content length for request. url: ${routeUrl}, method: ${req.method}`); | ||
@@ -116,0 +124,0 @@ res.once('finish', () => { |
@@ -12,2 +12,3 @@ const Prometheus = require('prom-client'); | ||
} | ||
_collectDefaultServerMetrics(timeout) { | ||
@@ -23,2 +24,3 @@ const NUMBER_OF_CONNECTIONS_METRICS_NAME = 'koajs_number_of_open_connections'; | ||
} | ||
_getConnections() { | ||
@@ -35,2 +37,3 @@ if (this.setupOptions.server) { | ||
} | ||
_handleResponse (ctx) { | ||
@@ -47,3 +50,3 @@ const responseLength = parseInt(ctx.response.get('Content-Length')) || 0; | ||
}, ctx.req.metrics.contentLength); | ||
ctx.req.metrics.timer({route: route, code: ctx.res.statusCode}); | ||
ctx.req.metrics.timer({ route: route, code: ctx.res.statusCode }); | ||
this.setupOptions.responseSizeHistogram.observe({ | ||
@@ -57,2 +60,3 @@ method: ctx.req.method, | ||
} | ||
_getRoute(ctx) { | ||
@@ -73,5 +77,6 @@ let route; | ||
} | ||
_handleSubRoutes(matchedRoute, originalUrl, method, router) { | ||
let route; | ||
let routeStart = matchedRoute.substring(0, matchedRoute.length - WILDCARD_ROUTE_ENDING.length); | ||
const routeStart = matchedRoute.substring(0, matchedRoute.length - WILDCARD_ROUTE_ENDING.length); | ||
let url = this._removeQueryFromUrl(originalUrl).substring(routeStart.length); | ||
@@ -91,4 +96,5 @@ let matchedRoutes = router.match(url, method); | ||
} | ||
_findFirstProperRoute(routes) { | ||
let properRoute = routes.find(route => { | ||
const properRoute = routes.find(route => { | ||
if (!route.path.endsWith('(.*)')) { | ||
@@ -103,5 +109,7 @@ return route; | ||
} | ||
_removeQueryFromUrl(url) { | ||
return url.split('?')[0]; | ||
} | ||
middleware(ctx, next) { | ||
@@ -108,0 +116,0 @@ if (!this.setupOptions.server && ctx.req.socket) { |
@@ -70,5 +70,6 @@ 'use strict'; | ||
switch (framework) { | ||
case 'koa': | ||
case 'koa': { | ||
const middleware = new KoaMiddleware(setupOptions); | ||
return middleware.middleware.bind(middleware); | ||
} | ||
default: { | ||
@@ -75,0 +76,0 @@ const middleware = new ExpressMiddleware(setupOptions); |
@@ -29,6 +29,6 @@ const Prometheus = require('prom-client'); | ||
if (res instanceof Error && !res.response && southboundClientErrors) { | ||
let error = res.error || res; | ||
const error = res.error || res; | ||
southboundClientErrors.inc({ target: error.hostname, error: error.code }); | ||
} else { | ||
let response = res.response || res; | ||
const response = res.response || res; | ||
if (response.timings) { | ||
@@ -35,0 +35,0 @@ response.request.metrics = response.request.metrics || {}; |
'use strics'; | ||
function getMetricNames(metricNames, useUniqueHistogramName, metricsPrefix, projectName) { | ||
let prefix = useUniqueHistogramName === true ? projectName : metricsPrefix; | ||
const prefix = useUniqueHistogramName === true ? projectName : metricsPrefix; | ||
@@ -6,0 +6,0 @@ if (prefix) { |
44551
0.57%418
0.48%243
2.1%