Michal Kobus | a16d07e | 2019-04-11 14:25:27 +0200 | [diff] [blame] | 1 | from prometheus_es_exporter import group_metrics |
| 2 | |
| 3 | |
| 4 | def format_label(key, value): |
| 5 | return key + '="' + value + '"' |
| 6 | |
| 7 | |
| 8 | def format_metrics(metric_name, label_keys, value_dict): |
| 9 | metrics = {} |
| 10 | |
| 11 | for label_values, value in value_dict.items(): |
| 12 | if len(label_keys) > 0: |
| 13 | # sorted_keys = sorted(label_keys) |
| 14 | labels = '{' |
| 15 | labels += ','.join([format_label(label_keys[i], label_values[i]) |
| 16 | for i in range(len(label_keys))]) |
| 17 | labels += '}' |
| 18 | else: |
| 19 | labels = '' |
| 20 | |
| 21 | metrics[metric_name + labels] = value |
| 22 | |
| 23 | return metrics |
| 24 | |
| 25 | |
| 26 | # Converts the parse_response() result into a psuedo-prometheus format |
| 27 | # that is useful for comparing results in tests. |
| 28 | # Uses the 'group_metrics()' function used by the exporter, so effectively |
| 29 | # tests that function. |
| 30 | def convert_result(result): |
| 31 | metric_dict = group_metrics(result) |
| 32 | return { |
| 33 | metric: value |
| 34 | for metric_name, (label_keys, value_dict) in metric_dict.items() |
| 35 | for metric, value in format_metrics(metric_name, label_keys, value_dict).items() |
| 36 | } |