Skip to content

Commit

Permalink
feat: Add prometheus gauge for total rfqm trade capacity (0xProject#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhinodavid authored Oct 7, 2021
1 parent 93b37f7 commit a656914
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/services/rfqm_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,19 @@ export class RfqmService {
public async runHealthCheckAsync(): Promise<HealthCheckResult> {
const heartbeats = await this._dbUtils.findRfqmWorkerHeartbeatsAsync();
const registryBalance = await this._blockchainUtils.getAccountBalanceAsync(this._registryAddress);
let gasPrice: BigNumber | undefined;
try {
gasPrice = await this._protocolFeeUtils.getGasPriceEstimationOrThrowAsync();
} catch (error) {
logger.warn({ error }, 'Failed to get gas price for health check');
}
return computeHealthCheckAsync(
RFQM_MAINTENANCE_MODE,
registryBalance,
RFQM_MAKER_ASSET_OFFERINGS,
this._sqsProducer,
heartbeats,
gasPrice,
);
}

Expand Down
18 changes: 16 additions & 2 deletions src/utils/rfqm_health_check.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RfqMakerAssetOfferings } from '@0x/asset-swapper';
import { BigNumber } from '@0x/utils';
import { Counter } from 'prom-client';
import { Counter, Gauge } from 'prom-client';
import { Producer } from 'sqs-producer';

import { ETH_DECIMALS } from '../constants';
import { ETH_DECIMALS, RFQM_TX_GAS_ESTIMATE } from '../constants';
import { RfqmWorkerHeartbeatEntity } from '../entities';

const SQS_QUEUE_SIZE_DEGRADED_THRESHOLD = 10; // More messages sitting in queue than this will cause a DEGRADED issue
Expand All @@ -26,6 +26,11 @@ const RFQM_HEALTH_CHECK_ISSUE_COUNTER = new Counter({
help: 'RFQM health system has detected a problem with the system',
});

const RFQM_TOTAL_SYSTEM_TRADE_CAPACITY_GAUGE = new Gauge({
name: 'rfqm_total_system_trade_capacity',
help: 'Total amount of ETH in the worker pool divided by the current expected gas of a trade',
});

export enum HealthCheckStatus {
Operational = 'operational',
Maintenance = 'maintenance',
Expand Down Expand Up @@ -81,6 +86,7 @@ export async function computeHealthCheckAsync(
offerings: RfqMakerAssetOfferings,
producer: Producer,
heartbeats: RfqmWorkerHeartbeatEntity[],
gasPrice?: BigNumber,
): Promise<HealthCheckResult> {
const pairs = transformPairs(offerings);

Expand All @@ -97,6 +103,14 @@ export async function computeHealthCheckAsync(
RFQM_HEALTH_CHECK_ISSUE_COUNTER.labels(issue.status, issue.label).inc(),
);

if (gasPrice) {
// Note that this gauge is an estimation of the total number of trades, since two workers could have
// 50% of the amount for one trade and the gauge would show 1 but the actual capacity would be 0.
const totalWorkerBalance = heartbeats.reduce((total, { balance }) => total.plus(balance), new BigNumber(0));
const totalSystemTradeCapacity = totalWorkerBalance.div(gasPrice.times(RFQM_TX_GAS_ESTIMATE));
RFQM_TOTAL_SYSTEM_TRADE_CAPACITY_GAUGE.set(totalSystemTradeCapacity.toNumber());
}

return {
status: getWorstStatus([httpStatus, workersStatus]),
pairs,
Expand Down
5 changes: 4 additions & 1 deletion test/services/rfqm_service_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,10 @@ describe('RfqmService', () => {

describe('runHealthCheckAsync', () => {
it('returns active pairs', async () => {
const service = buildRfqmServiceForUnitTest();
const dbUtilsMock = mock(RfqmDbUtils);
when(dbUtilsMock.findRfqmWorkerHeartbeatsAsync()).thenResolve([]);
const service = buildRfqmServiceForUnitTest({ dbUtils: instance(dbUtilsMock) });

const result = await service.runHealthCheckAsync();

expect(result.pairs).to.have.key(
Expand Down

0 comments on commit a656914

Please sign in to comment.