Skip to content

Commit

Permalink
feat: 0-conf Prometheus metrics (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Nov 7, 2024
1 parent 1ab9de7 commit bb1a42f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Boltz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class Boltz {

this.prometheus = new Prometheus(
this.logger,
this.service,
this.api,
this.config.prometheus,
this.config.pairs,
Expand Down
36 changes: 36 additions & 0 deletions lib/Prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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[],
Expand Down Expand Up @@ -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 = (
Expand Down
9 changes: 9 additions & 0 deletions lib/rates/LockupTransactionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit bb1a42f

Please sign in to comment.