Skip to content

Commit

Permalink
chore: instrument Brovider calls (#221)
Browse files Browse the repository at this point in the history
* chore: instrument Brovider calls

* feat: instrument brovider full archive node availability

* refactor: refactor

* fix: bump brovider metrics scrapping to 5min

* Update src/lib/metrics/index.ts

Co-authored-by: Chaitanya <[email protected]>

---------

Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
wa0x6e and ChaituVR authored Jan 24, 2024
1 parent 442202a commit d34f08c
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/lib/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import init, { client } from '@snapshot-labs/snapshot-metrics';
import networks from '@snapshot-labs/snapshot.js/src/networks.json';
import snapshot from '@snapshot-labs/snapshot.js';
import { Wallet } from '@ethersproject/wallet';
import { capture } from '@snapshot-labs/snapshot-sentry';
import { size as queueSize } from '../queue';
import getModerationList from '../moderationList';
Expand All @@ -19,6 +22,8 @@ export default function initMetrics(app: Express) {
errorHandler: capture,
db
});

run();
}

new client.Gauge({
Expand Down Expand Up @@ -151,3 +156,77 @@ try {
capture(e);
}
}

const providersResponseCode = new client.Gauge({
name: 'provider_response_code',
help: 'Response code of each provider request',
labelNames: ['network']
});

const providersTiming = new client.Gauge({
name: 'provider_duration_seconds',
help: 'Duration in seconds of each provider request',
labelNames: ['network', 'status']
});

const providersFullArchiveNodeAvailability = new client.Gauge({
name: 'provider_full_archive_node_availability',
help: 'Availability of full archive node for each provider',
labelNames: ['network']
});

const abi = ['function getEthBalance(address addr) view returns (uint256 balance)'];
const wallet = new Wallet(process.env.NFT_CLAIMER_PRIVATE_KEY as string);

function refreshProviderTiming() {
Object.values(networks).forEach(async network => {
const { key, multicall } = network;
const end = providersTiming.startTimer({ network: key });
let status = 0;

try {
const provider = snapshot.utils.getProvider(key);
await snapshot.utils.multicall(
key,
provider,
abi,
[wallet.address].map(adr => [multicall, 'getEthBalance', [adr]]),
{
blockTag: 'latest'
}
);
status = 1;
providersResponseCode.set({ network: key }, 200);
} catch (e: any) {
providersResponseCode.set({ network: key }, parseInt(e?.error?.status || 0));
} finally {
end({ status });
}
});
}

function refreshFullArchiveNodeChecker() {
Object.values(networks).forEach(async network => {
const { key, start, multicall } = network;
try {
const provider = snapshot.utils.getProvider(key);

await provider.getBalance(multicall, start);
providersFullArchiveNodeAvailability.set({ network: key }, 1);
} catch (e: any) {
providersFullArchiveNodeAvailability.set({ network: key }, 0);
}
});
}

async function run() {
try {
refreshProviderTiming();
refreshFullArchiveNodeChecker();
} catch (e) {
capture(e);
} finally {
await snapshot.utils.sleep(5 + 60e3);
run();
}
}

0 comments on commit d34f08c

Please sign in to comment.