diff --git a/server/src/controller/metricsController.ts b/server/src/controller/metricsController.ts index eb39302..71b06a9 100644 --- a/server/src/controller/metricsController.ts +++ b/server/src/controller/metricsController.ts @@ -1,8 +1,14 @@ import express, { Request, Response } from "express"; -import { Counter, collectDefaultMetrics, register } from "prom-client"; +import { container } from "tsyringe"; +import { Counter, Gauge, collectDefaultMetrics, register } from "prom-client"; +import { MetricsService } from "../services/metricsService"; +import { LocationService } from "../services/locationService"; +import { WifiLocationService } from "../services/wifiLocationService"; const router = express.Router(); - +const metricsService = container.resolve(MetricsService); +const locationService = container.resolve(LocationService); +const wifiLocationService = container.resolve(WifiLocationService); // Collect default system metrics (e.g., CPU, memory usage) const prefix = 'locationhub_'; @@ -10,9 +16,27 @@ collectDefaultMetrics({ prefix }); // Define a custom Counter metric const requestCounter = new Counter({ - name: "http_requests_total", + name: `${prefix}http_requests_total`, help: "Total number of HTTP requests", - labelNames: ["method", "route", "status"], // Labels for filtering in Prometheus + labelNames: ["method", "route", "status"], +}); + +const locationsTotal = new Gauge({ + name: `${prefix}locations_total`, + help: "Total number of location entries in database", + labelNames: ["database"], +}); + +const wifiLocationTotal = new Gauge({ + name: `${prefix}wifi_locations_total`, + help: "Total number of wifi location entries in database", + labelNames: ["database"], +}); + +const wigleApiRequest = new Gauge({ + name: `${prefix}wigle_api_requests_total`, + help: "Total number of wifi location entries in database", + labelNames: ["wigle"], }); // Define the metrics endpoint @@ -20,6 +44,10 @@ router.get("/", async (req: Request, res: Response) => { try { console.log("Metric Endpoint triggered"); + locationsTotal.set((await locationService.getAllLocations()).length); + wifiLocationTotal.set((await wifiLocationService.getAllWifiLocations()).length); + wigleApiRequest.set(metricsService.getWigleApiNumberOfRequests()); + // Increment the counter with labels requestCounter.inc({ method: req.method, route: req.route.path, status: 200 }); diff --git a/server/src/proxy/wigle.ts b/server/src/proxy/wigle.ts index 5bc5747..d6903de 100644 --- a/server/src/proxy/wigle.ts +++ b/server/src/proxy/wigle.ts @@ -1,3 +1,8 @@ +import { container } from "tsyringe"; +import { MetricsService } from "../services/metricsService"; + +const metricsService = container.resolve(MetricsService); + interface WigleApiResonse { response?: WifiLocationResponse, status_code: number, @@ -47,6 +52,7 @@ interface Result { export const getLocationForWifi = async ( mac: string ): Promise => { + metricsService.incWigleApiNumberOfRequests(); try { const url = `${process.env.WIGLE_BASE_URL!}${process.env .WIGLE_NETWORK_SEARCH!}?netid=${encodeURIComponent(mac)}`; diff --git a/server/src/services/metricsService.ts b/server/src/services/metricsService.ts new file mode 100644 index 0000000..35e6309 --- /dev/null +++ b/server/src/services/metricsService.ts @@ -0,0 +1,23 @@ +import { inject, injectable } from "tsyringe"; + +@injectable() +export class MetricsService { + private wigleApiNumberOfRequests: number; + + constructor() { + this.wigleApiNumberOfRequests = 0; // Initialize the variable + } + + // Method to increment the request count + public incWigleApiNumberOfRequests() { + console.log("inc") + this.wigleApiNumberOfRequests += 1; // Increment the number of requests + console.log(this.wigleApiNumberOfRequests) + } + + // Method to get the current request count + public getWigleApiNumberOfRequests() { + console.log("get: " + this.wigleApiNumberOfRequests) + return this.wigleApiNumberOfRequests; + } +} \ No newline at end of file