From 1d13bf9745d5808dc92a6de8b9746c2f61c5db7c Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Tue, 10 Aug 2021 15:27:29 -0700 Subject: [PATCH] fix(prometheus): metrics.ts leaks to global registry #1202 1. Specified a `register` property of the gauges as an empty array so that it does not pollute the global namespace. This is how this is supposed to be done as per the docs of prom-client. 2. Once the change from 1) took place, the issue became that the metrics gathering code was still trying to hit up the global scope for the metrics, e.g. calling the get metrics methods directly on the promClient object instead of the registry that we create for each prmoetheus exporter object separately. So a little additional refactor ensued to fix this as well by making sure that we grab a reference of the registry object at construction time and then re-use that wherever needed instead of going through the global promClient object. 3. Added missing .startMetricsCollection calls in the plugin constructors to ensure that the prometheus exporter object gets initialized properly (this is where the registry gets created as well so without this there are crashes happening when one tries to access the metrics through the registry) Why though? The problem was that the metrics.ts file that we have for all the plugin's metrics constructs a new Metric (Gauge) object at import time which then defaults to registering the metric in the global registry of prom-client by default. The latter was causing crashes when separate versions of the same metrics.ts file are imported in a scenario were the API server imports plugins from a different directory (this issue is coming from the branch where I'm working on plugin sandboxing via the live-plugin-manager). Fixes #1202 Signed-off-by: Peter Somogyvari --- .../main/typescript/prometheus-exporter/prometheus-exporter.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index fa119b0ee53..ba705e04327 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -13,7 +13,6 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; - public readonly transactions: Transactions = { counter: 0, totalTime: 0,