Skip to content

Commit

Permalink
Add one more endpoint with metrics per miner, extend console output
Browse files Browse the repository at this point in the history
  • Loading branch information
voron committed Aug 8, 2024
1 parent 7b3cb41 commit 16058bc
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions exporter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,44 @@
from websockets import connect
from aiohttp import web
from prometheus_async import aio
from prometheus_client import Histogram, Gauge
from prometheus_client import Histogram, Gauge, CollectorRegistry


async def server_stats(request: web.Request) -> web.Response:
"""
It's a copy from prometheus_async, using custom registry
Return a web response with the plain text version of the metrics.
:rtype: :class:`aiohttp.web.Response`
"""
generate, content_type = aio.web._choose_generator(request.headers.get("Accept"))

rsp = web.Response(body=generate(registry))
# This is set separately because aiohttp complains about `;` in
# content_type thinking it means there's also a charset.
# cf. https://github.com/aio-libs/aiohttp/issues/2197
rsp.content_type = content_type

return rsp


def process_block(block):
timestamp = int(block['timestamp'], 16)
block_number = int(block['number'], 16)
ts = time.time()
lag = ts - timestamp
miner = block['miner']
gasUsed = int(block['gasUsed'], 16)
gasLimit = int(block['gasLimit'], 16)
gasUsedPct = float(gasUsed * 100 / gasLimit)
if lag < max_block_lag:
hist.observe(lag)
hist_miner.labels(miner=miner).observe(lag)
gauge.set("{:+.4f}".format(lag))
print("ts=%d block=%d lag=%2.4f" % (timestamp, block_number, lag), flush=True)
# print(block, flush=True)
print(
"ts=%d block=%d lag=%2.4f miner=%s gasUsed=%2.1f%% (%d/%d)" % (timestamp, block_number, lag, miner, gasUsedPct,
gasUsed, gasLimit), flush=True)
return


Expand Down Expand Up @@ -77,8 +103,12 @@ async def health(self):
"HIST_BUCKETS", "0.05,0.08,0.1,0.15,0.2,0.3,0.4,0.6,0.8,1.0,1.2,1.6,2.0,2.5,3.0,4.0,8.0,+Inf")
max_block_lag = float(os.environ.get("MAX_BLOCK_LAG", 60.0))

registry = CollectorRegistry(auto_describe=True)

hist = Histogram('head_lag_seconds', 'Last block lag',
buckets=buckets.split(','))
hist_miner = Histogram('head_lag_miners', 'Last block lag per miner',
buckets=buckets.split(','), labelnames=["miner"], registry=registry)
gauge = Gauge('head_lag_seconds_last', 'Last block lag')

loop = asyncio.new_event_loop()
Expand All @@ -89,6 +119,7 @@ async def health(self):
app.cleanup_ctx.append(background_tasks)
app.on_shutdown.append(on_shutdown)
app.router.add_get("/metrics", aio.web.server_stats)
app.router.add_get("/metrics/miner", server_stats)
app.router.add_get("/health", health)

web.run_app(app, port=metrics_port, loop=loop, handle_signals=True)

0 comments on commit 16058bc

Please sign in to comment.