Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add oracle balance metric #161

Merged
merged 6 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions __mocks__/@celo/contractkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export const newKit = () => ({
web3: {
eth: {
getTransaction: async (_: string) => Promise.resolve(),
getBalance: jest.fn(async (_: string) => Promise.resolve('1000000000000000000')),
},
utils: {
fromWei: jest.fn((value: number) => value / 1e18),
},
},
})
Expand Down
21 changes: 11 additions & 10 deletions src/default_config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import BigNumber from 'bignumber.js'
import Logger from 'bunyan'
import {
BaseReporterConfigSubset,
BlockBasedReporterConfigSubset,
DataAggregatorConfigSubset,
OracleApplicationConfig,
} from './app'
import {
AggregationMethod,
minutesToMs,
OracleCurrencyPair,
ReportStrategy,
secondsToMs,
WalletType,
minutesToMs,
secondsToMs,
} from './utils'
import {
BaseReporterConfigSubset,
BlockBasedReporterConfigSubset,
DataAggregatorConfigSubset,
OracleApplicationConfig,
} from './app'

import BigNumber from 'bignumber.js'
import Logger from 'bunyan'

export const baseLogger = Logger.createLogger({
name: 'oracle',
Expand Down
12 changes: 12 additions & 0 deletions src/metric_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class MetricCollector {

private lastBlockHeaderNumberGauge: Gauge<string>

private oracleBalanceValueGauge: Gauge<string>

private potentialReportValueGauge: Gauge<string>

private reportCountCounter: Counter<string>
Expand Down Expand Up @@ -113,6 +115,12 @@ export class MetricCollector {
labelNames: ['type'],
})

this.oracleBalanceValueGauge = new Gauge({
name: 'oracle_balance_value',
help: 'CELO balance of the oracle address',
labelNames: ['oracleAddress'],
})

this.potentialReportValueGauge = new Gauge({
name: 'oracle_potential_report_value',
help:
Expand Down Expand Up @@ -330,6 +338,10 @@ export class MetricCollector {
this.lastBlockHeaderNumberGauge.set({ type }, blockNumber)
}

oracleBalanceValue(oracleAddress: string, balance: number) {
this.oracleBalanceValueGauge.set({ oracleAddress }, balance)
}

websocketProviderSetup() {
this.websocketProviderSetupCounter.inc()
}
Expand Down
8 changes: 8 additions & 0 deletions src/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ export abstract class BaseReporter {
})
}

async setOracleBalanceMetric() {
const balance = await this.config.kit.web3.eth.getBalance(this.config.oracleAccount)
this.config.metricCollector!.oracleBalanceValue(
this.config.oracleAccount,
Number(this.config.kit.web3.utils.fromWei(balance))
)
}

get lastReportedPrice(): BigNumber | undefined {
return this._lastReportedPrice
}
Expand Down
1 change: 1 addition & 0 deletions src/reporters/block_based_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class BlockBasedReporter extends BaseReporter {
if (shouldReport) {
const trigger = heartbeat ? ReportTrigger.HEARTBEAT : ReportTrigger.PRICE_CHANGE
await this.report(price, trigger)
await this.setOracleBalanceMetric()
} else {
this.logger.info(
{
Expand Down
15 changes: 15 additions & 0 deletions test/reporters/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,21 @@ describe('BaseReporter', () => {
})
})

describe('setOracleBalanceMetric()', () => {
it('should set the account balance metric', async () => {
const mockBalanceInWei = '1000000000000000000'
const mockBalance = 1
await reporter.setOracleBalanceMetric()

expect(reporter.config.kit.web3.eth.getBalance).toHaveBeenLastCalledWith(mockOracleAccount)
expect(reporter.config.kit.web3.utils.fromWei).toHaveBeenCalledWith(mockBalanceInWei)
expect(metricCollector!.oracleBalanceValue).toHaveBeenCalledWith(
mockOracleAccount,
mockBalance
)
})
})

describe('expire()', () => {
it('collects metrics', async () => {
jest
Expand Down