diff --git a/config.schema.json b/config.schema.json index 80a51cc..496e77d 100644 --- a/config.schema.json +++ b/config.schema.json @@ -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", diff --git a/src/platform.ts b/src/platform.ts index 03e7757..ab97f43 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -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 diff --git a/src/prometheus.ts b/src/prometheus.ts index e54270b..ba8a1a8 100644 --- a/src/prometheus.ts +++ b/src/prometheus.ts @@ -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 {