diff --git a/changelog.d/355.bugfix b/changelog.d/355.bugfix new file mode 100644 index 00000000..f036979e --- /dev/null +++ b/changelog.d/355.bugfix @@ -0,0 +1 @@ +Fix a bug that prevented bridges from calling `getPrometheusMetrics` without first calling `listen` in `Bridge`. \ No newline at end of file diff --git a/src/bridge.ts b/src/bridge.ts index 92d2f798..452a8d28 100644 --- a/src/bridge.ts +++ b/src/bridge.ts @@ -613,6 +613,10 @@ export class Bridge { bindAddress: "127.0.0.1", }); + if (this.metrics) { + this.metrics.registerMatrixSdkMetrics(this.botSdkAS); + } + this.clientFactory = this.opts.clientFactory || new ClientFactory({ url: this.opts.homeserverUrl, token: asToken, @@ -730,7 +734,7 @@ export class Bridge { this.onLog(line, false); }); - this.customiseAppservice(); + this.customiseAppserviceThirdPartyLookup(); if (this.metrics) { this.metrics.addAppServicePath(this); } @@ -750,13 +754,6 @@ export class Bridge { await this.listen(port, hostname, backlog, appServiceInstance); } - /** - * Apply any customisations required on the appService object. - */ - private customiseAppservice() { - this.customiseAppserviceThirdPartyLookup(); - } - // Set a timer going which will periodically remove Intent objects to prevent // them from accumulating too much. Removal is based on access time (calls to // getIntent). Intents expire after `INTENT_CULL_EVICT_AFTER_MS` of not being called. @@ -1509,6 +1506,7 @@ export class Bridge { * The instance will automatically register the Matrix SDK metrics by calling * {PrometheusMetrics~registerMatrixSdkMetrics}. * @param {boolean} registerEndpoint Register the /metrics endpoint on the appservice HTTP server. Defaults to true. + * Note: `listen()` must have been called if this is true or this will throw. * @param {Registry?} registry Optionally provide an alternative registry for metrics. */ public getPrometheusMetrics(registerEndpoint = true, registry?: Registry): PrometheusMetrics { @@ -1518,18 +1516,12 @@ export class Bridge { const metrics = this.metrics = new PrometheusMetrics(registry); - if (!this.botSdkAS) { - throw Error('initalise() not called, cannot listen'); - } - - metrics.registerMatrixSdkMetrics(this.botSdkAS); - - // TODO(paul): register some bridge-wide standard ones here - - // In case we're called after .run() - if (this.appService && registerEndpoint) { + if (this.botSdkAS) { + metrics.registerMatrixSdkMetrics(this.botSdkAS); + } // Else, we will set this up in initalise() + if (registerEndpoint && this.appservice) { metrics.addAppServicePath(this); - } + } // Else, we will add the path in listen() return metrics; }