From 0dcd03d52c02cfbc810f7d776f71933e2210f457 Mon Sep 17 00:00:00 2001 From: Dominik K Date: Fri, 10 Nov 2023 16:36:08 +0100 Subject: [PATCH] oop --- src/util/promHandler.js | 26 -------------------- src/util/promHandler.ts | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 26 deletions(-) delete mode 100644 src/util/promHandler.js create mode 100644 src/util/promHandler.ts diff --git a/src/util/promHandler.js b/src/util/promHandler.js deleted file mode 100644 index e398f192..00000000 --- a/src/util/promHandler.js +++ /dev/null @@ -1,26 +0,0 @@ -const Prometheus = require('prom-client'); -const register = new Prometheus.Registry(); - -Prometheus.collectDefaultMetrics({ register }); - -const http = require('http'); -const server = http.createServer(async (req, res) => { - const serverGrowthGauge = new Prometheus.Gauge({ name: 'server_growth', help: 'The number of new servers added to the server in the last hour' }); - if (req.url === '/metrics') { - const metrics = await register.metrics(); - - - serverGrowthGauge.set(100); - res.setHeader('Content-Type', register.contentType); - res.end(metrics); - } else { - res.writeHead(404); - res.end(); - } -}); - -server.listen(9091); - - - -console.log('Server listening on port 9090'); \ No newline at end of file diff --git a/src/util/promHandler.ts b/src/util/promHandler.ts new file mode 100644 index 00000000..8789d6bd --- /dev/null +++ b/src/util/promHandler.ts @@ -0,0 +1,53 @@ +const express = require("express"); +const promClient = require("prom-client"); + +const { client } = require("./wouldYou"); + +class prometheusClient { + private registry; + private counter; + constructor() { + this.registry = new promClient.Registry(); + this.counter = new promClient.Counter({ + name: "example_counter", + help: "A simple example counter metric", + registers: [this.registry], + }); + + const app = express(); + + app.use(express.json()); + + // Expose Prometheus metrics endpoint + app.get("/metrics", async (req, res) => { + res.set("Content-Type", this.registry.contentType()); + res.end(await this.registry.metrics()); + }); + } + + increment(counter: string) { + switch (this.counter) { + case "wouldYou": + this.counter = wouldYou; + break; + case "neverHaveIEver": + this.counter = neverHaveIEver; + break; + case "mostLikely": + this.counter = mostLikely; + break; + case "truthOrDare": + this.counter = truthOrDare; + break; + default: + this.counter = wouldYou; + } + this.counter.inc(); + } + + getMetrics() { + return this.registry.metrics(); + } +} + +module.exports = prometheusClient;