Skip to content

Commit

Permalink
feat: add prometheus instrumentation (#1361)
Browse files Browse the repository at this point in the history
* feat: add prometheus instrumentation

* fix: instrumentation prometheus
  • Loading branch information
renat473 committed Jul 9, 2023
1 parent a71c39a commit 2f7495a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -98,6 +98,7 @@
"shx": "^0.3.4",
"swagger-ui-dist": "^5.1.0",
"ts-loader": "^9.4.4",
"prom-client": "^14.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"webpack-cli": "^5.1.4"
Expand Down
38 changes: 38 additions & 0 deletions src/middleware/instrumentation.ts
@@ -0,0 +1,38 @@
/*
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Request, Response } from 'express';
import Prometheus from 'prom-client';

const register = new Prometheus.Registry();

export async function metrics(req: Request, res: Response) {
/**
#swagger.tags = ["Misc"]
#swagger.autoBody=false
#swagger.description = 'This endpoint can be used to check the status of API metrics. It returns a response with the collected metrics.'
}
*/
const register = new Prometheus.Registry();
register.setDefaultLabels({
app: 'wppconnect-server',
});
Prometheus.collectDefaultMetrics({ register });

res.setHeader('Content-Type', register.contentType);
register.metrics().then((data) => res.status(200).send(data));
}
export const prometheusRegister = register;
5 changes: 5 additions & 0 deletions src/routes/index.ts
Expand Up @@ -31,6 +31,7 @@ import * as SessionController from '../controller/sessionController';
import * as StatusController from '../controller/statusController';
import verifyToken from '../middleware/auth';
import * as HealthCheck from '../middleware/healthCheck';
import * as prometheusRegister from '../middleware/instrumentation';
import statusConnection from '../middleware/statusConnection';
import swaggerDocument from '../swagger.json';

Expand Down Expand Up @@ -893,4 +894,8 @@ routes.get('/api-docs', swaggerUi.setup(swaggerDocument));
routes.get('/healthz', HealthCheck.healthz);
routes.get('/unhealthy', HealthCheck.unhealthy);

//Metrics Prometheus

routes.get('/metrics', prometheusRegister.metrics);

export default routes;
8 changes: 8 additions & 0 deletions src/swagger.json
Expand Up @@ -7339,6 +7339,14 @@
"responses": {}
}
},
"/metrics": {
"get": {
"tags": ["Misc"],
"description": "This endpoint can be used to check the status of API metrics. It returns a response with the collected metrics.",
"parameters": [],
"responses": {}
}
},
"/unhealthy": {
"get": {
"tags": ["Misc"],
Expand Down

0 comments on commit 2f7495a

Please sign in to comment.