Skip to content

Commit

Permalink
cloud_function: convert to prom metrics
Browse files Browse the repository at this point in the history
Signed-off-by: bingyuyap <bingyu.yap.21@gmail.com>
  • Loading branch information
bingyuyap committed Apr 24, 2024
1 parent a1298a9 commit 207e10a
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 148 deletions.
130 changes: 130 additions & 0 deletions cloud_functions/src/getNTTRateLimits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import {
assertEnvironmentVariable,
NTT_MANAGER_CONTRACT,
NTT_TOKENS,
NTT_TRANSCEIVER_CONTRACT,
NTT_SUPPORTED_CHAINS,
getEvmTokenDecimals,
getSolanaTokenDecimals,
} from '@wormhole-foundation/wormhole-monitor-common';
import { EvmPlatform, EvmChains } from '@wormhole-foundation/sdk-evm';
import { SolanaPlatform } from '@wormhole-foundation/sdk-solana';
import { EvmNtt } from '@wormhole-foundation/sdk-evm-ntt';
import { SolanaNtt } from '@wormhole-foundation/sdk-solana-ntt';
import { Network, Chain, contracts, rpc } from '@wormhole-foundation/sdk-base';
import { Gauge, register } from 'prom-client';

const outboundCapacityGauge = new Gauge({
name: 'ntt_outbound_capacity',
help: 'NTT outbound capacity for token on chain',
labelNames: ['token', 'chain', 'network', 'product'],
});

const inboundCapacityGauge = new Gauge({
name: 'ntt_inbound_capacity',
help: 'NTT inbound capacity for token on chain from inbound_chain',
labelNames: ['token', 'chain', 'inbound_chain', 'network', 'product'],
});

const PRODUCT = 'cloud_functions_ntt';

async function setCapacityGauge(
gauge: Gauge,
labels: Record<string, string | number>,
capacity: bigint
) {
gauge.set(labels, Number(capacity));
}

async function getRateLimits(network: Network, token: string, chain: Chain) {
let ntt: EvmNtt<Network, EvmChains> | SolanaNtt<Network, 'Solana'>;
let tokenDecimals: number;
const rpcEndpoint = rpc.rpcAddress(network, chain as Chain);
const tokenAddress = NTT_TOKENS[network][token][chain]!;
const managerContract = NTT_MANAGER_CONTRACT[network][token][chain]!;
const transceiverContract = NTT_TRANSCEIVER_CONTRACT[network][token][chain]!;

if (chain === 'Solana') {
const platform = new SolanaPlatform(network);
ntt = new SolanaNtt(network, chain, platform.getRpc(chain), {
coreBridge: contracts.coreBridge(network, chain),
ntt: {
token: tokenAddress,
manager: managerContract,
transceiver: {
wormhole: transceiverContract,
},
},
});
tokenDecimals = await getSolanaTokenDecimals(rpcEndpoint, tokenAddress);
} else {
const evmChain = chain as EvmChains;
const platform = new EvmPlatform(network);
ntt = new EvmNtt(network, evmChain, platform.getRpc(evmChain), {
ntt: {
token: tokenAddress,
manager: managerContract,
transceiver: {
wormhole: transceiverContract,
},
},
});
tokenDecimals = await getEvmTokenDecimals(rpcEndpoint, managerContract);
}

const outboundCapacity = await ntt.getCurrentOutboundCapacity();
const normalizedOutboundCapacity = outboundCapacity / BigInt(10 ** tokenDecimals);

await setCapacityGauge(
outboundCapacityGauge,
{ token, chain, network, product: PRODUCT },
normalizedOutboundCapacity
);

const inboundChains = NTT_SUPPORTED_CHAINS(network, token).filter(
(inboundChain) => inboundChain !== chain
);
await Promise.all(
inboundChains.map(async (inboundChain) => {
const inboundCapacity = await ntt.getCurrentInboundCapacity(inboundChain);
const normalizedInboundCapacity = inboundCapacity / BigInt(10 ** tokenDecimals);
await setCapacityGauge(
inboundCapacityGauge,
{ token, chain, inbound_chain: inboundChain, network, product: PRODUCT },
normalizedInboundCapacity
);
})
);
}

export async function getNTTRateLimits(req: any, res: any) {
res.set('Access-Control-Allow-Origin', '*');
if (req.method === 'OPTIONS') {
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.sendStatus(204);
return;
}

try {
const network = assertEnvironmentVariable('NETWORK') as Network;
const managerContracts = NTT_MANAGER_CONTRACT[network];

const rateLimitPromises = Object.entries(managerContracts).flatMap(([token, manager]) =>
Object.entries(manager)
.map(([chain, contract]) =>
contract ? getRateLimits(network, token, chain as Chain) : null
)
.filter(Boolean)
);

await Promise.all(rateLimitPromises);

res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (e) {
console.error(e);
res.sendStatus(500);
}
}
4 changes: 2 additions & 2 deletions cloud_functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const { getReobserveVaas } = require('./getReobserveVaas');
export const { wormchainMonitor } = require('./wormchainMonitor');
export const { getLatestTokenData } = require('./getLatestTokenData');
export const { getSolanaEvents } = require('./getSolanaEvents');
export const { pushNTTRateLimits } = require('./pushNTTRateLimits');
export const { getNTTRateLimits } = require('./getNTTRateLimits');

// Register an HTTP function with the Functions Framework that will be executed
// when you make an HTTP request to the deployed function's endpoint.
Expand All @@ -52,4 +52,4 @@ functions.http('getReobserveVaas', getReobserveVaas);
functions.http('wormchainMonitor', wormchainMonitor);
functions.http('latestTokenData', getLatestTokenData);
functions.http('getSolanaEvents', getSolanaEvents);
functions.http('pushNTTRateLimits', pushNTTRateLimits);
functions.http('getNTTRateLimits', getNTTRateLimits);
137 changes: 0 additions & 137 deletions cloud_functions/src/pushNttRateLimits.ts

This file was deleted.

5 changes: 1 addition & 4 deletions dashboard/src/utils/evmNttHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
callContractMethod,
getMethodId,
} from '@wormhole-foundation/wormhole-monitor-common';
import { callContractMethod, getMethodId } from '@wormhole-foundation/wormhole-monitor-common';

export async function getCurrentOutboundCapacity(
rpc: string,
Expand Down
7 changes: 2 additions & 5 deletions dashboard/src/utils/nttHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import {
NTT_MANAGER_CONTRACT,
NTT_TOKENS,
NTT_SUPPORTED_CHAINS,
getEvmTokenDecimals,
} from '@wormhole-foundation/wormhole-monitor-common';

import {
getCurrentInboundCapacity,
getCurrentOutboundCapacity,
getTokenDecimals,
} from './evmNttHelpers';
import { getCurrentInboundCapacity, getCurrentOutboundCapacity } from './evmNttHelpers';

import {
getTokenDecimals as getSolTokenDecimals,
Expand Down

0 comments on commit 207e10a

Please sign in to comment.