Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
lint: fixed lint
Browse files Browse the repository at this point in the history
  • Loading branch information
matteodisabatino committed Jan 16, 2021
1 parent 828fea7 commit 674aec6
Show file tree
Hide file tree
Showing 7 changed files with 2,171 additions and 290 deletions.
7 changes: 0 additions & 7 deletions .eslintrc

This file was deleted.

20 changes: 20 additions & 0 deletions .eslintrc.json
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": {}
}
8 changes: 4 additions & 4 deletions lib/index.d.ts
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;
75 changes: 38 additions & 37 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,88 @@
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;
const 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)
const regExp = new RegExp(pathToExclude)
return regExp.test(path)
})
}
}

function instrument(server, options) {
const opt = defaultOptions(options);
const instrument = (server, options) => {
const opt = defaultOptions(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();
return next()
}

server.use(middleware);
server.use(middleware)

server.get(opt.url, async (req, res,next) => {
server.get(opt.url, async (req, res, next) => {
try {
res.header("content-type", "text/plain; charset=utf-8");
res.send(await client.register.metrics());
res.header('content-type', 'text/plain; charset=utf-8')
res.send(await client.register.metrics())
} catch (e) {
next(e);
next(e)
}
});
})
}

module.exports = {
client,
instrument
};
}

0 comments on commit 674aec6

Please sign in to comment.