From ab9c6d5988ec761d9291331448a56b9a4b45d46f 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 --- .../src/main/typescript/api-server.ts | 1 + .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 12 +++++++----- .../src/main/typescript/plugin-consortium-manual.ts | 1 + .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 11 ++++++----- .../src/main/typescript/plugin-keychain-memory.ts | 1 + .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 13 ++++++++----- .../src/main/typescript/plugin-keychain-vault.ts | 1 + .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 12 +++++++----- .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 12 +++++++----- .../typescript/plugin-ledger-connector-corda.ts | 1 + .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 11 ++++++----- .../prometheus-exporter/prometheus-exporter.ts | 11 ++++++----- .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 12 +++++++----- .../main/typescript/prometheus-exporter/metrics.ts | 1 + .../prometheus-exporter/prometheus-exporter.ts | 12 +++++++----- 22 files changed, 74 insertions(+), 45 deletions(-) diff --git a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts index 31c31b0798..c9f94af4cc 100644 --- a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts +++ b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts @@ -120,6 +120,7 @@ export class ApiServer { pollingIntervalInMin: 1, }); } + this.prometheusExporter.startMetricsCollection(); this.prometheusExporter.setTotalPluginImports(this.getPluginImportsCount()); this.log = LoggerProvider.getOrCreate({ diff --git a/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/metrics.ts index 662ca99d7c..3e38c0c28f 100644 --- a/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/metrics.ts @@ -4,6 +4,7 @@ export const K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS = "cactus_api_server_total_plugin_imports"; export const totalTxCount = new Gauge({ + registers: [], name: K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS, help: "Total number of plugins imported", labelNames: ["type"], diff --git a/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index 7b464c52cd..afc62894f0 100644 --- a/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-cmd-api-server/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,6 +1,7 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { TotalPluginImports } from "./response.type"; import { K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS } from "./metrics"; +import { totalTxCount } from "./metrics"; import { collectMetrics } from "./data-fetcher"; export interface IPrometheusExporterOptions { @@ -10,12 +11,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly totalPluginImports: TotalPluginImports = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public setTotalPluginImports(totalPluginImports: number): void { @@ -24,15 +27,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_API_SERVER_TOTAL_PLUGIN_IMPORTS, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalTxCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } diff --git a/packages/cactus-plugin-consortium-manual/src/main/typescript/plugin-consortium-manual.ts b/packages/cactus-plugin-consortium-manual/src/main/typescript/plugin-consortium-manual.ts index db527b2767..9187e59980 100644 --- a/packages/cactus-plugin-consortium-manual/src/main/typescript/plugin-consortium-manual.ts +++ b/packages/cactus-plugin-consortium-manual/src/main/typescript/plugin-consortium-manual.ts @@ -84,6 +84,7 @@ export class PluginConsortiumManual this.prometheusExporter, `${fnTag} options.prometheusExporter`, ); + this.prometheusExporter.startMetricsCollection(); this.prometheusExporter.setNodeCount(this.getNodeCount()); } diff --git a/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/metrics.ts index 55d35149ed..b965dd39f4 100644 --- a/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/metrics.ts @@ -4,6 +4,7 @@ export const K_CACTUS_CONSORTIUM_MANUAL_TOTAL_NODE_COUNT = "cactus_consortium_manual_total_node_count"; export const totalTxCount = new Gauge({ + registers: [], name: K_CACTUS_CONSORTIUM_MANUAL_TOTAL_NODE_COUNT, help: "Total cactus node count", labelNames: ["type"], diff --git a/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index 4208c86359..6c07c56008 100644 --- a/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-consortium-manual/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,4 +1,4 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { NodeCount } from "./response.type"; import { totalTxCount, @@ -12,12 +12,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly nodeCount: NodeCount = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public setNodeCount(nodeCount: number): void { @@ -28,15 +30,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_CONSORTIUM_MANUAL_TOTAL_NODE_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalTxCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } diff --git a/packages/cactus-plugin-keychain-memory/src/main/typescript/plugin-keychain-memory.ts b/packages/cactus-plugin-keychain-memory/src/main/typescript/plugin-keychain-memory.ts index 38d95f4d1a..784da89976 100644 --- a/packages/cactus-plugin-keychain-memory/src/main/typescript/plugin-keychain-memory.ts +++ b/packages/cactus-plugin-keychain-memory/src/main/typescript/plugin-keychain-memory.ts @@ -58,6 +58,7 @@ export class PluginKeychainMemory { this.prometheusExporter, `${fnTag} options.prometheusExporter`, ); + this.prometheusExporter.startMetricsCollection(); this.log.info(`Created ${this.className}. KeychainID=${opts.keychainId}`); this.log.warn( diff --git a/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/metrics.ts index cf20d28cc9..ff644d22cf 100644 --- a/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/metrics.ts @@ -4,6 +4,7 @@ export const K_CACTUS_KEYCHAIN_MEMORY_TOTAL_KEY_COUNT = "cactus_keychain_memory_total_key_count"; export const totalKeyCount = new Gauge({ + registers: [], name: K_CACTUS_KEYCHAIN_MEMORY_TOTAL_KEY_COUNT, help: "Total keys present in memory", labelNames: ["type"], diff --git a/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index 4e3c22985b..a0975c45bc 100644 --- a/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-keychain-memory/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,7 +1,9 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { KeyCount } from "./response.type"; import { collectMetrics } from "./data-fetcher"; import { K_CACTUS_KEYCHAIN_MEMORY_TOTAL_KEY_COUNT } from "./metrics"; +import { totalKeyCount } from "./metrics"; + export interface IPrometheusExporterOptions { pollingIntervalInMin?: number; } @@ -9,12 +11,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly keyCount: KeyCount = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public setTotalKeyCounter(keyCount: number): void { @@ -23,15 +27,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_KEYCHAIN_MEMORY_TOTAL_KEY_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalKeyCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } diff --git a/packages/cactus-plugin-keychain-vault/src/main/typescript/plugin-keychain-vault.ts b/packages/cactus-plugin-keychain-vault/src/main/typescript/plugin-keychain-vault.ts index c105017b5e..9c7dda5a59 100644 --- a/packages/cactus-plugin-keychain-vault/src/main/typescript/plugin-keychain-vault.ts +++ b/packages/cactus-plugin-keychain-vault/src/main/typescript/plugin-keychain-vault.ts @@ -114,6 +114,7 @@ export class PluginKeychainVault implements IPluginWebService, IPluginKeychain { this.prometheusExporter, `${fnTag} options.prometheusExporter`, ); + this.prometheusExporter.startMetricsCollection(); this.log.info(`Created Vault backend OK. Endpoint=${this.endpoint}`); diff --git a/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/metrics.ts index 2f65f2e0c7..b2776cc0f5 100644 --- a/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/metrics.ts @@ -4,6 +4,7 @@ export const K_CACTUS_KEYCHAIN_VAULT_MANAGED_KEY_COUNT = "cactus_keychain_vault_managed_key_count"; export const totalKeyCount = new Gauge({ + registers: [], name: K_CACTUS_KEYCHAIN_VAULT_MANAGED_KEY_COUNT, help: "The number of keys that were set in the backing Vault deployment via this specific keychain plugin instance", diff --git a/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index f25017e07c..e05f3e26ff 100644 --- a/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-keychain-vault/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,7 +1,8 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { VaultKeys } from "./response.type"; import { collectMetrics } from "./data-fetcher"; import { K_CACTUS_KEYCHAIN_VAULT_MANAGED_KEY_COUNT } from "./metrics"; +import { totalKeyCount } from "./metrics"; export interface IPrometheusExporterOptions { pollingIntervalInMin?: number; @@ -10,12 +11,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly vaultKeys: VaultKeys = new Map(); + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public setTotalKeyCounter(key: string, operation: string): void { @@ -28,15 +31,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_KEYCHAIN_VAULT_MANAGED_KEY_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalKeyCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts index 6b2faa9435..8b1addf43e 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/metrics.ts @@ -3,6 +3,7 @@ import { Gauge } from "prom-client"; export const K_CACTUS_BESU_TOTAL_TX_COUNT = "cactus_besu_total_tx_count"; export const totalTxCount = new Gauge({ + registers: [], name: "cactus_besu_total_tx_count", help: "Total transactions executed", labelNames: ["type"], diff --git a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index 28eab02588..4ece66e25e 100644 --- a/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-ledger-connector-besu/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,7 +1,8 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { Transactions } from "./response.type"; import { collectMetrics } from "./data-fetcher"; import { K_CACTUS_BESU_TOTAL_TX_COUNT } from "./metrics"; +import { totalTxCount } from "./metrics"; export interface IPrometheusExporterOptions { pollingIntervalInMin?: number; @@ -10,12 +11,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly transactions: Transactions = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public addCurrentTransaction(): void { @@ -23,15 +26,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_BESU_TOTAL_TX_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalTxCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } diff --git a/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/plugin-ledger-connector-corda.ts b/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/plugin-ledger-connector-corda.ts index 68e7337927..c799c7403e 100644 --- a/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/plugin-ledger-connector-corda.ts +++ b/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/plugin-ledger-connector-corda.ts @@ -77,6 +77,7 @@ export class PluginLedgerConnectorCorda this.prometheusExporter, `${fnTag} options.prometheusExporter`, ); + this.prometheusExporter.startMetricsCollection(); } public getPrometheusExporter(): PrometheusExporter { diff --git a/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/metrics.ts index 57734600b2..4569ce9f14 100644 --- a/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/metrics.ts @@ -3,6 +3,7 @@ import { Gauge } from "prom-client"; export const K_CACTUS_CORDA_TOTAL_TX_COUNT = "cactus_corda_total_tx_count"; export const totalTxCount = new Gauge({ + registers: [], name: K_CACTUS_CORDA_TOTAL_TX_COUNT, help: "Total transactions executed", labelNames: ["type"], diff --git a/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index 052ec6c3a8..aaa6b459b9 100644 --- a/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-ledger-connector-corda/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,4 +1,4 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { Transactions } from "./response.type"; import { totalTxCount, K_CACTUS_CORDA_TOTAL_TX_COUNT } from "./metrics"; @@ -9,12 +9,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly transactions: Transactions = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public addCurrentTransaction(): void { @@ -25,15 +27,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_CORDA_TOTAL_TX_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalTxCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } 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 733cb6f736..8939683942 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 @@ -1,4 +1,4 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { Transactions } from "./response.type"; import { totalTxCount, K_CACTUS_FABRIC_TOTAL_TX_COUNT } from "./metrics"; @@ -9,12 +9,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly transactions: Transactions = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public addCurrentTransaction(): void { @@ -25,15 +27,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_FABRIC_TOTAL_TX_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalTxCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } diff --git a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/metrics.ts index 2e57bea80e..a620886d60 100644 --- a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/metrics.ts @@ -3,6 +3,7 @@ import { Gauge } from "prom-client"; export const K_CACTUS_QUORUM_TOTAL_TX_COUNT = "cactus_quorum_total_tx_count"; export const totalTxCount = new Gauge({ + registers: [], name: K_CACTUS_QUORUM_TOTAL_TX_COUNT, help: "Total transactions executed", labelNames: ["type"], diff --git a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index c09d47f2ba..462c8af871 100644 --- a/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-ledger-connector-quorum/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,7 +1,8 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { Transactions } from "./response.type"; import { collectMetrics } from "./data.fetcher"; import { K_CACTUS_QUORUM_TOTAL_TX_COUNT } from "./metrics"; +import { totalTxCount } from "./metrics"; export interface IPrometheusExporterOptions { pollingIntervalInMin?: number; @@ -10,12 +11,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly transactions: Transactions = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public addCurrentTransaction(): void { @@ -23,15 +26,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_QUORUM_TOTAL_TX_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalTxCount); + promClient.collectDefaultMetrics({ register: this.registry }); } } diff --git a/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/metrics.ts b/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/metrics.ts index 0a46752f6f..4fe5181e4a 100644 --- a/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/metrics.ts +++ b/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/metrics.ts @@ -3,6 +3,7 @@ import { Gauge } from "prom-client"; export const K_CACTUS_XDAI_TOTAL_TX_COUNT = "cactus_xdai_total_tx_count"; export const totalTxCount = new Gauge({ + registers: [], name: "cactus_xdai_total_tx_count", help: "Total transactions executed", labelNames: ["type"], diff --git a/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/prometheus-exporter.ts b/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/prometheus-exporter.ts index 43bfbdc710..b0b741c795 100644 --- a/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/prometheus-exporter.ts +++ b/packages/cactus-plugin-ledger-connector-xdai/src/main/typescript/prometheus-exporter/prometheus-exporter.ts @@ -1,7 +1,8 @@ -import promClient from "prom-client"; +import promClient, { Registry } from "prom-client"; import { Transactions } from "./response.type"; import { collectMetrics } from "./data-fetcher"; import { K_CACTUS_XDAI_TOTAL_TX_COUNT } from "./metrics"; +import { totalTxCount } from "./metrics"; export interface IPrometheusExporterOptions { pollingIntervalInMin?: number; @@ -10,12 +11,14 @@ export interface IPrometheusExporterOptions { export class PrometheusExporter { public readonly metricsPollingIntervalInMin: number; public readonly transactions: Transactions = { counter: 0 }; + public readonly registry: Registry; constructor( public readonly prometheusExporterOptions: IPrometheusExporterOptions, ) { this.metricsPollingIntervalInMin = prometheusExporterOptions.pollingIntervalInMin || 1; + this.registry = new Registry(); } public addCurrentTransaction(): void { @@ -23,15 +26,14 @@ export class PrometheusExporter { } public async getPrometheusMetrics(): Promise { - const result = await promClient.register.getSingleMetricAsString( + const result = await this.registry.getSingleMetricAsString( K_CACTUS_XDAI_TOTAL_TX_COUNT, ); return result; } public startMetricsCollection(): void { - const Registry = promClient.Registry; - const register = new Registry(); - promClient.collectDefaultMetrics({ register }); + this.registry.registerMetric(totalTxCount); + promClient.collectDefaultMetrics({ register: this.registry }); } }