Skip to content

Commit

Permalink
refactor: metrics module
Browse files Browse the repository at this point in the history
  • Loading branch information
mfw78 committed Jan 23, 2024
1 parent 279f720 commit 9fd3f66
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 47 deletions.
3 changes: 1 addition & 2 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { RunOptions } from "../types";
import { getLogger, DBService } from "../utils";
import { getLogger, DBService, ApiService } from "../utils";
import { ChainContext } from "../domain";
import { ApiService } from "../utils/api";

/**
* Run the watch-tower 👀🐮
Expand Down
11 changes: 0 additions & 11 deletions src/domain/addContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ import {

import { ChainContext } from "./chainContext";
import { ConditionalOrderParams } from "@cowprotocol/cow-sdk";
import {
addContractRunsTotal,
addContractsErrorsTotal,
addContractsRunDurationSeconds,
measureTime,
merkleRootTotal,
ownersTotal,
singleOrdersTotal,
activeOrdersTotal,
activeOwnersTotal,
} from "../utils/metrics";

/**
* Listens to these events on the `ComposableCoW` contract:
Expand Down
29 changes: 14 additions & 15 deletions src/domain/chainContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,8 @@ import {
DBService,
getLogger,
isRunningInKubernetesPod,
metrics,
} from "../utils";
import {
blockHeight,
blockProducingRate,
eventsProcessedTotal,
processBlockDurationSeconds,
reorgDepth,
reorgsTotal,
} from "../utils/metrics";
import { hexZeroPad } from "ethers/lib/utils";
import { FilterPolicy } from "../utils/filterPolicy";

Expand Down Expand Up @@ -183,7 +176,9 @@ export class ChainContext {
const { pageSize } = this;

// Set the block height metric
blockHeight.labels(chainId.toString()).set(lastProcessedBlock?.number ?? 0);
metrics.blockHeight
.labels(chainId.toString())
.set(lastProcessedBlock?.number ?? 0);

// Start watching from (not including) the last processed block (if any)
let fromBlock = lastProcessedBlock
Expand Down Expand Up @@ -269,7 +264,9 @@ export class ChainContext {
await this.registry.write();

// Set the block height metric
blockHeight.labels(chainId.toString()).set(Number(blockNumber));
metrics.blockHeight
.labels(chainId.toString())
.set(Number(blockNumber));
} catch (err) {
log.error(`Error processing block ${blockNumber}`, err);
}
Expand Down Expand Up @@ -334,16 +331,16 @@ export class ChainContext {

// Set the block time metric
const _blockTime = block.timestamp - lastBlockReceived.timestamp;
blockProducingRate.labels(chainId.toString()).set(_blockTime);
metrics.blockProducingRate.labels(chainId.toString()).set(_blockTime);

if (
blockNumber <= lastBlockReceived.number &&
block.hash !== lastBlockReceived.hash
) {
// This is a re-org, so process the block again
reorgsTotal.labels(chainId.toString()).inc();
metrics.reorgsTotal.labels(chainId.toString()).inc();
log.info(`Re-org detected, re-processing block ${blockNumber}`);
reorgDepth
metrics.reorgDepth
.labels(chainId.toString())
.set(lastBlockReceived.number - blockNumber + 1);
}
Expand All @@ -361,7 +358,9 @@ export class ChainContext {
// Block height metric
this.registry.lastProcessedBlock = blockToRegistryBlock(block);
this.registry.write();
blockHeight.labels(chainId.toString()).set(Number(blockNumber));
metrics.blockHeight
.labels(chainId.toString())
.set(Number(blockNumber));
} catch {
log.error(`Error processing block ${blockNumber}`);
}
Expand Down Expand Up @@ -457,7 +456,7 @@ async function processBlock(
blockTimestampOverride?: number
) {
const { provider, chainId } = context;
const timer = processBlockDurationSeconds
const timer = metrics.processBlockDurationSeconds
.labels(context.chainId.toString())
.startTimer();
const log = getLogger(
Expand Down
12 changes: 0 additions & 12 deletions src/domain/checkForAndPlaceOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ import {
formatEpoch,
} from "@cowprotocol/cow-sdk";
import { ChainContext, SDK_BACKOFF_NUM_OF_ATTEMPTS } from "./chainContext";
import {
pollingOnChainDurationSeconds,
activeOrdersTotal,
activeOwnersTotal,
orderBookDiscreteOrdersTotal,
orderBookErrorsTotal,
pollingOnChainChecksTotal,
pollingRunsTotal,
pollingUnexpectedErrorsTotal,
pollingOnChainEthersErrorsTotal,
measureTime,
} from "../utils/metrics";
import { FilterAction } from "../utils/filterPolicy";
import { validateOrder } from "../utils/filterOrder";

Expand Down
7 changes: 3 additions & 4 deletions src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { BytesLike, ethers } from "ethers";

import type { ConditionalOrderCreatedEvent } from "./generated/ComposableCoW";
import { ConditionalOrderParams, PollResult } from "@cowprotocol/cow-sdk";
import { DBService } from "../utils";
import { activeOrdersTotal, activeOwnersTotal } from "../utils/metrics";
import { DBService, metrics } from "../utils";

// Standardise the storage key
const LAST_NOTIFIED_ERROR_STORAGE_KEY = "LAST_NOTIFIED_ERROR";
Expand Down Expand Up @@ -167,12 +166,12 @@ export class Registry {
.catch(() => null);

// Return registry (on its latest version)
activeOwnersTotal.labels(network).set(ownerOrders.size);
metrics.activeOwnersTotal.labels(network).set(ownerOrders.size);
const numOrders = Array.from(ownerOrders.values()).reduce(
(acc, o) => acc + o.size,
0
);
activeOrdersTotal.labels(network).set(numOrders);
metrics.activeOrdersTotal.labels(network).set(numOrders);

return new Registry(
ownerOrders,
Expand Down
5 changes: 2 additions & 3 deletions src/utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SupportedChainId,
} from "@cowprotocol/cow-sdk";
import { getLogger } from "./logging";
import { pollingOnChainInvalidInterfacesTotal } from "./metrics";
import { metrics } from ".";

// Selectors that are required to be part of the contract's bytecode in order to be considered compatible
const REQUIRED_SELECTORS = [
Expand Down Expand Up @@ -293,7 +293,6 @@ export function handleOnChainCustomError(params: {
`Order for safe ${owner} where the Safe has swap guard enabled. Deleting order...`
);
return dropOrder("The conditional order didn't pass the swap guard");
// TODO: Add metrics to track this
case CustomErrorSelectors.ORDER_NOT_VALID:
const reason = msgWithSelector(parsedCustomError.message);
log.info(
Expand Down Expand Up @@ -333,7 +332,7 @@ export function handleOnChainCustomError(params: {
log.debug(
`Contract returned a non-compliant interface revert via getTradeableOrderWithSignature. Simulate: https://dashboard.tenderly.co/gp-v2/watch-tower-prod/simulator/new?network=${chainId}&contractAddress=${target}&rawFunctionInput=${callData}`
);
pollingOnChainInvalidInterfacesTotal.labels(...metricLabels).inc();
metrics.pollingOnChainInvalidInterfacesTotal.labels(...metricLabels).inc();
return {
result: PollResultCode.DONT_TRY_AGAIN,
reason: "Order returned a non-compliant (invalid/erroneous) revert hint",
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from "./poll";
export * from "./misc";
export * from "./db";
export * from "./logging";
export * from "./api";
export * as metrics from "./metrics";

0 comments on commit 9fd3f66

Please sign in to comment.