Skip to content

Commit

Permalink
Rework metric formatting in tests to use more of the exporter code
Browse files Browse the repository at this point in the history
  • Loading branch information
braedon committed Apr 15, 2018
1 parent 7a8f89d commit d86facd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 56 deletions.
2 changes: 1 addition & 1 deletion tests/test_cluster_health_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_endpoint(self):
'indices_shards_unassigned_shards{index="foo",shard="4"}': 1,
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)


if __name__ == '__main__':
Expand Down
20 changes: 10 additions & 10 deletions tests/test_indices_stats_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ def test_endpoint_cluster(self):
'primaries_query_cache_evictions{index="_all"}': 0,
'primaries_fielddata_memory_size_in_bytes{index="_all"}': 0,
'primaries_fielddata_evictions{index="_all"}': 0,
'primaries_fielddata_fields_memory_size_in_bytes{field="group1",index="_all"}': 1024,
'primaries_fielddata_fields_memory_size_in_bytes{field="group2",index="_all"}': 2048,
'primaries_fielddata_fields_memory_size_in_bytes{index="_all",field="group1"}': 1024,
'primaries_fielddata_fields_memory_size_in_bytes{index="_all",field="group2"}': 2048,
'primaries_completion_size_in_bytes{index="_all"}': 0,
'primaries_segments_count{index="_all"}': 3,
'primaries_segments_memory_in_bytes{index="_all"}': 7908,
Expand Down Expand Up @@ -675,8 +675,8 @@ def test_endpoint_cluster(self):
'total_query_cache_evictions{index="_all"}': 0,
'total_fielddata_memory_size_in_bytes{index="_all"}': 0,
'total_fielddata_evictions{index="_all"}': 0,
'total_fielddata_fields_memory_size_in_bytes{field="group1",index="_all"}': 1024,
'total_fielddata_fields_memory_size_in_bytes{field="group2",index="_all"}': 2048,
'total_fielddata_fields_memory_size_in_bytes{index="_all",field="group1"}': 1024,
'total_fielddata_fields_memory_size_in_bytes{index="_all",field="group2"}': 2048,
'total_completion_size_in_bytes{index="_all"}': 0,
'total_segments_count{index="_all"}': 3,
'total_segments_memory_in_bytes{index="_all"}': 7908,
Expand All @@ -701,7 +701,7 @@ def test_endpoint_cluster(self):
'total_recovery_throttle_time_in_millis{index="_all"}': 0,
}
result = convert_result(parse_response(self.response, parse_indices=False))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_endpoint_indices(self):

Expand Down Expand Up @@ -766,8 +766,8 @@ def test_endpoint_indices(self):
'primaries_query_cache_evictions{index="foo"}': 0,
'primaries_fielddata_memory_size_in_bytes{index="foo"}': 0,
'primaries_fielddata_evictions{index="foo"}': 0,
'primaries_fielddata_fields_memory_size_in_bytes{field="group1",index="foo"}': 1024,
'primaries_fielddata_fields_memory_size_in_bytes{field="group2",index="foo"}': 2048,
'primaries_fielddata_fields_memory_size_in_bytes{index="foo",field="group1"}': 1024,
'primaries_fielddata_fields_memory_size_in_bytes{index="foo",field="group2"}': 2048,
'primaries_completion_size_in_bytes{index="foo"}': 0,
'primaries_segments_count{index="foo"}': 3,
'primaries_segments_memory_in_bytes{index="foo"}': 7908,
Expand Down Expand Up @@ -850,8 +850,8 @@ def test_endpoint_indices(self):
'total_query_cache_evictions{index="foo"}': 0,
'total_fielddata_memory_size_in_bytes{index="foo"}': 0,
'total_fielddata_evictions{index="foo"}': 0,
'total_fielddata_fields_memory_size_in_bytes{field="group1",index="foo"}': 1024,
'total_fielddata_fields_memory_size_in_bytes{field="group2",index="foo"}': 2048,
'total_fielddata_fields_memory_size_in_bytes{index="foo",field="group1"}': 1024,
'total_fielddata_fields_memory_size_in_bytes{index="foo",field="group2"}': 2048,
'total_completion_size_in_bytes{index="foo"}': 0,
'total_segments_count{index="foo"}': 3,
'total_segments_memory_in_bytes{index="foo"}': 7908,
Expand All @@ -876,7 +876,7 @@ def test_endpoint_indices(self):
'total_recovery_throttle_time_in_millis{index="foo"}': 0,
}
result = convert_result(parse_response(self.response, parse_indices=True))
self.assertEqual(result, expected)
self.assertEqual(expected, result)


if __name__ == '__main__':
Expand Down
32 changes: 16 additions & 16 deletions tests/test_nodes_stats_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,16 +594,16 @@ def test_endpoint(self):
'jvm_mem_pools_peak_max_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",pool="old"}': 1798569984,
'jvm_threads_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 40,
'jvm_threads_peak_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 46,
'jvm_gc_collectors_collection_count{collector="young",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 2,
'jvm_gc_collectors_collection_time_in_millis{collector="young",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 189,
'jvm_gc_collectors_collection_count{collector="old",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 1,
'jvm_gc_collectors_collection_time_in_millis{collector="old",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 143,
'jvm_buffer_pools_count{buffer_pool="direct",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 29,
'jvm_buffer_pools_used_in_bytes{buffer_pool="direct",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 87069546,
'jvm_buffer_pools_total_capacity_in_bytes{buffer_pool="direct",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 87069545,
'jvm_buffer_pools_count{buffer_pool="mapped",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 3,
'jvm_buffer_pools_used_in_bytes{buffer_pool="mapped",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 9658,
'jvm_buffer_pools_total_capacity_in_bytes{buffer_pool="mapped",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 9658,
'jvm_gc_collectors_collection_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",collector="young"}': 2,
'jvm_gc_collectors_collection_time_in_millis{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",collector="young"}': 189,
'jvm_gc_collectors_collection_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",collector="old"}': 1,
'jvm_gc_collectors_collection_time_in_millis{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",collector="old"}': 143,
'jvm_buffer_pools_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",buffer_pool="direct"}': 29,
'jvm_buffer_pools_used_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",buffer_pool="direct"}': 87069546,
'jvm_buffer_pools_total_capacity_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",buffer_pool="direct"}': 87069545,
'jvm_buffer_pools_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",buffer_pool="mapped"}': 3,
'jvm_buffer_pools_used_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",buffer_pool="mapped"}': 9658,
'jvm_buffer_pools_total_capacity_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",buffer_pool="mapped"}': 9658,
'jvm_classes_current_loaded_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 10236,
'jvm_classes_total_loaded_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 10236,
'jvm_classes_total_unloaded_count{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 0,
Expand Down Expand Up @@ -697,11 +697,11 @@ def test_endpoint(self):
'fs_data_total_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",path="/usr/share/elasticsearch/data/nodes/0"}': 233134567424,
'fs_data_free_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",path="/usr/share/elasticsearch/data/nodes/0"}': 92206276608,
'fs_data_available_in_bytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",path="/usr/share/elasticsearch/data/nodes/0"}': 80292356096,
'fs_io_stats_devices_operations{device_name="dm-0",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 22045,
'fs_io_stats_devices_read_operations{device_name="dm-0",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 14349,
'fs_io_stats_devices_write_operations{device_name="dm-0",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 7696,
'fs_io_stats_devices_read_kilobytes{device_name="dm-0",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 294732,
'fs_io_stats_devices_write_kilobytes{device_name="dm-0",node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 113424,
'fs_io_stats_devices_operations{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",device_name="dm-0"}': 22045,
'fs_io_stats_devices_read_operations{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",device_name="dm-0"}': 14349,
'fs_io_stats_devices_write_operations{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",device_name="dm-0"}': 7696,
'fs_io_stats_devices_read_kilobytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",device_name="dm-0"}': 294732,
'fs_io_stats_devices_write_kilobytes{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z",device_name="dm-0"}': 113424,
'fs_io_stats_total_operations{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 22045,
'fs_io_stats_total_read_operations{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 14349,
'fs_io_stats_total_write_operations{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 7696,
Expand Down Expand Up @@ -741,7 +741,7 @@ def test_endpoint(self):
'ingest_total_failed{node_id="bRcKq5zUTAuwNf4qvnXzIQ",node_name="bRcKq5z"}': 0,
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)


if __name__ == '__main__':
Expand Down
24 changes: 12 additions & 12 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_query(self):
'took_milliseconds': 1
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

# effectively tests other singe-value metrics: max,min,sum,cardinality
def test_avg(self):
Expand Down Expand Up @@ -82,7 +82,7 @@ def test_avg(self):
'val_avg_value': 2
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

# effecively tests other mult-value metrics: percentile_ranks
def test_percentiles(self):
Expand Down Expand Up @@ -138,7 +138,7 @@ def test_percentiles(self):
'val_percentiles_values_99_0': 2.98
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_stats(self):
# Query:
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_stats(self):
'val_stats_sum': 6.0
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_extended_stats(self):
# Query:
Expand Down Expand Up @@ -249,7 +249,7 @@ def test_extended_stats(self):

}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_filter(self):
# Query:
Expand Down Expand Up @@ -299,7 +299,7 @@ def test_filter(self):
'group1_filter_val_sum_value': 3.0
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_filters(self):
# Query:
Expand Down Expand Up @@ -366,7 +366,7 @@ def test_filters(self):
'group_filter_val_sum_value{group_filter="group_b"}': 3.0
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_filters_anonymous(self):
# Query:
Expand Down Expand Up @@ -433,7 +433,7 @@ def test_filters_anonymous(self):
'group_filter_val_sum_value{group_filter="filter_1"}': 3.0
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_terms(self):
# Query:
Expand Down Expand Up @@ -501,7 +501,7 @@ def test_terms(self):
'group1_term_val_sum_value{group1_term="b"}': 3.0
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_terms_numeric(self):
# Query:
Expand Down Expand Up @@ -578,7 +578,7 @@ def test_terms_numeric(self):
'val_terms_val_sum_value{val_terms="3"}': 3.0
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

def test_nested_terms(self):
# Query:
Expand Down Expand Up @@ -697,7 +697,7 @@ def test_nested_terms(self):
'group1_term_group2_term_val_sum_value{group1_term="b",group2_term="b"}': 3.0,
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)

# Tests handling of disallowed characters in labels and metric names
# The '-'s in the aggregation name aren't allowed in metric names or
Expand Down Expand Up @@ -771,7 +771,7 @@ def test_bad_chars(self):
'__group_filter_1_val_sum_value{_group_filter_1="group_b"}': 3.0
}
result = convert_result(parse_response(response))
self.assertEqual(result, expected)
self.assertEqual(expected, result)


if __name__ == '__main__':
Expand Down
40 changes: 23 additions & 17 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
from prometheus_es_exporter import (format_metric_name,
format_label_key,
format_label_value)
from prometheus_es_exporter import group_metrics


def format_label(key, value_list):
return format_label_key(key) + '="' + format_label_value(value_list) + '"'
def format_label(key, value):
return key + '="' + value + '"'


def format_metric(name_list, label_dict):
name = format_metric_name(name_list)
def format_metrics(metric_name, label_keys, value_dict):
metrics = {}

if len(label_dict) > 0:
sorted_keys = sorted(label_dict.keys())
labels = '{'
labels += ','.join([format_label(k, label_dict[k]) for k in sorted_keys])
labels += '}'
else:
labels = ''
for label_values, value in value_dict.items():
if len(label_keys) > 0:
# sorted_keys = sorted(label_keys)
labels = '{'
labels += ','.join([format_label(label_keys[i], label_values[i])
for i in range(len(label_keys))])
labels += '}'
else:
labels = ''

return name + labels
metrics[metric_name + labels] = value

return metrics


# Converts the parse_response() result into a psuedo-prometheus format
# that is useful for comparing results in tests.
# Uses the 'grop_metrics()' function used by the exporter, so effectively
# tests that function.
def convert_result(result):
metric_dict = group_metrics(result)
return {
format_metric(name_list, label_dict): value
for (name_list, label_dict, value) in result
metric: value
for metric_name, (label_keys, value_dict) in metric_dict.items()
for metric, value in format_metrics(metric_name, label_keys, value_dict).items()
}

0 comments on commit d86facd

Please sign in to comment.