Skip to content

Commit

Permalink
chore: add metrics to subgraph provider (Uniswap#552)
Browse files Browse the repository at this point in the history
* chore: add metrics to subgraph provider

* better metrics for subgraph provider

* improve metrics and add v3

* base deviation is 39 now

* optimism variation 61
  • Loading branch information
mikeki authored Apr 26, 2024
1 parent 21744dd commit bebfaf1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/providers/v2/subgraph-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -80,6 +81,7 @@ export class V2SubgraphProvider implements IV2SubgraphProvider {
_tokenOut?: Token,
providerConfig?: ProviderConfig
): Promise<V2SubgraphPool[]> {
const beforeAll = Date.now();
let blockNumber = providerConfig?.blockNumber
? await providerConfig.blockNumber
: undefined;
Expand Down Expand Up @@ -111,6 +113,7 @@ export class V2SubgraphProvider implements IV2SubgraphProvider {
}.`
);

let outerRetries = 0;
await retry(
async () => {
const timeout = new Timeout();
Expand All @@ -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}`
Expand All @@ -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;
};

Expand All @@ -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 },
Expand All @@ -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
Expand All @@ -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 (
Expand All @@ -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`
);
Expand Down
26 changes: 25 additions & 1 deletion src/providers/v3/subgraph-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -107,6 +107,7 @@ export class V3SubgraphProvider implements IV3SubgraphProvider {
_tokenOut?: Token,
providerConfig?: ProviderConfig
): Promise<V3SubgraphPool[]> {
const beforeAll = Date.now();
let blockNumber = providerConfig?.blockNumber
? await providerConfig.blockNumber
: undefined;
Expand Down Expand Up @@ -145,6 +146,8 @@ export class V3SubgraphProvider implements IV3SubgraphProvider {
}.`
);

let retries = 0;

await retry(
async () => {
const timeout = new Timeout();
Expand All @@ -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, {
Expand All @@ -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;
};

Expand All @@ -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 },
Expand All @@ -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) =>
Expand All @@ -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`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit bebfaf1

Please sign in to comment.