aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan José <juan.jose.arboleda.a@gmail.com>2022-02-26 14:33:46 -0500
committerGitHub <noreply@github.com>2022-02-26 13:33:46 -0600
commit2cf941d1e56cc37e8ff33995c25d3b7b0739d0a3 (patch)
tree4c702c5d13221d4ec377b0f0a06ccde21961a91c
parent89fe5c03c0a6af84bd31cb1f329c90b0f3619780 (diff)
downloadcompiler-explorer-gh-2124.tar.gz
compiler-explorer-gh-2124.zip
Move Prometheus metrics server into a separated file (#3391)gh-2124
This will make the code a bit more readable and the Prometheus server more easy to maintain.
-rwxr-xr-xapp.js15
-rw-r--r--lib/metrics-server.ts22
2 files changed, 24 insertions, 13 deletions
diff --git a/app.js b/app.js
index b72831fdb..a8554c534 100755
--- a/app.js
+++ b/app.js
@@ -57,6 +57,7 @@ import { RouteAPI } from './lib/handlers/route-api';
import { SourceHandler } from './lib/handlers/source';
import { languages as allLanguages } from './lib/languages';
import { logger, logToLoki, logToPapertrail, suppressConsoleLog } from './lib/logger';
+import { setupMetricsServer } from './lib/metrics-server';
import { ClientOptionsHandler } from './lib/options-handler';
import * as props from './lib/properties';
import { ShortLinkResolver } from './lib/shortener/google';
@@ -547,19 +548,7 @@ async function main() {
if (opts.metricsPort) {
logger.info(`Running metrics server on port ${opts.metricsPort}`);
- PromClient.collectDefaultMetrics();
- const metricsServer = express();
-
- metricsServer.get('/metrics', async (req, res) => {
- try {
- res.set('Content-Type', PromClient.register.contentType);
- res.end(await PromClient.register.metrics());
- } catch (ex) {
- res.status(500).end(ex);
- }
- });
-
- metricsServer.listen(opts.metricsPort, defArgs.hostname);
+ setupMetricsServer(opts.metricsPort, defArgs.hostname);
}
webServer
diff --git a/lib/metrics-server.ts b/lib/metrics-server.ts
new file mode 100644
index 000000000..eb8debc5d
--- /dev/null
+++ b/lib/metrics-server.ts
@@ -0,0 +1,22 @@
+import express from 'express';
+import PromClient from 'prom-client';
+
+/**
+ * Will launch the Prometheus metrics server
+ *
+ * @param serverPort - The listening port to bind into this metrics server.
+ * @param hostname - The TCP host to attach the listener.
+ * @returns void
+ */
+export function setupMetricsServer(serverPort: number, hostname: string): void {
+ PromClient.collectDefaultMetrics();
+ const metricsServer = express();
+
+ metricsServer.get('/metrics', (req, res) => {
+ PromClient.register.metrics()
+ .then(metrics => { res.header('Content-Type', PromClient.register.contentType).send(metrics); })
+ .catch(err => res.status(500).send(err));
+ });
+
+ metricsServer.listen(serverPort, hostname);
+}