adonis5-prometheus
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -8,3 +8,4 @@ /// <reference types="@adonisjs/http-server/build/adonis-typings" /> | ||
constructor(metrics: Metrics, config: any); | ||
handle({ request, response }: HttpContextContract, next: () => Promise<void>): Promise<void>; | ||
handle({ request, response, route }: HttpContextContract, next: () => Promise<void>): Promise<void>; | ||
private afterRequest; | ||
} |
@@ -9,19 +9,42 @@ "use strict"; | ||
} | ||
async handle({ request, response }, next) { | ||
const { enabled: enableHttpMetric } = this.config.httpMetric; | ||
const enableThroughputMetric = this.config.throughputMetric.enabled; | ||
async handle({ request, response, route }, next) { | ||
const httpMetricOptions = this.config.httpMetric; | ||
/** | ||
* Start HTTP request timer. | ||
* Start HTTP request timer ( if route not excluded ) with the | ||
* given options for url parsing. | ||
* The timer will be stopped when the request is finished. | ||
*/ | ||
let stopHttpRequestTimer; | ||
if (enableHttpMetric) { | ||
stopHttpRequestTimer = this.metrics.httpMetric.startTimer({ | ||
method: request.method(), | ||
url: request.url(), | ||
}); | ||
if (httpMetricOptions.enabled) { | ||
const includeRouteParams = httpMetricOptions.includeRouteParams; | ||
const includeQueryParams = httpMetricOptions.includeQueryParams; | ||
const excludedRoutes = httpMetricOptions.excludedRoutes || []; | ||
if (!excludedRoutes.includes(route?.pattern)) { | ||
let url = includeRouteParams ? request.url() : route?.pattern; | ||
if (includeQueryParams && request.parsedUrl.query) { | ||
url += `?${request.parsedUrl.query}`; | ||
} | ||
stopHttpRequestTimer = this.metrics.httpMetric.startTimer({ | ||
method: request.method(), | ||
url, | ||
}); | ||
} | ||
} | ||
/** | ||
* Continue execution. | ||
* Execute request and track metrics for the request. | ||
* If the request fails with any error, we have to catch | ||
* this errror, track metricks, then rethrow the error. | ||
*/ | ||
await next(); | ||
try { | ||
await next(); | ||
this.afterRequest(response.response.statusCode, stopHttpRequestTimer); | ||
} | ||
catch (err) { | ||
this.afterRequest(err.status || 500, stopHttpRequestTimer); | ||
throw err; | ||
} | ||
} | ||
async afterRequest(statusCode, stopHttpRequestTimer) { | ||
const enableThroughputMetric = this.config.throughputMetric.enabled; | ||
const httpMetricOptions = this.config.httpMetric; | ||
/** | ||
@@ -35,6 +58,8 @@ * Track request throughput.. | ||
*/ | ||
if (enableHttpMetric) { | ||
stopHttpRequestTimer({ | ||
statusCode: response.response.statusCode, | ||
}); | ||
if (httpMetricOptions.enabled && stopHttpRequestTimer) { | ||
let statusCodeStr = statusCode.toString(); | ||
if (httpMetricOptions.shouldGroupStatusCode) { | ||
statusCodeStr = statusCodeStr[0] + 'xx'; | ||
} | ||
stopHttpRequestTimer({ statusCode: statusCodeStr }); | ||
} | ||
@@ -41,0 +66,0 @@ } |
@@ -1,2 +0,2 @@ | ||
export default { | ||
const prometheusConfig { | ||
/* | ||
@@ -38,3 +38,5 @@ |-------------------------------------------------------------------------- | ||
name: 'adonis_http_request_durations', | ||
includeQueryParams: true, | ||
includeQueryParams: false, | ||
includeRouteParams: false, | ||
shouldGroupStatusCode: true, | ||
help: 'Total time each HTTP request takes.', | ||
@@ -44,2 +46,3 @@ labelNames: ['method', 'url', 'statusCode'], | ||
prefix: '', | ||
excludedRoutes: ["/metrics", "/health"], | ||
}, | ||
@@ -75,1 +78,3 @@ | ||
} | ||
export default prometheusConfig |
{ | ||
"name": "adonis5-prometheus", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Prometheus wrapper for Adonis 5", | ||
@@ -5,0 +5,0 @@ "main": "build/providers/PrometheusProvider.js", |
20969
228