diff --git a/server/src/controller/metricsController.ts b/server/src/controller/metricsController.ts index 03d5caf..4e0b1ff 100644 --- a/server/src/controller/metricsController.ts +++ b/server/src/controller/metricsController.ts @@ -4,58 +4,21 @@ import { Counter, Gauge, collectDefaultMetrics, register } from "prom-client"; import { LocationService } from "../services/locationService"; import { WifiLocationService } from "../services/wifiLocationService"; import { TtnGatewayReceptionService } from "../services/ttnGatewayReceptionService"; +import { MetricsService } from "../services/metricsService"; const router = express.Router(); const locationService = container.resolve(LocationService); const wifiLocationService = container.resolve(WifiLocationService); const ttnGatewayReceptionService = container.resolve(TtnGatewayReceptionService); +const metricsService = container.resolve(MetricsService); -// Collect default system metrics (e.g., CPU, memory usage) -const prefix = 'locationhub_'; -collectDefaultMetrics({ prefix }); - -// Define a custom Counter metric -const requestCounter = new Counter({ - name: `${prefix}http_requests_total`, - help: "Total number of HTTP requests", - labelNames: ["method", "route", "status"], -}); - -const locationsTotal = new Gauge({ - name: `${prefix}locations_total`, - help: "Total number of location entries in database", - labelNames: ["database"], -}); - -const gnssLocationsTotal = new Gauge({ - name: `${prefix}gnss_locations_total`, - help: "Total number of location entries with GNSS 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 wifiLocationNotResolvable = new Gauge({ - name: `${prefix}wifi_locations_not_resolvable`, - help: "Unresolved number of wifi location entries in database", - labelNames: ["database"], -}); - -const wifiLocationRequestLimitExceeded = new Gauge({ - name: `${prefix}wifi_locations_request_limit_exceeded`, - help: "Unresolved number of wifi location because request limit exceeded entries in database", - labelNames: ["database"], -}); - -const ttnGatewayReceptions = new Gauge({ - name: `${prefix}ttn_gateway_receptions`, - help: "Total number of TTN Gateway receptions entries in database", - labelNames: ["database"], -}); +const requestCounter = metricsService.getRequestCounter(); +const locationsTotal = metricsService.getLocationsTotal(); +const gnssLocationsTotal = metricsService.getGnssLocationsTotal() +const wifiLocationTotal = metricsService.getWifiLocationTotal(); +const wifiLocationNotResolvable = metricsService.getWifiLocationResolvable(); +const wifiLocationRequestLimitExceeded = metricsService.getLWifiLocationRequestLimitExceeded(); +const ttnGatewayReceptions = metricsService.getTnnGatewayReceptions(); // Define the metrics endpoint router.get("/", async (req: Request, res: Response) => { diff --git a/server/src/services/metricsService.ts b/server/src/services/metricsService.ts new file mode 100644 index 0000000..3b22898 --- /dev/null +++ b/server/src/services/metricsService.ts @@ -0,0 +1,70 @@ +import {injectable } from "tsyringe"; +import { Counter, Gauge, collectDefaultMetrics} from "prom-client"; + +// Collect default system metrics (e.g., CPU, memory usage) +const prefix = 'locationhub_'; +collectDefaultMetrics({ prefix }); + +@injectable() +export class MetricsService { + constructor( + ) { } + + public getRequestCounter() { + const requestCounter = new Counter({ + name: `${prefix}http_requests_total`, + help: "Total number of HTTP requests", + labelNames: ["method", "route", "status"], + }); + return requestCounter; + } + + public getLocationsTotal() { + const locationsTotal = new Gauge({ + name: `${prefix}locations_total`, + help: "Total number of location entries in database", + labelNames: ["database"], + }); + return locationsTotal; + } + public getGnssLocationsTotal() { + const gnssLocationsTotal = new Gauge({ + name: `${prefix}gnss_locations_total`, + help: "Total number of location entries with GNSS in database", + labelNames: ["database"], + }); + return gnssLocationsTotal; + } + public getWifiLocationTotal() { + const wifiLocationTotal = new Gauge({ + name: `${prefix}wifi_locations_total`, + help: "Total number of wifi location entries in database", + labelNames: ["database"], + }); + return wifiLocationTotal; + } + public getWifiLocationResolvable() { + const wifiLocationNotResolvable = new Gauge({ + name: `${prefix}wifi_locations_not_resolvable`, + help: "Unresolved number of wifi location entries in database", + labelNames: ["database"], + }); + return wifiLocationNotResolvable; + } + public getLWifiLocationRequestLimitExceeded() { + const wifiLocationRequestLimitExceeded = new Gauge({ + name: `${prefix}wifi_locations_request_limit_exceeded`, + help: "Unresolved number of wifi location because request limit exceeded entries in database", + labelNames: ["database"], + }); + return wifiLocationRequestLimitExceeded; + } + public getTnnGatewayReceptions() { + const ttnGatewayReceptions = new Gauge({ + name: `${prefix}ttn_gateway_receptions`, + help: "Total number of TTN Gateway receptions entries in database", + labelNames: ["database"], + }); + return ttnGatewayReceptions; + } +}