Add Prometheus metrics endpoint #14
@@ -57,18 +57,31 @@ const ttnGatewayReceptions = new Gauge({
|
|||||||
labelNames: ["database"],
|
labelNames: ["database"],
|
||||||
});
|
});
|
||||||
|
localhorst marked this conversation as resolved
Outdated
|
|||||||
|
|
||||||
|
|
||||||
// Define the metrics endpoint
|
// Define the metrics endpoint
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
console.log("Metric Endpoint triggered");
|
const [
|
||||||
|
allLocations,
|
||||||
|
gnssLocations,
|
||||||
|
allWifiLocations,
|
||||||
|
wifiLocationsNotResolvable,
|
||||||
|
wifiLocationsRequestLimitExceeded,
|
||||||
|
allTtnGatewayReceptions
|
||||||
|
] = await Promise.all([
|
||||||
|
locationService.getAllLocations(),
|
||||||
|
localhorst marked this conversation as resolved
Outdated
Pheanox
commented
Ich würde auf jedenfall hier auf alle promises gleichzeitig warten mit Promise.all. Damit es wieder gleichzeitig abgearbeitet werden kann. Und dann weil sonst der Controller zu voll wird, würde ich das vermutlich in einen MetricService auslagern Ich würde auf jedenfall hier auf alle promises gleichzeitig warten mit Promise.all. Damit es wieder gleichzeitig abgearbeitet werden kann. Und dann weil sonst der Controller zu voll wird, würde ich das vermutlich in einen MetricService auslagern
|
|||||||
|
locationService.getAllGnssLocations(),
|
||||||
|
wifiLocationService.getAllWifiLocations(),
|
||||||
|
wifiLocationService.getAllWifiLocationsByNotResolvable(),
|
||||||
|
wifiLocationService.getAllWifiLocationsByRequestLimitExceeded(),
|
||||||
|
ttnGatewayReceptionService.getAllGatewayReceptions()
|
||||||
|
]);
|
||||||
|
|
||||||
locationsTotal.set((await locationService.getAllLocations()).length);
|
locationsTotal.set(allLocations.length);
|
||||||
gnssLocationsTotal.set((await locationService.getAllGnssLocations()).length);
|
gnssLocationsTotal.set(gnssLocations.length);
|
||||||
wifiLocationTotal.set((await wifiLocationService.getAllWifiLocations()).length);
|
wifiLocationTotal.set(allWifiLocations.length);
|
||||||
wifiLocationNotResolvable.set((await wifiLocationService.getAllWifiLocationsByNotResolvable()).length);
|
wifiLocationNotResolvable.set(wifiLocationsNotResolvable.length);
|
||||||
wifiLocationRequestLimitExceeded.set((await wifiLocationService.getAllWifiLocationsByRequestLimitExceeded()).length);
|
wifiLocationRequestLimitExceeded.set(wifiLocationsRequestLimitExceeded.length);
|
||||||
ttnGatewayReceptions.set((await ttnGatewayReceptionService.getAllGatewayReceptions()).length);
|
ttnGatewayReceptions.set(allTtnGatewayReceptions.length);
|
||||||
|
|
||||||
// Increment the counter with labels
|
// Increment the counter with labels
|
||||||
requestCounter.inc({ method: req.method, route: req.route.path, status: 200 });
|
requestCounter.inc({ method: req.method, route: req.route.path, status: 200 });
|
||||||
@@ -79,7 +92,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Increment the counter for errors
|
// Increment the counter for errors
|
||||||
requestCounter.inc({ method: req.method, route: req.route.path, status: 500 });
|
requestCounter.inc({ method: req.method, route: req.route.path, status: 500 });
|
||||||
|
|
||||||
console.error("Error running metrics endpoint:", error);
|
console.error("Error running metrics endpoint:", error);
|
||||||
res.status(500).json({ error: "Error running metrics endpoint" });
|
res.status(500).json({ error: "Error running metrics endpoint" });
|
||||||
}
|
}
|
||||||
|
|||||||
Ich würde das in eine extra Datei auslagern