-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prometheus
metrics
endpoint in dedicated server & port (#…
…1232) * feat: add prometheus `metrics` endpoint in dedicated server & port - add separate express instance and port for prometheus metrics - expose the default prometheus metrics - add a custom metric of type `counter` that increments in every http error * fix lint errors by using an IIFE & adding void operator - remove the `return` keyword - add an IIFE inside the callback - add the `void` operator * update dep prom-client * add prometheus flags & fix for process.kill - add a prometheus flag that runs the prometheus server & enables the metrics endpoint - add a prometheus-port flag so that the user can choose a custom port to run the metrics endpoint - change the code in killAll so that there is no `Object is possibly undefined` error * code refactor, help page & README - add prometheus options and description under help flag - add `metricsApp` and increased `httpErrorCounter` only if prometheus flag is true - move logger and register inside functions - docs: add a section for prometheus in the README * remove jest unnecessary flags --------- Co-authored-by: tarikgul <[email protected]>
- Loading branch information
Showing
9 changed files
with
608 additions
and
524 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
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
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
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
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
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
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
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,60 @@ | ||
import express from 'express'; | ||
import { Application, Request, Response } from 'express'; | ||
import client from 'prom-client'; | ||
|
||
import { Log } from '../logging/Log'; | ||
import { parseArgs } from '../parseArgs'; | ||
|
||
export const httpErrorCounter = new client.Counter({ | ||
name: 'sas_http_errors', | ||
help: 'Number of HTTP Errors', | ||
}); | ||
|
||
interface IAppConfiguration { | ||
port: number; | ||
host: string; | ||
} | ||
|
||
export default class Metrics_App { | ||
private app: Application; | ||
private readonly port: number; | ||
private readonly host: string; | ||
|
||
/** | ||
* @param appConfig configuration for app. | ||
*/ | ||
constructor({ host }: IAppConfiguration) { | ||
const args = parseArgs(); | ||
|
||
this.port = Number(args.prometheus_port); | ||
this.app = express(); | ||
this.host = host; | ||
|
||
this.metricsEndpoint(); | ||
} | ||
|
||
listen(): void { | ||
const { logger } = Log; | ||
this.app.listen(this.port, this.host, () => { | ||
logger.info( | ||
`Metrics Server started at http://${this.host}:${this.port}/` | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* Mount the metrics endpoint. | ||
*/ | ||
private metricsEndpoint() { | ||
const register = new client.Registry(); | ||
register.registerMetric(httpErrorCounter); | ||
client.collectDefaultMetrics({ register, prefix: 'sas_' }); | ||
// Set up the metrics endpoint | ||
this.app.get('/metrics', (_req: Request, res: Response) => { | ||
void (async () => { | ||
res.set('Content-Type', register.contentType); | ||
res.send(await register.metrics()); | ||
})(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.