metrics fixes

This commit is contained in:
tim
2025-02-24 22:15:05 -04:00
parent 04d7686c30
commit 8b541bd76d
3 changed files with 21 additions and 12 deletions

View File

@@ -14,7 +14,7 @@ from dexorder.contract import get_contract_event
from dexorder.contract.dexorder import get_dexorder_contract from dexorder.contract.dexorder import get_dexorder_contract
from dexorder.event_handler import (init, dump_log, handle_vault_created, handle_order_placed, from dexorder.event_handler import (init, dump_log, handle_vault_created, handle_order_placed,
handle_transfer, handle_swap_filled, handle_order_canceled, handle_order_cancel_all, handle_transfer, handle_swap_filled, handle_order_canceled, handle_order_cancel_all,
handle_uniswap_swaps, handle_vault_impl_changed, initialize_metrics) handle_uniswap_swaps, handle_vault_impl_changed, update_metrics)
from dexorder.memcache import memcache from dexorder.memcache import memcache
from dexorder.memcache.memcache_state import RedisState, publish_all from dexorder.memcache.memcache_state import RedisState, publish_all
from dexorder.order.executionhandler import handle_dexorderexecutions, execute_tranches from dexorder.order.executionhandler import handle_dexorderexecutions, execute_tranches
@@ -81,6 +81,8 @@ def setup_logevent_triggers(runner):
# runner.add_event_trigger(handle_fees_changed, get_contract_event('IFeeManager', 'FeesChanged')) # runner.add_event_trigger(handle_fees_changed, get_contract_event('IFeeManager', 'FeesChanged'))
# runner.add_callback(adjust_gas) # runner.add_callback(adjust_gas)
runner.add_callback(update_metrics)
# noinspection DuplicatedCode # noinspection DuplicatedCode
async def main(): async def main():
@@ -114,7 +116,6 @@ async def main():
await redis_state.save(state.root_fork, state.diffs_by_branch[state.root_branch.id]) await redis_state.save(state.root_fork, state.diffs_by_branch[state.root_branch.id])
await initialize_accounting() await initialize_accounting()
initialize_metrics()
runner = BlockStateRunner(state, publish_all=publish_all if redis_state else None) runner = BlockStateRunner(state, publish_all=publish_all if redis_state else None)
setup_logevent_triggers(runner) setup_logevent_triggers(runner)

View File

@@ -3,7 +3,7 @@ import logging
from web3.types import EventData from web3.types import EventData
from dexorder import db, metric, current_w3 from dexorder import db, metric, current_w3, timestamp
from dexorder.accounting import accounting_fill, accounting_placement, accounting_transfer, is_tracked_address, \ from dexorder.accounting import accounting_fill, accounting_placement, accounting_transfer, is_tracked_address, \
accounting_lock accounting_lock
from dexorder.base.chain import current_chain from dexorder.base.chain import current_chain
@@ -214,13 +214,21 @@ async def handle_vault_impl_changed(upgrade: EventData):
log.debug(f'Vault {addr} upgraded to impl version {version}') log.debug(f'Vault {addr} upgraded to impl version {version}')
async def get_gas_price(): slow_metric_update = 0
return await current_w3.get().eth.gas_price async def update_metrics():
# called at the end of the runloop in the worker context
metric.vaults.set(vault_owners.upper_len())
metric.open_orders.set(Order.open_orders.upper_len())
metric.triggers_time.set(len(TimeTrigger.all))
metric.triggers_line.set(len(PriceLineTrigger.triggers_set))
def initialize_metrics(): # slow updates
metric.vaults.set_function(vault_owners.upper_len) global slow_metric_update
metric.open_orders.set_function(Order.open_orders.upper_len) now = timestamp()
metric.triggers_time.set_function(lambda: len(TimeTrigger.all)) if now - slow_metric_update >= 60:
metric.triggers_line.set_function(lambda: len(PriceLineTrigger.triggers_set)) slow_metric_update = now
metric.gas_price.set_function(get_gas_price)
# put slow updates here
price = await current_w3.get().eth.gas_price
metric.gas_price.observe(price)

View File

@@ -22,4 +22,4 @@ volume = Counter("volume", "Total volume of successful executions in USD")
account_total = Gauge('account_total', 'Total number of accounts configured') account_total = Gauge('account_total', 'Total number of accounts configured')
account_available = Gauge('account_available', 'Number of accounts that do not have any pending transactions') account_available = Gauge('account_available', 'Number of accounts that do not have any pending transactions')
gas_price = Gauge('gas_price', 'Gas price in wei') gas_price = Summary('gas_price', 'Gas price in wei')