Skip to content
Snippets Groups Projects
Commit f542775c authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

Added custom metrics

parent 3f43a86b
No related branches found
No related tags found
1 merge request!1Master to main
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
...@@ -20,6 +20,17 @@ Before your Routes: ...@@ -20,6 +20,17 @@ Before your Routes:
And to enable the *_metrics*-Endpoint: And to enable the *_metrics*-Endpoint:
`router.get('/_metrics', metrics.endpoint)` `router.get('/_metrics', metrics.endpoint)`
To Add your own custom Metrics:
`metrics.addCustomMetric({
name: 'test',
help: 'Some Test Metric',
labelNames: ['tester']
})`
then you may increase or decrease the value at any given point:
`this.metrics.incCustomMetric('test', 'tester', 'called_bar')`
`this.metrics.decCustomMetric('test', 'tester', 'called_bar')`
### Options ### Options
The Following Options may be used to configure the behaviour. The Following Options may be used to configure the behaviour.
......
This diff is collapsed.
...@@ -25,6 +25,12 @@ class App { ...@@ -25,6 +25,12 @@ class App {
disableDefaultMetrics: false disableDefaultMetrics: false
}) })
this.metrics.addCustomMetric({
name: 'test',
help: 'Some Test Metric',
labelNames: ['tester']
})
this.router.use(this.metrics.collect) this.router.use(this.metrics.collect)
this.router.get('/favicon.ico', (req, res) => res.status(204)) // No Favicon here this.router.get('/favicon.ico', (req, res) => res.status(204)) // No Favicon here
...@@ -33,6 +39,7 @@ class App { ...@@ -33,6 +39,7 @@ class App {
res.status(200).send('foo') res.status(200).send('foo')
}) })
this.router.get('/bar', (req:express.Request, res:express.Response) => { this.router.get('/bar', (req:express.Request, res:express.Response) => {
this.metrics.incCustomMetric('test', 'tester', 'called_bar')
res.status(200).send('bar') res.status(200).send('bar')
}) })
this.router.get('/404', (req:express.Request, res:express.Response) => { this.router.get('/404', (req:express.Request, res:express.Response) => {
......
This diff is collapsed.
...@@ -25,31 +25,31 @@ ...@@ -25,31 +25,31 @@
"prom-client": "^12.0.0" "prom-client": "^12.0.0"
}, },
"devDependencies": { "devDependencies": {
"@stryker-mutator/core": "^3.1.0", "@stryker-mutator/core": "^3.2.4",
"@stryker-mutator/jest-runner": "^3.1.0", "@stryker-mutator/jest-runner": "^3.2.4",
"@stryker-mutator/typescript": "^3.1.0", "@stryker-mutator/typescript": "^3.2.4",
"@types/express": "^4.17.4", "@types/express": "^4.17.6",
"@types/jest": "^25.1.4", "@types/jest": "^26.0.0",
"@types/node": "^13.9.1", "@types/node": "^14.0.13",
"@types/superagent": "^4.1.7", "@types/superagent": "^4.1.7",
"@types/supertest": "^2.0.8", "@types/supertest": "^2.0.9",
"@typescript-eslint/eslint-plugin": "^2.24.0", "@typescript-eslint/eslint-plugin": "^3.2.0",
"eslint": "^6.8.0", "eslint": "^7.2.0",
"eslint-config-standard-with-typescript": "^15.0.0", "eslint-config-standard-with-typescript": "^18.0.2",
"eslint-plugin-import": "^2.20.1", "eslint-plugin-import": "^2.21.2",
"eslint-plugin-node": "^11.0.0", "eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1", "eslint-plugin-promise": "^4.2.1",
"eslint-plugin-security": "^1.4.0", "eslint-plugin-security": "^1.4.0",
"eslint-plugin-standard": "^4.0.1", "eslint-plugin-standard": "^4.0.1",
"eslint-plugin-tsdoc": "^0.2.3", "eslint-plugin-tsdoc": "^0.2.5",
"husky": "^4.2.3", "husky": "^4.2.5",
"jest": "^25.1.0", "jest": "^26.0.1",
"jest-html-reporters": "^1.2.1", "jest-html-reporters": "^1.2.1",
"mock-express-request": "^0.2.2", "mock-express-request": "^0.2.2",
"standard": "^14.3.3", "standard": "^14.3.4",
"supertest": "^4.0.2", "supertest": "^4.0.2",
"ts-jest": "^25.2.1", "ts-jest": "^26.1.0",
"typescript": "^3.8.3" "typescript": "^3.9.5"
}, },
"husky": { "husky": {
"hooks": { "hooks": {
......
...@@ -8,6 +8,12 @@ export interface Options { ...@@ -8,6 +8,12 @@ export interface Options {
disableDefaultMetrics: boolean disableDefaultMetrics: boolean
} }
export interface CustomMetric {
name: string
help: string
labelNames: string[]
}
export class Metrics { export class Metrics {
public readonly _ignore: string[] public readonly _ignore: string[]
public readonly _disableRouteCounter: boolean public readonly _disableRouteCounter: boolean
...@@ -21,8 +27,11 @@ export class Metrics { ...@@ -21,8 +27,11 @@ export class Metrics {
public readonly _numOfRequests: any public readonly _numOfRequests: any
public readonly _numOfErrors: any public readonly _numOfErrors: any
public readonly _customMetrics: any
constructor (options: Partial<Options> = {}) { constructor (options: Partial<Options> = {}) {
this._client = require('prom-client') this._client = require('prom-client')
this._customMetrics = {}
if (typeof options.ignore !== 'undefined') { if (typeof options.ignore !== 'undefined') {
this._ignore = options.ignore this._ignore = options.ignore
...@@ -78,6 +87,25 @@ export class Metrics { ...@@ -78,6 +87,25 @@ export class Metrics {
} }
} }
public addCustomMetric = (options: CustomMetric): void => {
this._customMetrics[options.name] = new this._client.Gauge({
name: options.name,
help: options.help,
labelNames: options.labelNames
})
}
public incCustomMetric = (name: string, label: string, value: string): void => {
const inc: any = {}
inc[label] = value
this._customMetrics[name].inc(inc)
}
public decCustomMetric = (name: string, label: string, value: string): void => {
const dec: any = {}
dec[label] = value
this._customMetrics[name].dec(dec)
}
public collect = (req: express.Request, res: express.Response, next: express.NextFunction): void => { public collect = (req: express.Request, res: express.Response, next: express.NextFunction): void => {
res.locals.startEpoch = Date.now() res.locals.startEpoch = Date.now()
res.on('finish', () => { res.on('finish', () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment