Allow configuring the prefix

This commit is contained in:
Lars Strojny 2022-11-08 14:28:13 +01:00
parent 900f528b95
commit 4a7d6ff6de
3 changed files with 15 additions and 3 deletions

View file

@ -17,6 +17,12 @@
"required": false,
"default": false
},
"prefix": {
"title": "Metrics prefix",
"type": "string",
"required": false,
"default": "homebridge"
},
"port": {
"title": "Probe server port",
"description": "TCP port for the prometheus probe server to listen to",

View file

@ -24,7 +24,7 @@ export class PrometheusExporterPlatform implements IndependentPlatformPlugin {
this.log.debug('Starting probe HTTP server on port %d', this.config.port)
this.httpServer = new PrometheusServer(this.config.port, this.log, this.config.debug)
this.httpServer = new PrometheusServer(this.config.port, this.log, this.config.debug, this.config.prefix)
serve(this.httpServer)
.then((httpServerController) => {
this.log.debug('HTTP server started on port %d', this.config.port)
@ -47,6 +47,7 @@ export class PrometheusExporterPlatform implements IndependentPlatformPlugin {
this.config.debug = this.config.debug ?? false
this.config.port = this.config.port ?? 36123
this.config.prefix = this.config.prefix ?? 'homebridge'
this.config.refresh_interval = this.config.refresh_interval || 60
this.config.request_timeout = this.config.request_timeout || 10
this.config.discovery_timeout = this.config.discovery_timeout || 20

View file

@ -78,7 +78,12 @@ export class PrometheusServer implements HttpServer {
private metricsInitialized = false
private metrics: Metric[] = []
constructor(public readonly port: number, public readonly log: Logger, public readonly debug: boolean) {}
constructor(
public readonly port: number,
public readonly log: Logger,
public readonly debug: boolean,
private readonly prefix: string,
) {}
onRequest(): HttpResponse | undefined {
if (!this.metricsInitialized) {
@ -91,7 +96,7 @@ export class PrometheusServer implements HttpServer {
}
onMetrics(): HttpResponse {
const renderer = new MetricsRenderer('homebridge')
const renderer = new MetricsRenderer(this.prefix)
const metrics = this.metrics.map((metric) => renderer.render(metric)).join('\n')
return {