From a01cb54326032b6abb6b6de334cad30e920a8624 Mon Sep 17 00:00:00 2001 From: jerryfan01234 <44346807+jerryfan01234@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:21:20 -0400 Subject: [PATCH] [OTE-821] Affiliates roundtable metrics (#2311) --- .../tasks/update-affiliate-info.test.ts | 18 ++++++++++++++++++ .../tasks/update-wallet-total-volume.test.ts | 18 ++++++++++++++++++ .../src/tasks/update-affiliate-info.ts | 11 ++++++++++- .../src/tasks/update-wallet-total-volume.ts | 11 ++++++++++- 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts b/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts index 6f3884ab41..adc63c68e3 100644 --- a/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts +++ b/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts @@ -1,3 +1,4 @@ +import { stats } from '@dydxprotocol-indexer/base'; import { dbHelpers, testConstants, @@ -263,6 +264,23 @@ describe('update-affiliate-info', () => { const updatedInfo = await AffiliateInfoTable.findById(testConstants.defaultWallet2.address); expect(updatedInfo).toEqual(expectedAffiliateInfo); }); + + it('Successfully records metrics', async () => { + jest.spyOn(stats, 'gauge'); + + await PersistentCacheTable.create({ + key: PersistentCacheKeys.AFFILIATE_INFO_UPDATE_TIME, + value: DateTime.utc().toISO(), + }); + + await affiliateInfoUpdateTask(); + + expect(stats.gauge).toHaveBeenCalledWith( + `roundtable.persistent_cache_${PersistentCacheKeys.AFFILIATE_INFO_UPDATE_TIME}_lag_seconds`, + expect.any(Number), + { cache: PersistentCacheKeys.AFFILIATE_INFO_UPDATE_TIME }, + ); + }); }); async function getAffiliateInfoUpdateTime(): Promise { diff --git a/indexer/services/roundtable/__tests__/tasks/update-wallet-total-volume.test.ts b/indexer/services/roundtable/__tests__/tasks/update-wallet-total-volume.test.ts index 77e4cfec40..9cc63afb81 100644 --- a/indexer/services/roundtable/__tests__/tasks/update-wallet-total-volume.test.ts +++ b/indexer/services/roundtable/__tests__/tasks/update-wallet-total-volume.test.ts @@ -1,3 +1,4 @@ +import { stats } from '@dydxprotocol-indexer/base'; import { dbHelpers, testConstants, @@ -222,6 +223,23 @@ describe('update-wallet-total-volume', () => { totalVolume: '14', // 1 + 4 + 9 })); }); + + it('Successfully records metrics', async () => { + jest.spyOn(stats, 'gauge'); + + await PersistentCacheTable.create({ + key: PersistentCacheKeys.TOTAL_VOLUME_UPDATE_TIME, + value: DateTime.utc().toISO(), + }); + + await walletTotalVolumeUpdateTask(); + + expect(stats.gauge).toHaveBeenCalledWith( + `roundtable.persistent_cache_${PersistentCacheKeys.TOTAL_VOLUME_UPDATE_TIME}_lag_seconds`, + expect.any(Number), + { cache: PersistentCacheKeys.TOTAL_VOLUME_UPDATE_TIME }, + ); + }); }); async function getTotalVolumeUpdateTime(): Promise { diff --git a/indexer/services/roundtable/src/tasks/update-affiliate-info.ts b/indexer/services/roundtable/src/tasks/update-affiliate-info.ts index 58191d5abf..e3546eeec9 100644 --- a/indexer/services/roundtable/src/tasks/update-affiliate-info.ts +++ b/indexer/services/roundtable/src/tasks/update-affiliate-info.ts @@ -1,4 +1,4 @@ -import { logger } from '@dydxprotocol-indexer/base'; +import { logger, stats } from '@dydxprotocol-indexer/base'; import { PersistentCacheTable, AffiliateInfoTable, @@ -10,6 +10,8 @@ import { } from '@dydxprotocol-indexer/postgres'; import { DateTime } from 'luxon'; +import config from '../config'; + const defaultLastUpdateTime: string = '2024-09-16T00:00:00Z'; /** @@ -36,6 +38,13 @@ export default async function runTask(): Promise { ? persistentCacheEntry.value : defaultLastUpdateTime); + // Track how long ago the last update time (windowStartTime) in persistent cache was + stats.gauge( + `${config.SERVICE_NAME}.persistent_cache_${PersistentCacheKeys.AFFILIATE_INFO_UPDATE_TIME}_lag_seconds`, + DateTime.utc().diff(windowStartTime).as('seconds'), + { cache: PersistentCacheKeys.AFFILIATE_INFO_UPDATE_TIME }, + ); + let windowEndTime = DateTime.fromISO(latestBlock.time); // During backfilling, we process one day at a time to reduce roundtable runtime. if (windowEndTime > windowStartTime.plus({ days: 1 })) { diff --git a/indexer/services/roundtable/src/tasks/update-wallet-total-volume.ts b/indexer/services/roundtable/src/tasks/update-wallet-total-volume.ts index 7908c2bfc5..0286f1ef8f 100644 --- a/indexer/services/roundtable/src/tasks/update-wallet-total-volume.ts +++ b/indexer/services/roundtable/src/tasks/update-wallet-total-volume.ts @@ -1,4 +1,4 @@ -import { logger } from '@dydxprotocol-indexer/base'; +import { logger, stats } from '@dydxprotocol-indexer/base'; import { PersistentCacheTable, WalletTable, @@ -10,6 +10,8 @@ import { } from '@dydxprotocol-indexer/postgres'; import { DateTime } from 'luxon'; +import config from '../config'; + const defaultLastUpdateTime: string = '2023-10-26T00:00:00Z'; /** @@ -39,6 +41,13 @@ export default async function runTask(): Promise { ? persistentCacheEntry.value : defaultLastUpdateTime); + // Track how long ago the last update time (windowStartTime) in persistent cache was + stats.gauge( + `${config.SERVICE_NAME}.persistent_cache_${PersistentCacheKeys.TOTAL_VOLUME_UPDATE_TIME}_lag_seconds`, + DateTime.utc().diff(windowStartTime).as('seconds'), + { cache: PersistentCacheKeys.TOTAL_VOLUME_UPDATE_TIME }, + ); + let windowEndTime = DateTime.fromISO(latestBlock.time); // During backfilling, we process one day at a time to reduce roundtable runtime. if (windowEndTime > windowStartTime.plus({ days: 1 })) {