Skip to content

feat: cache all metrics data for 10 seconds for performances reasons #690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/aleph/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def get_defaults():
"max_concurrency": 20,
},
},
"cache": {
"ttl": {
"total_aleph_messages": 120,
"eth_height": 600,
"metrics": 10,
},
},
},
"p2p": {
# Port used for HTTP communication between nodes.
Expand Down
12 changes: 8 additions & 4 deletions src/aleph/web/controllers/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

LOGGER = getLogger("WEB.metrics")

config = get_config()


def format_dict_for_prometheus(values: Dict) -> str:
"""Format a dict to a Prometheus tags string"""
Expand Down Expand Up @@ -101,8 +103,8 @@ class Metrics(DataClassJsonMixin):
)


# Cache Aleph messages count for 2 minutes
@cached(ttl=120)
# Cache Aleph messages count for 2 minutes by default
@cached(ttl=config.aleph.cache.ttl.total_aleph_messages)
async def fetch_reference_total_messages() -> Optional[int]:
"""Obtain the total number of Aleph messages from another node."""
LOGGER.debug("Fetching Aleph messages count")
Expand All @@ -124,8 +126,8 @@ async def fetch_reference_total_messages() -> Optional[int]:
return None


# Cache ETH height for 10 minutes
@cached(ttl=600)
# Cache ETH height for 10 minutes by default
@cached(ttl=config.aleph.cache.ttl.eth_height)
async def fetch_eth_height() -> Optional[int]:
"""Obtain the height of the Ethereum blockchain."""
LOGGER.debug("Fetching ETH height")
Expand All @@ -143,6 +145,8 @@ async def fetch_eth_height() -> Optional[int]:
return -1 # We got a boggus value!


# Cache metrics for 10 seconds by default
@cached(ttl=config.aleph.cache.ttl.metrics)
async def get_metrics(session: DbSession, node_cache: NodeCache) -> Metrics:
sync_messages_reference_total = await fetch_reference_total_messages()
eth_reference_height = await fetch_eth_height()
Expand Down
Loading