Rename /probe to /metrics
This commit is contained in:
parent
ff7fd44f2c
commit
d0b9b6a36e
3 changed files with 17 additions and 14 deletions
|
@ -9,9 +9,9 @@ export type HttpResponse = {
|
||||||
export type HttpServerConfig = {
|
export type HttpServerConfig = {
|
||||||
port: number
|
port: number
|
||||||
requestInterceptor: () => HttpResponse | undefined
|
requestInterceptor: () => HttpResponse | undefined
|
||||||
probeController: () => HttpResponse
|
metricsController: () => HttpResponse
|
||||||
notFoundController: () => HttpResponse
|
notFoundController: () => HttpResponse
|
||||||
errorController: () => HttpResponse
|
errorController: (error: unknown) => HttpResponse
|
||||||
logger: Logger
|
logger: Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ function adaptResponseToReply(response: HttpResponse, reply: FastifyReply): void
|
||||||
export const serve: HttpServer = async ({
|
export const serve: HttpServer = async ({
|
||||||
port,
|
port,
|
||||||
requestInterceptor,
|
requestInterceptor,
|
||||||
probeController,
|
metricsController,
|
||||||
notFoundController,
|
notFoundController,
|
||||||
errorController,
|
errorController,
|
||||||
}) => {
|
}) => {
|
||||||
|
@ -61,15 +61,15 @@ export const serve: HttpServer = async ({
|
||||||
})
|
})
|
||||||
|
|
||||||
fastify.setErrorHandler(async (error, request: FastifyRequest, reply: FastifyReply) => {
|
fastify.setErrorHandler(async (error, request: FastifyRequest, reply: FastifyReply) => {
|
||||||
adaptResponseToReply(errorController(), reply)
|
adaptResponseToReply(errorController(error), reply)
|
||||||
})
|
})
|
||||||
|
|
||||||
fastify.setNotFoundHandler(async (request: FastifyRequest, reply: FastifyReply) => {
|
fastify.setNotFoundHandler(async (request: FastifyRequest, reply: FastifyReply) => {
|
||||||
adaptResponseToReply(notFoundController(), reply)
|
adaptResponseToReply(notFoundController(), reply)
|
||||||
})
|
})
|
||||||
|
|
||||||
fastify.get('/probe', async (request: FastifyRequest, reply: FastifyReply) => {
|
fastify.get('/metrics', async (request: FastifyRequest, reply: FastifyReply) => {
|
||||||
adaptResponseToReply(probeController(), reply)
|
adaptResponseToReply(metricsController(), reply)
|
||||||
})
|
})
|
||||||
|
|
||||||
await fastify.listen({ port, host: '::' })
|
await fastify.listen({ port, host: '::' })
|
||||||
|
|
|
@ -61,9 +61,9 @@ export class PrometheusExporterPlatform implements IndependentPlatformPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
probeController: () => {
|
metricsController: () => {
|
||||||
const renderer = new MetricsRenderer('homebridge')
|
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 {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
|
@ -74,13 +74,16 @@ export class PrometheusExporterPlatform implements IndependentPlatformPlugin {
|
||||||
notFoundController: () => ({
|
notFoundController: () => ({
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
headers: contentTypeHeader,
|
headers: contentTypeHeader,
|
||||||
body: 'Not found. Try /probe',
|
body: 'Not found. Try /metrics',
|
||||||
}),
|
}),
|
||||||
errorController: () => ({
|
errorController: (e) => {
|
||||||
|
this.log.error('HTTP request error: %o', e)
|
||||||
|
return {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
headers: contentTypeHeader,
|
headers: contentTypeHeader,
|
||||||
body: 'Server error',
|
body: 'Server error',
|
||||||
}),
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.then((http) => {
|
.then((http) => {
|
||||||
this.log.debug('HTTP server started on port %d', this.config.probe_port)
|
this.log.debug('HTTP server started on port %d', this.config.probe_port)
|
||||||
|
|
Loading…
Reference in a new issue