Skip to content

Commit

Permalink
Merge v0.5.1
Browse files Browse the repository at this point in the history
Merge v0.5.1
  • Loading branch information
Kamikaza731 authored Dec 14, 2024
2 parents c078b90 + e30bb45 commit ff1b995
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ docker run -d \
-v $(pwd)/config:/app/config \
-v $(pwd)/chain_database:/app/chain_database \
--name tnom \
tnom:v0.5.0
tnom:v0.5.1
```
# Prometheus metrics
Expand Down
2 changes: 1 addition & 1 deletion tnom/database_handler/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ def write_epoch_data(path: Path, data: dict[str, int]) -> None:
data["small_balance_alert_executed"],
data["very_small_balance_alert_executed"],
data["consecutive_misses"],
data["slash_epoch"],
data["api_cons_miss"],
data["slash_epoch"],
))
conn.commit()

Expand Down
4 changes: 3 additions & 1 deletion tnom/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def setup_argument_parser() -> argparse.ArgumentParser:
parser.add_argument(
"--version",
action="version",
version="v0.5.0",
version="v0.5.1",
)

parser.add_argument(
Expand Down Expand Up @@ -737,6 +737,8 @@ def handle_shutdown() -> None:
prometheus_host,
prometheus_port,
shutdown_event,
database_path,
config_yml["monitoring_interval"],
),
)
tasks.append(prometheus_task)
Expand Down
31 changes: 30 additions & 1 deletion tnom/prometheus_client_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import hypercorn.asyncio
import hypercorn.config
from database_handler import read_current_epoch_data
from database_handler import read_current_epoch_data, read_last_recorded_epoch
from fastapi import FastAPI
from prometheus_client import Counter, Gauge, make_asgi_app

Expand Down Expand Up @@ -163,6 +163,8 @@ async def start_metrics_server(
prometheus_host: str,
prometheus_port: int,
shutdown_event: asyncio.Event,
db_path: Path,
update_interval: int = 60,
) -> None:
"""Start the Prometheus metrics server.
Expand All @@ -177,6 +179,9 @@ async def start_metrics_server(
prometheus_host (str): The hostname to listen on.
shutdown_event (asyncio.Event): An event to notify when the server
should shutdown.
db_path (Path): The path to the database file.
update_interval (int, optional): The interval in seconds to update the
metrics. Defaults to 60.
Returns:
None
Expand All @@ -202,7 +207,28 @@ async def shutdown_trigger() -> None:
"""
await shutdown_event.wait()

async def periodic_metric_update() -> None:
"""Periodically update metrics with the latest epoch."""
while not shutdown_event.is_set():
try:
# Read the latest epoch
latest_epoch = read_last_recorded_epoch(db_path)

# Update the metrics object with the new epoch
metrics.epoch = latest_epoch

# Update the metrics
metrics.update_metrics()

# Wait for the specified interval
await asyncio.sleep(update_interval)
except (OSError, asyncio.TimeoutError) as e: # noqa: PERF203
logging.exception("Error updating metrics: %s", e) # noqa: TRY401
# Wait a bit before retrying to avoid rapid error loops
await asyncio.sleep(10)

try:
update_task = asyncio.create_task(periodic_metric_update())
await hypercorn.asyncio.serve(
app,
config,
Expand All @@ -213,4 +239,7 @@ async def shutdown_trigger() -> None:
# Hypercorn's serve function doesn't automatically clean up
# Make sure to release resources if necessary here.
finally:
# Cancel the update task
update_task.cancel()
logging.info("Prometheus server shutting down.")

0 comments on commit ff1b995

Please sign in to comment.