11# Fetch metrics from the built-in Prometheus endpoint of HDFS components.
22
33import logging
4- import re
54import sys
65
76import requests
@@ -16,8 +15,13 @@ def check_metrics(
1615 )
1716 assert response .ok , "Requesting metrics failed"
1817
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 ()
1922 for metric in expected_metrics :
20- assert re .search (f"^{ metric } " , response .text , re .MULTILINE ) is not None , (
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 , (
2125 f"Metric '{ metric } ' not found for { role } "
2226 )
2327
@@ -30,9 +34,9 @@ def check_namenode_metrics(
3034 # Kind "MetricsSystem"
3135 'metrics_system_num_active_sources{context="metricssystem",hostname="hdfs-namenode-default-0"}' ,
3236 # Counter suffixed with "_total"
33- # The metric attributes can change so use .* for them.
37+ # The metric attributes can change so we remove them from the expected metric .
3438 # The full name looks like: 'fs_namesystem_files_total{context="dfs",enabledecpolicies="RS-6-3-1024k",hastate="active",totalsynctimes="4 7 ",hostname="hdfs-namenode-default-0"}',
35- "fs_namesystem_files_total.* " ,
39+ "fs_namesystem_files_total" ,
3640 # Metric suffixed with "_created"
3741 'namenode_files_created{processname="NameNode",sessionid="null",context="dfs",hostname="hdfs-namenode-default-0"}' ,
3842 # Boolean metric
@@ -53,17 +57,17 @@ def check_datanode_metrics(
5357 'metrics_system_num_active_sources{context="metricssystem",hostname="hdfs-datanode-default-0"}' ,
5458 # Kind "FSDatasetState" suffixed with "_total"
5559 # 'org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_estimated_capacity_lost_total{context="FSDatasetState",storageinfo="FSDataset{dirpath=\'[/stackable/data/hdd/datanode,/stackable/data/hdd-1/datanode, /stackable/data/ssd/datanode]\'}",hostname="hdfs-datanode-default-0"}',
56- "org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_estimated_capacity_lost_total.* " ,
60+ "org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_estimated_capacity_lost_total" ,
5761 # Kind "FSDatasetState"
5862 # 'org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_capacity{context="FSDatasetState",storageinfo="FSDataset{dirpath=\'[/stackable/data/hdd/datanode, /stackable/data/hdd-1/datanode, /stackable/data/ssd/datanode]\'}",hostname="hdfs-datanode-default-0"}',
59- "org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_capacity.* " ,
63+ "org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_capacity" ,
6064 # Kind "DataNodeActivity" suffixed with "_info"
6165 'datanode_blocks_get_local_path_info{sessionid="null",context="dfs",hostname="hdfs-datanode-default-0"}' ,
6266 # Kind "DataNodeActivity"
6367 'datanode_blocks_read{sessionid="null",context="dfs",hostname="hdfs-datanode-default-0"}' ,
6468 # Counter suffixed with "_total"
6569 # 'org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_estimated_capacity_lost_total{context="FSDatasetState",storageinfo="FSDataset{dirpath=\'[/stackable/data/hdd/datanode,/stackable/data/hdd-1/datanode, /stackable/data/ssd/datanode]\'}",hostname="hdfs-datanode-default-0"}',
66- "org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_estimated_capacity_lost_total.* " ,
70+ "org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_estimated_capacity_lost_total" ,
6771 # Boolean metric
6872 #'hadoop_datanode_security_enabled{kind="DataNodeInfo",role="DataNode",service="HDFS"}',
6973 # Non-special metric
0 commit comments