move metrics definition to service
This commit is contained in:
		| @ -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) => { | ||||
|  | ||||
							
								
								
									
										70
									
								
								server/src/services/metricsService.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								server/src/services/metricsService.ts
									
									
									
									
									
										Normal file
									
								
							| @ -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; | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user