|
| 1 | +# Fetch metrics from the built-in Prometheus endpoint of HDFS components. |
| 2 | + |
| 3 | +import logging |
| 4 | +import sys |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | + |
| 9 | +def check_metrics( |
| 10 | + namespace: str, role: str, port: int, expected_metrics: list[str] |
| 11 | +) -> None: |
| 12 | + response: requests.Response = requests.get( |
| 13 | + f"http://test-hbase-{role}-default-0.test-hbase-{role}-default.{namespace}.svc.cluster.local:{port}/prometheus", |
| 14 | + timeout=10, |
| 15 | + ) |
| 16 | + assert response.ok, "Requesting metrics failed" |
| 17 | + |
| 18 | + # Split the response into lines to check for metric names at the beginning of each line. |
| 19 | + # This is a bit slower than using a regex but it allows to use special characters like "{}" in metric names |
| 20 | + # without needing to escape them. |
| 21 | + response_lines = response.text.splitlines() |
| 22 | + for metric in expected_metrics: |
| 23 | + # Use any() with a generator to stop early if the metric is found. |
| 24 | + assert any((line.startswith(metric) for line in response_lines)) is True, ( |
| 25 | + f"Metric '{metric}' not found for {role}" |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def check_master_metrics( |
| 30 | + namespace: str, |
| 31 | +) -> None: |
| 32 | + expected_metrics: list[str] = ["master_queue_size"] |
| 33 | + |
| 34 | + check_metrics(namespace, "master", 16010, expected_metrics) |
| 35 | + |
| 36 | + |
| 37 | +def check_regionserver_metrics( |
| 38 | + namespace: str, |
| 39 | +) -> None: |
| 40 | + expected_metrics: list[str] = [ |
| 41 | + "table_requests_namespace_hbase_table_meta_table_write_query_per_second_count" |
| 42 | + ] |
| 43 | + |
| 44 | + check_metrics(namespace, "regionserver", 16030, expected_metrics) |
| 45 | + |
| 46 | + |
| 47 | +def check_restserver_metrics( |
| 48 | + namespace: str, |
| 49 | +) -> None: |
| 50 | + expected_metrics: list[str] = ["ugi_metrics_get_groups_num_ops"] |
| 51 | + |
| 52 | + check_metrics(namespace, "restserver", 8085, expected_metrics) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + namespace_arg: str = sys.argv[1] |
| 57 | + |
| 58 | + logging.basicConfig( |
| 59 | + level="DEBUG", |
| 60 | + format="%(asctime)s %(levelname)s: %(message)s", |
| 61 | + stream=sys.stdout, |
| 62 | + ) |
| 63 | + |
| 64 | + check_master_metrics(namespace_arg) |
| 65 | + check_regionserver_metrics(namespace_arg) |
| 66 | + check_restserver_metrics(namespace_arg) |
| 67 | + |
| 68 | + print("All expected metrics found") |
0 commit comments