Skip to content
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

add cardano governor metrics from prometheus endpoint to mqtt payload #29

Merged
merged 2 commits into from
Oct 17, 2024
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
24 changes: 24 additions & 0 deletions src/blockperf/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
from datetime import datetime, timedelta, timezone
from pathlib import Path
from urllib.request import urlopen

from blockperf import __version__ as blockperf_version
from blockperf.blocksample import BlockSample, slot_time_of
Expand Down Expand Up @@ -101,6 +102,19 @@ def print_block_stats(self, blocksample: BlockSample) -> None:
)
logger.info("\n" + msg)

def prometheus_metrics(self, endpoint) -> dict:
"""Retrieves metrics from given endpoint and returns as dictionary"""
metrics = None
try:
with urlopen(endpoint) as response:
raw_metrics = response.read().decode()
metrics = {
k[0]: k[1] for k in [m.split(" ") for m in raw_metrics.splitlines()]
}
except Exception as e:
logger.error("error retrieving metrics from prometheus: %s", e)
return metrics

def mqtt_payload_from(self, sample: BlockSample) -> dict:
"""Returns a dictionary for use as payload when publishing the sample."""
payload = {
Expand All @@ -122,6 +136,16 @@ def mqtt_payload_from(self, sample: BlockSample) -> dict:
"blockLocalPort": str(self.app_config.relay_public_port),
"blockG": str(sample.block_g),
}

if endpoint := self.app_config.prometheus_endpoint:
metrics = self.prometheus_metrics(endpoint)
if metrics:
payload["inboundGovernor_hot"] = int(
metrics.get("cardano_node_metrics_inboundGovernor_hot")
)
payload["inboundGovernor_warm"] = int(
metrics.get("cardano_node_metrics_inboundGovernor_warm")
)
return payload

def ensure_maxblocks(self):
Expand Down
12 changes: 12 additions & 0 deletions src/blockperf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,15 @@ def masked_addresses(self) -> list:
f"Given address {addr} is not a valid ip address"
) from exc
return validated_addresses

@property
def prometheus_endpoint(self) -> [str, None]:
has_prometheus = self.node_config.get("hasPrometheus")
if (
not has_prometheus
or not isinstance(has_prometheus, list)
or not len(has_prometheus) == 2
or not isinstance(has_prometheus[1], int)
):
return None
return f"http://127.0.0.1:{has_prometheus[1]}/metrics"