wigle api request counter
This commit is contained in:
		| @ -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 }); | ||||
|  | ||||
|  | ||||
| @ -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)}`; | ||||
|  | ||||
							
								
								
									
										23
									
								
								server/src/services/metricsService.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								server/src/services/metricsService.ts
									
									
									
									
									
										Normal 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; | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user