This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from matteodisabatino/master
Update dependencies and improvements
- Loading branch information
Showing
7 changed files
with
13,563 additions
and
2,174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"env": { | ||
"es2021": true, | ||
"jest/globals": true, | ||
"node": true | ||
}, | ||
"extends": [ | ||
"standard" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": 12, | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"jest" | ||
], | ||
"rules": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import * as promClient from "prom-client"; | ||
import * as express from "express"; | ||
import * as promClient from 'prom-client' | ||
import * as express from 'express' | ||
|
||
export const client: typeof promClient; | ||
export declare function instrument (server: express.Express): void; | ||
export const client: typeof promClient | ||
export declare function instrument (server: express.Express): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,82 @@ | ||
const client = require("prom-client") | ||
const client = require('prom-client') | ||
|
||
client.collectDefaultMetrics(); | ||
client.collectDefaultMetrics() | ||
|
||
const metric = { | ||
http: { | ||
requests: { | ||
clients: new client.Gauge({ | ||
name: "http_requests_processing", | ||
help: "Http requests in progress", | ||
labelNames: ["method", "path", "status"] | ||
name: 'http_requests_processing', | ||
help: 'Http requests in progress', | ||
labelNames: ['method', 'path', 'status'] | ||
}), | ||
throughput: new client.Counter({ | ||
name: "http_requests_total", | ||
help: "Http request throughput", | ||
labelNames: ["method", "path", "status"] | ||
name: 'http_requests_total', | ||
help: 'Http request throughput', | ||
labelNames: ['method', 'path', 'status'] | ||
}), | ||
duration: new client.Histogram({ | ||
name: "http_request_duration_seconds", | ||
help: "Request duration histogram in seconds", | ||
labelNames: ["method", "path", "status"] | ||
name: 'http_request_duration_seconds', | ||
help: 'Request duration histogram in seconds', | ||
labelNames: ['method', 'path', 'status'] | ||
}) | ||
} | ||
} | ||
}; | ||
|
||
function defaultOptions(options) { | ||
options = options || {}; | ||
options.url = options.url || "/metrics"; | ||
return options; | ||
} | ||
|
||
function isPathExcluded(options, path) { | ||
const isPathExcluded = (options, path) => { | ||
if (!options.excludePaths) { | ||
return false | ||
} else { | ||
return options.excludePaths.some(pathToExclude => { | ||
let regExp = new RegExp(pathToExclude) | ||
return regExp.test(path) | ||
}) | ||
} | ||
|
||
return options.excludePaths.some(pathToExclude => { | ||
const regExp = new RegExp(pathToExclude) | ||
return regExp.test(path) | ||
}) | ||
} | ||
|
||
function instrument(server, options) { | ||
const opt = defaultOptions(options); | ||
const instrument = (server, options = {}) => { | ||
const opt = Object.assign({ url: '/metrics' }, options) | ||
|
||
function middleware(req, res, next) { | ||
const middleware = (req, res, next) => { | ||
// If path is the excluded paths list, we don't add a metric for it :) | ||
if (isPathExcluded(opt, req.path)) { | ||
return next() | ||
} | ||
|
||
if (req.path !== opt.url) { | ||
const end = metric.http.requests.duration.startTimer(); | ||
metric.http.requests.clients.inc(1, Date.now()); | ||
const end = metric.http.requests.duration.startTimer() | ||
metric.http.requests.clients.inc(1, Date.now()) | ||
|
||
res.on("finish", function () { | ||
res.on('finish', () => { | ||
const labels = { | ||
method: req.method, | ||
path: req.route ? req.baseUrl + req.route.path : req.path, | ||
status: res.statusCode | ||
}; | ||
} | ||
|
||
metric.http.requests.clients.dec(1, Date.now()); | ||
metric.http.requests.throughput.inc(labels, 1, Date.now()); | ||
end(labels); | ||
}); | ||
metric.http.requests.clients.dec(1, Date.now()) | ||
metric.http.requests.throughput.inc(labels, 1, Date.now()) | ||
end(labels) | ||
}) | ||
} | ||
|
||
return next(); | ||
next() | ||
} | ||
|
||
server.use(middleware); | ||
server.use(middleware) | ||
|
||
server.get(opt.url, (req, res) => { | ||
res.header("content-type", "text/plain; charset=utf-8"); | ||
return res.send(client.register.metrics()); | ||
}); | ||
server.get(opt.url, async (req, res, next) => { | ||
try { | ||
res.header('content-type', 'text/plain; charset=utf-8') | ||
res.send(await client.register.metrics()) | ||
} catch (e) { | ||
next(e) | ||
} | ||
}) | ||
} | ||
|
||
module.exports = { | ||
client, | ||
instrument | ||
}; | ||
} |
Oops, something went wrong.