Skip to content

Commit

Permalink
Normalize middleware routes
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkorytnicki committed May 30, 2023
1 parent 2327799 commit 609f91d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
const shimmer = require('shimmer');
const { client } = require('./prom.js');
const shimmer = require("shimmer");
const { client } = require("./prom.js");

const prefix = process.env.NEXTJS_PROMETHEUS_PREFIX + "_" ?? "";

const httpRequestDurationSeconds = new client.Histogram({
name: `${prefix}middleware_http_request_duration_seconds`,
help: 'Duration of HTTP requests to middleware in seconds',
labelNames: ['mw'],
help: "Duration of HTTP requests to middleware in seconds",
labelNames: ["mw"],
buckets: [0.01, 0.03, 0.05, 0.1, 0.9, 1],
});

const httpRequestsTotal = new client.Counter({
name: `${prefix}middleware_http_requests_total`,
help: 'Total number of HTTP requests to middleware',
labelNames: ['mw'],
help: "Total number of HTTP requests to middleware",
labelNames: ["mw"],
});

const httpErrorsTotal = new client.Counter({
name: `${prefix}middleware_http_errors_total`,
help: 'Total number of HTTP errors in middleware',
labelNames: ['mw'],
help: "Total number of HTTP errors in middleware",
labelNames: ["mw"],
});

module.exports = function initialize(nextServer) {
const Server = nextServer.default;
shimmer.wrap(Server.prototype, 'runMiddleware', function middlewareRecorder(originalMiddleware) {
shimmer.wrap(Server.prototype, "runMiddleware", function middlewareRecorder(originalMiddleware) {
return async function wrappedMiddleware(...args) {
const startTime = performance.now();
const middlewarePath = args[0].parsed.path
const middlewarePath = args[0].parsed.path.split("?")[0];
let hasError = false;
let result;

Expand All @@ -43,12 +43,11 @@ module.exports = function initialize(nextServer) {

if (hasError) {
httpErrorsTotal.labels(middlewarePath).inc();
return
return;
}
}


return result;
};
});
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-prometheus",
"version": "0.0.3",
"version": "0.0.4",
"description": "Observability of a nextjs application w/ Prometheus",
"main": "index.js",
"publishConfig": {
Expand Down

0 comments on commit 609f91d

Please sign in to comment.