mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 16:13:06 +00:00
feat: Add prometheus middleware to express
This enables collecting metrics from node.js and express.
This commit is contained in:
@@ -26,6 +26,7 @@ import {Express} from 'express';
|
||||
import morgan from 'morgan';
|
||||
import {join} from 'path';
|
||||
import {configFile, DEFAULT_TIMEOUT, isTestEnvironment, mailer, plugins, validator} from './common';
|
||||
import {getPrometheusMiddleware} from './middleware/prometheus';
|
||||
import {MailQueue} from './notification/mail-queue';
|
||||
import {bulkAddRouter} from './routes/bulk-add-route';
|
||||
import {bulkDoneRouter} from './routes/bulk-done-route';
|
||||
@@ -63,6 +64,10 @@ export async function configureApp(app: Express, databases: {[name: string]: Dat
|
||||
}, stream: process.stdout,
|
||||
}));
|
||||
|
||||
if (process.env.PROMETHEUS_MIDDLEWARE === 'true') {
|
||||
app.use(getPrometheusMiddleware());
|
||||
}
|
||||
|
||||
const corsOptions = {
|
||||
allowedHeaders: [
|
||||
'DNT',
|
||||
|
||||
43
src/middleware/prometheus.ts
Normal file
43
src/middleware/prometheus.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2021 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import express from 'express';
|
||||
import expressPrometheusMiddleware from 'express-prometheus-middleware';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
type UserOptions = Parameters<typeof expressPrometheusMiddleware>[0];
|
||||
|
||||
/**
|
||||
* Create and configure a new Express Prometheus Middleware instance
|
||||
*
|
||||
* This function tries to configure the new instance with JSON read from
|
||||
* `./conf/prometheus.json`. When this fails an instance configured with
|
||||
* default options is returned.
|
||||
*
|
||||
* @returns express.Express
|
||||
*/
|
||||
export function getPrometheusMiddleware(): express.Express {
|
||||
const configFileName = path.join('./config', 'prometheus.json');
|
||||
let options: UserOptions = {};
|
||||
|
||||
try {
|
||||
options = JSON.parse(fs.readFileSync(configFileName, 'utf-8'));
|
||||
} catch(err) {
|
||||
Logger.warn('Could not get options for Prometheus Middleware.', err);
|
||||
}
|
||||
|
||||
return expressPrometheusMiddleware(options);
|
||||
}
|
||||
Reference in New Issue
Block a user