diff --git a/src/providers/v2/subgraph-provider.ts b/src/providers/v2/subgraph-provider.ts index 6a171d377..47251de7d 100644 --- a/src/providers/v2/subgraph-provider.ts +++ b/src/providers/v2/subgraph-provider.ts @@ -5,6 +5,7 @@ import { gql, GraphQLClient } from 'graphql-request'; import _ from 'lodash'; import { log } from '../../util/log'; +import { metric } from '../../util/metric'; import { ProviderConfig } from '../provider'; export interface V2SubgraphPool { @@ -80,6 +81,7 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { _tokenOut?: Token, providerConfig?: ProviderConfig ): Promise { + const beforeAll = Date.now(); let blockNumber = providerConfig?.blockNumber ? await providerConfig.blockNumber : undefined; @@ -111,6 +113,7 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { }.` ); + let outerRetries = 0; await retry( async () => { const timeout = new Timeout(); @@ -120,25 +123,42 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { let pairs: RawV2SubgraphPool[] = []; let pairsPage: RawV2SubgraphPool[] = []; + // metrics variables + let totalPages = 0; + let retries = 0; + do { + totalPages += 1; + await retry( async () => { + const before = Date.now(); const poolsResult = await this.client.request<{ pairs: RawV2SubgraphPool[]; }>(query2, { pageSize: this.pageSize, id: lastId, }); + metric.putMetric( + `V2SubgraphProvider.chain_${this.chainId}.getPools.paginate.latency`, + Date.now() - before + ); pairsPage = poolsResult.pairs; pairs = pairs.concat(pairsPage); lastId = pairs[pairs.length - 1]!.id; + + metric.putMetric( + `V2SubgraphProvider.chain_${this.chainId}.getPools.paginate.pageSize`, + pairsPage.length + ); }, { retries: this.retries, onRetry: (err, retry) => { pools = []; + retries += 1; log.info( { err }, `Failed request for page of pools from subgraph. Retry attempt: ${retry}` @@ -148,6 +168,10 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { ); } while (pairsPage.length > 0); + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.paginate`, totalPages); + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.pairs.length`, pairs.length); + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.paginate.retries`, retries); + return pairs; }; @@ -171,16 +195,19 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { { retries: this.retries, onRetry: (err, retry) => { + outerRetries += 1; if ( this.rollback && blockNumber && _.includes(err.message, 'indexed up to') ) { + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.indexError`, 1); blockNumber = blockNumber - 10; log.info( `Detected subgraph indexing error. Rolled back block number to: ${blockNumber}` ); } + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.timeout`, 1); pools = []; log.info( { err }, @@ -190,6 +217,8 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { } ); + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.retries`, outerRetries); + // Filter pools that have tracked reserve ETH less than threshold. // trackedReserveETH filters pools that do not involve a pool from this allowlist: // https://github.com/Uniswap/v2-subgraph/blob/7c82235cad7aee4cfce8ea82f0030af3d224833e/src/mappings/pricing.ts#L43 @@ -198,6 +227,7 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { // TODO: Remove. Temporary fix to ensure tokens without trackedReserveETH are in the list. const FEI = '0x956f47f50a910163d8bf957cf5846d573e7f87ca'; + const beforeFilter = Date.now(); const poolsSanitized: V2SubgraphPool[] = pools .filter((pool) => { return ( @@ -222,6 +252,10 @@ export class V2SubgraphProvider implements IV2SubgraphProvider { }; }); + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.filter.latency`, Date.now() - beforeFilter); + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools`, 1); + metric.putMetric(`V2SubgraphProvider.chain_${this.chainId}.getPools.latency`, Date.now() - beforeAll); + log.info( `Got ${pools.length} V2 pools from the subgraph. ${poolsSanitized.length} after filtering` ); diff --git a/src/providers/v3/subgraph-provider.ts b/src/providers/v3/subgraph-provider.ts index e5e08d7bf..63645f28c 100644 --- a/src/providers/v3/subgraph-provider.ts +++ b/src/providers/v3/subgraph-provider.ts @@ -4,7 +4,7 @@ import Timeout from 'await-timeout'; import { gql, GraphQLClient } from 'graphql-request'; import _ from 'lodash'; -import { log } from '../../util'; +import { log, metric } from '../../util'; import { ProviderConfig } from '../provider'; import { V2SubgraphPool } from '../v2/subgraph-provider'; @@ -107,6 +107,7 @@ export class V3SubgraphProvider implements IV3SubgraphProvider { _tokenOut?: Token, providerConfig?: ProviderConfig ): Promise { + const beforeAll = Date.now(); let blockNumber = providerConfig?.blockNumber ? await providerConfig.blockNumber : undefined; @@ -145,6 +146,8 @@ export class V3SubgraphProvider implements IV3SubgraphProvider { }.` ); + let retries = 0; + await retry( async () => { const timeout = new Timeout(); @@ -154,7 +157,12 @@ export class V3SubgraphProvider implements IV3SubgraphProvider { let pools: RawV3SubgraphPool[] = []; let poolsPage: RawV3SubgraphPool[] = []; + // metrics variables + let totalPages = 0; + do { + totalPages += 1; + const poolsResult = await this.client.request<{ pools: RawV3SubgraphPool[]; }>(query, { @@ -167,8 +175,13 @@ export class V3SubgraphProvider implements IV3SubgraphProvider { pools = pools.concat(poolsPage); lastId = pools[pools.length - 1]!.id; + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.paginate.pageSize`, poolsPage.length); + } while (poolsPage.length > 0); + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.paginate`, totalPages); + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.pools.length`, pools.length); + return pools; }; @@ -192,16 +205,19 @@ export class V3SubgraphProvider implements IV3SubgraphProvider { { retries: this.retries, onRetry: (err, retry) => { + retries += 1; if ( this.rollback && blockNumber && _.includes(err.message, 'indexed up to') ) { + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.indexError`, 1); blockNumber = blockNumber - 10; log.info( `Detected subgraph indexing error. Rolled back block number to: ${blockNumber}` ); } + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.timeout`, 1); pools = []; log.info( { err }, @@ -211,6 +227,9 @@ export class V3SubgraphProvider implements IV3SubgraphProvider { } ); + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.retries`, retries); + + const beforeFilter = Date.now(); const poolsSanitized = pools .filter( (pool) => @@ -234,6 +253,11 @@ export class V3SubgraphProvider implements IV3SubgraphProvider { }; }); + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.filter.latency`, Date.now() - beforeFilter); + + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools`, 1); + metric.putMetric(`V3SubgraphProvider.chain_${this.chainId}.getPools.latency`, Date.now() - beforeAll); + log.info( `Got ${pools.length} V3 pools from the subgraph. ${poolsSanitized.length} after filtering` ); diff --git a/test/integ/routers/alpha-router/alpha-router.integration.test.ts b/test/integ/routers/alpha-router/alpha-router.integration.test.ts index 19cad6619..842edffa3 100644 --- a/test/integ/routers/alpha-router/alpha-router.integration.test.ts +++ b/test/integ/routers/alpha-router/alpha-router.integration.test.ts @@ -120,7 +120,7 @@ const GAS_ESTIMATE_DEVIATION_PERCENT: { [chainId in ChainId]: number } = { [ChainId.MAINNET]: 40, [ChainId.GOERLI]: 62, [ChainId.SEPOLIA]: 50, - [ChainId.OPTIMISM]: 35, + [ChainId.OPTIMISM]: 61, [ChainId.OPTIMISM_GOERLI]: 30, [ChainId.OPTIMISM_SEPOLIA]: 30, [ChainId.ARBITRUM_ONE]: 53, @@ -134,7 +134,7 @@ const GAS_ESTIMATE_DEVIATION_PERCENT: { [chainId in ChainId]: number } = { [ChainId.MOONBEAM]: 30, [ChainId.BNB]: 44, [ChainId.AVALANCHE]: 36, - [ChainId.BASE]: 34, + [ChainId.BASE]: 39, [ChainId.BASE_GOERLI]: 30, [ChainId.ZORA]: 30, [ChainId.ZORA_SEPOLIA]: 30,