diff --git a/lib/Boltz.ts b/lib/Boltz.ts index 82b35249..e933f0f3 100644 --- a/lib/Boltz.ts +++ b/lib/Boltz.ts @@ -209,6 +209,7 @@ class Boltz { this.prometheus = new Prometheus( this.logger, + this.service, this.api, this.config.prometheus, this.config.pairs, diff --git a/lib/Prometheus.ts b/lib/Prometheus.ts index 480a1b67..b5a7cba6 100644 --- a/lib/Prometheus.ts +++ b/lib/Prometheus.ts @@ -5,6 +5,7 @@ import { getPairId } from './Utils'; import Api from './api/Api'; import { PairConfig } from './consts/Types'; import StatsRepository, { SwapType } from './db/repositories/StatsRepository'; +import Service from './service/Service'; type PrometheusConfig = { host?: string; @@ -22,6 +23,7 @@ class Prometheus { constructor( private readonly logger: Logger, + private readonly service: Service, private readonly api: Api, private readonly config: PrometheusConfig | undefined, pairs: PairConfig[], @@ -165,6 +167,40 @@ class Prometheus { 'number of swap status messages cached', () => this.api.swapInfos.cacheSize, ); + + const service = this.service; + + this.swapRegistry!.registerMetric( + new Gauge({ + name: `${Prometheus.metric_prefix}zeroconf_risk`, + labelNames: ['symbol'], + help: '0-conf risk of a symbol', + collect: function () { + for (const { + symbol, + risk, + } of service.lockupTransactionTracker.risks()) { + this.set({ symbol }, Number(risk)); + } + }, + }), + ); + + this.swapRegistry!.registerMetric( + new Gauge({ + name: `${Prometheus.metric_prefix}zeroconf_risk_max`, + labelNames: ['symbol'], + help: 'max 0-conf risk of a symbol', + collect: function () { + for (const { + symbol, + maxRisk, + } of service.lockupTransactionTracker.maxRisks()) { + this.set({ symbol }, Number(maxRisk)); + } + }, + }), + ); }; private registerGauge = ( diff --git a/lib/rates/LockupTransactionTracker.ts b/lib/rates/LockupTransactionTracker.ts index ca453a49..796f2c94 100644 --- a/lib/rates/LockupTransactionTracker.ts +++ b/lib/rates/LockupTransactionTracker.ts @@ -112,6 +112,15 @@ class LockupTransactionTracker extends TypedEventEmitter<{ } }; + public maxRisks = () => + Array.from(this.maxRisk.entries()).map(([symbol, maxRisk]) => ({ + symbol, + maxRisk, + })); + + public risks = () => + Array.from(this.risk.entries()).map(([symbol, risk]) => ({ symbol, risk })); + public zeroConfAccepted = (symbol: string): boolean => this.zeroConfAcceptedMap.get(symbol) || false;