wigle api request counter

This commit is contained in:
Hendrik Schutter 2025-01-11 23:38:44 +01:00
parent 9b00853d5a
commit e8154d1e13
3 changed files with 61 additions and 4 deletions

View File

@ -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 });

View File

@ -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<WigleApiResonse | undefined> => {
metricsService.incWigleApiNumberOfRequests();
try {
const url = `${process.env.WIGLE_BASE_URL!}${process.env
.WIGLE_NETWORK_SEARCH!}?netid=${encodeURIComponent(mac)}`;

View File

@ -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;
}
}