Skip to content

Commit

Permalink
Pass metric as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
hweawer committed Dec 16, 2024
1 parent 4cd8a5e commit 910ae4b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/blockchain/web3_extentions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from typing import Any, Callable, Set, cast
from urllib.parse import urlparse

from metrics.metrics import ETH_RPC_REQUESTS, ETH_RPC_REQUESTS_DURATION, ONCHAIN_TRANSPORT_ETH_RPC_REQUESTS
from metrics.metrics import ETH_RPC_REQUESTS_DURATION
from prometheus_client import Counter
from requests import HTTPError, Response
from web3 import Web3
from web3.middleware import construct_simple_cache_middleware
Expand All @@ -11,15 +12,13 @@
logger = logging.getLogger(__name__)


def add_requests_metric_middleware(web3: Web3) -> Web3:
def add_requests_metric_middleware(web3: Web3, rpc_metric: Counter) -> Web3:
"""
Works correctly with MultiProvider and vanilla Providers.
ETH_RPC_REQUESTS_DURATION - HISTOGRAM with requests time.
ETH_RPC_REQUESTS - Counter with requests count, response codes and request domain.
"""
chain_id = web3.eth.chain_id
metric = ETH_RPC_REQUESTS if chain_id == 1 else ONCHAIN_TRANSPORT_ETH_RPC_REQUESTS

def metrics_collector(make_request: Callable[[RPCEndpoint, Any], RPCResponse], w3: Web3) -> Callable[[RPCEndpoint, Any], RPCResponse]:
"""Constructs a middleware which measure requests parameters"""
Expand All @@ -30,7 +29,7 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
response = make_request(method, params)
except HTTPError as ex:
failed: Response = ex.response
metric.labels(
rpc_metric.labels(
method=method,
code=failed.status_code,
domain=urlparse(web3.provider.endpoint_uri).netloc, # pyright: ignore
Expand All @@ -44,7 +43,7 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
if isinstance(error, dict):
code = error.get('code') or code

metric.labels(
rpc_metric.labels(
method=method,
code=code,
domain=urlparse(web3.provider.endpoint_uri).netloc, # pyright: ignore
Expand Down Expand Up @@ -72,12 +71,12 @@ def add_cache_middleware(web3: Web3) -> Web3:
return web3


def add_middlewares(web3: Web3) -> Web3:
def add_middlewares(web3: Web3, rpc_metric: Counter) -> Web3:
"""
Cache middleware should go first to avoid rewriting metrics for cached requests.
If middleware has level = 0, the middleware will be appended to the end of the middleware list.
So we need [..., cache, other middlewares]
"""
add_cache_middleware(web3)
add_requests_metric_middleware(web3)
add_requests_metric_middleware(web3, rpc_metric)
return web3
3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from bots.unvetter import run_unvetter
from metrics.healthcheck_pulse import start_pulse_server
from metrics.logging import logging
from metrics.metrics import ETH_RPC_REQUESTS
from prometheus_client import start_http_server
from web3_multi_provider import FallbackProvider

Expand Down Expand Up @@ -49,7 +50,7 @@ def main(bot_name: str):
)

logger.info({'msg': 'Add metrics to web3 requests.'})
add_middlewares(w3)
add_middlewares(w3, ETH_RPC_REQUESTS)

if bot_name == BotModule.DEPOSITOR:
run_depositor(w3)
Expand Down
3 changes: 2 additions & 1 deletion src/transport/msg_providers/onchain_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from blockchain.web3_extentions.middleware import add_middlewares
from eth_typing import ChecksumAddress
from eth_utils import to_bytes
from metrics.metrics import ONCHAIN_TRANSPORT_ETH_RPC_REQUESTS
from schema import Schema
from transport.msg_providers.common import BaseMessageProvider
from transport.msg_providers.rabbit import MessageType
Expand Down Expand Up @@ -327,4 +328,4 @@ def _parse_log(self, log: LogReceipt) -> Optional[dict]:
@staticmethod
def create_onchain_transport_w3() -> Web3:
w3 = Web3(FallbackProvider(variables.ONCHAIN_TRANSPORT_RPC_ENDPOINTS))
return add_middlewares(w3)
return add_middlewares(w3, ONCHAIN_TRANSPORT_ETH_RPC_REQUESTS)
3 changes: 2 additions & 1 deletion tests/fixtures/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from blockchain.web3_extentions.lido_contracts import LidoContracts
from blockchain.web3_extentions.middleware import add_middlewares
from blockchain.web3_extentions.transaction import TransactionUtils
from metrics.metrics import ETH_RPC_REQUESTS
from web3 import HTTPProvider, Web3

from tests.fork import anvil_fork
Expand Down Expand Up @@ -35,7 +36,7 @@ def web3_provider_integration(request) -> Web3:

with anvil_fork(anvil_path, rpc_endpoint, block_num):
w3 = Web3(HTTPProvider('http://127.0.0.1:8545', request_kwargs={'timeout': 3600}))
add_middlewares(w3)
add_middlewares(w3, ETH_RPC_REQUESTS)
assert w3.is_connected(), 'Failed to connect to the Web3 provider.'
yield w3

Expand Down

0 comments on commit 910ae4b

Please sign in to comment.