move metrics definition to service

This commit is contained in:
Hendrik Schutter 2025-01-25 19:08:30 +01:00
parent f969b0a4c0
commit 4d0e84b84a
2 changed files with 79 additions and 46 deletions

View File

@ -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) => {

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