diff --git a/src/http/api.ts b/src/http/api.ts index 6ca148d..ea7efe9 100644 --- a/src/http/api.ts +++ b/src/http/api.ts @@ -9,9 +9,9 @@ export type HttpResponse = { export type HttpServerConfig = { port: number requestInterceptor: () => HttpResponse | undefined - probeController: () => HttpResponse + metricsController: () => HttpResponse notFoundController: () => HttpResponse - errorController: () => HttpResponse + errorController: (error: unknown) => HttpResponse logger: Logger } diff --git a/src/http/fastify.ts b/src/http/fastify.ts index b11fd49..76bd5bc 100644 --- a/src/http/fastify.ts +++ b/src/http/fastify.ts @@ -42,7 +42,7 @@ function adaptResponseToReply(response: HttpResponse, reply: FastifyReply): void export const serve: HttpServer = async ({ port, requestInterceptor, - probeController, + metricsController, notFoundController, errorController, }) => { @@ -61,15 +61,15 @@ export const serve: HttpServer = async ({ }) fastify.setErrorHandler(async (error, request: FastifyRequest, reply: FastifyReply) => { - adaptResponseToReply(errorController(), reply) + adaptResponseToReply(errorController(error), reply) }) fastify.setNotFoundHandler(async (request: FastifyRequest, reply: FastifyReply) => { adaptResponseToReply(notFoundController(), reply) }) - fastify.get('/probe', async (request: FastifyRequest, reply: FastifyReply) => { - adaptResponseToReply(probeController(), reply) + fastify.get('/metrics', async (request: FastifyRequest, reply: FastifyReply) => { + adaptResponseToReply(metricsController(), reply) }) await fastify.listen({ port, host: '::' }) diff --git a/src/platform.ts b/src/platform.ts index 4b9c17c..dde27e1 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -61,9 +61,9 @@ export class PrometheusExporterPlatform implements IndependentPlatformPlugin { } } }, - probeController: () => { + metricsController: () => { const renderer = new MetricsRenderer('homebridge') - const metrics = this.metrics.map(renderer.render).join('\n') + const metrics = this.metrics.map((metric) => renderer.render(metric)).join('\n') return { statusCode: 200, @@ -74,13 +74,16 @@ export class PrometheusExporterPlatform implements IndependentPlatformPlugin { notFoundController: () => ({ statusCode: 404, headers: contentTypeHeader, - body: 'Not found. Try /probe', - }), - errorController: () => ({ - statusCode: 500, - headers: contentTypeHeader, - body: 'Server error', + body: 'Not found. Try /metrics', }), + errorController: (e) => { + this.log.error('HTTP request error: %o', e) + return { + statusCode: 500, + headers: contentTypeHeader, + body: 'Server error', + } + }, }) .then((http) => { this.log.debug('HTTP server started on port %d', this.config.probe_port)