Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prometheus metrics in dedicated port #1232

Merged
merged 9 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"express-winston": "^4.2.0",
"http-errors": "^2.0.0",
"lru-cache": "^7.13.1",
"prom-client": "^14.1.1",
"rxjs": "^7.5.6",
"winston": "^3.8.1"
},
Expand Down
10 changes: 10 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Log } from './logging/Log';
import * as middleware from './middleware';
import { parseArgs } from './parseArgs';
import { SidecarConfig } from './SidecarConfig';
import Metrics_App from './util/metrics';

async function main() {
const { config } = SidecarConfig;
Expand Down Expand Up @@ -88,8 +89,17 @@ async function main() {
host: config.EXPRESS.HOST,
});

// Create our App
const metricsApp = new Metrics_App({
Imod7 marked this conversation as resolved.
Show resolved Hide resolved
port: 9100,
host: config.EXPRESS.HOST,
});

// Start the server
app.listen();

// Start the Metrics server
metricsApp.listen();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/middleware/error/httpErrorMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ErrorRequestHandler } from 'express';
import { HttpError } from 'http-errors';

import { Log } from '../../logging/Log';
import { httpErrorCounter } from '../../util/metrics';
/**
* Handle HttpError instances.
*
Expand Down Expand Up @@ -47,6 +48,8 @@ export const httpErrorMiddleware: ErrorRequestHandler = (
stack: err.stack,
};

httpErrorCounter.inc();
Imod7 marked this conversation as resolved.
Show resolved Hide resolved

Log.logger.error(info);

res.status(code).send(info);
Expand Down
59 changes: 59 additions & 0 deletions src/util/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import express from 'express';
import { Application, Request, Response } from 'express';
import client from 'prom-client';

import { Log } from '../logging/Log';

const { logger } = Log;

const register = new client.Registry();
Imod7 marked this conversation as resolved.
Show resolved Hide resolved

export const httpErrorCounter = new client.Counter({
name: 'sas_http_errors',
help: 'Number of HTTP Errors',
});

register.registerMetric(httpErrorCounter);
Imod7 marked this conversation as resolved.
Show resolved Hide resolved

client.collectDefaultMetrics({ register, prefix: 'sas_' });

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) {
this.app = express();
this.port = 9100;
this.host = host;

this.metricsEndpoint();
}

listen(): void {
this.app.listen(this.port, this.host, () => {
logger.info(
`Metrics Server started at http://${this.host}:${this.port}/`
);
});
}

/**
* Mount the metrics endpoint.
*/
private metricsEndpoint() {
// Set up the metrics endpoint
this.app.get('/metrics', async (_req: Request, res: Response) => {
Imod7 marked this conversation as resolved.
Show resolved Hide resolved
res.set('Content-Type', register.contentType);
return res.send(await register.metrics());
});
}
}
Loading