diff --git a/prometheus_es_exporter/cluster_health_parser.py b/prometheus_es_exporter/cluster_health_parser.py index 3f40fed..3e7167a 100644 --- a/prometheus_es_exporter/cluster_health_parser.py +++ b/prometheus_es_exporter/cluster_health_parser.py @@ -1,3 +1,5 @@ +from .utils import merge_dicts + singular_forms = { 'indices': 'index', 'shards': 'shard' @@ -31,7 +33,7 @@ def parse_block(block, metric=[], labels={}): else: singular_key = key for n_key, n_value in value.items(): - result.extend(parse_block(n_value, metric=metric + [key], labels={**labels, singular_key: [n_key]})) + result.extend(parse_block(n_value, metric=metric + [key], labels=merge_dicts(labels, {singular_key: [n_key]}))) return result diff --git a/prometheus_es_exporter/indices_stats_parser.py b/prometheus_es_exporter/indices_stats_parser.py index 7c753d0..8772b9e 100644 --- a/prometheus_es_exporter/indices_stats_parser.py +++ b/prometheus_es_exporter/indices_stats_parser.py @@ -1,3 +1,5 @@ +from .utils import merge_dicts + singular_forms = { 'fields': 'field' } @@ -24,7 +26,7 @@ def parse_block(block, metric=[], labels={}): else: singular_key = key for n_key, n_value in value.items(): - result.extend(parse_block(n_value, metric=metric + [key], labels={**labels, singular_key: [n_key]})) + result.extend(parse_block(n_value, metric=metric + [key], labels=merge_dicts(labels, {singular_key: [n_key]}))) else: result.extend(parse_block(value, metric=metric + [key], labels=labels)) elif isinstance(value, list) and key in bucket_list_keys: @@ -32,7 +34,7 @@ def parse_block(block, metric=[], labels={}): for n_value in value: bucket_name = n_value[bucket_name_key] - result.extend(parse_block(n_value, metric=metric + [key], labels={**labels, bucket_name_key: [bucket_name]})) + result.extend(parse_block(n_value, metric=metric + [key], labels=merge_dicts(labels, {bucket_name_key: [bucket_name]}))) return result diff --git a/prometheus_es_exporter/nodes_stats_parser.py b/prometheus_es_exporter/nodes_stats_parser.py index 964e3ca..753efae 100644 --- a/prometheus_es_exporter/nodes_stats_parser.py +++ b/prometheus_es_exporter/nodes_stats_parser.py @@ -1,3 +1,5 @@ +from .utils import merge_dicts + singular_forms = { 'pools': 'pool', 'collectors': 'collector', @@ -35,7 +37,7 @@ def parse_block(block, metric=[], labels={}): else: singular_key = key for n_key, n_value in value.items(): - result.extend(parse_block(n_value, metric=metric + [key], labels={**labels, singular_key: [n_key]})) + result.extend(parse_block(n_value, metric=metric + [key], labels=merge_dicts(labels, {singular_key: [n_key]}))) else: result.extend(parse_block(value, metric=metric + [key], labels=labels)) elif isinstance(value, list) and key in bucket_list_keys: @@ -43,13 +45,13 @@ def parse_block(block, metric=[], labels={}): for n_value in value: bucket_name = n_value[bucket_name_key] - result.extend(parse_block(n_value, metric=metric + [key], labels={**labels, bucket_name_key: [bucket_name]})) + result.extend(parse_block(n_value, metric=metric + [key], labels=merge_dicts(labels, {bucket_name_key: [bucket_name]}))) return result def parse_node(node, metric=[], labels={}): - labels = {**labels, 'node_name': [node['name']]} + labels = merge_dicts(labels, node_name=[node['name']]) return parse_block(node, metric=metric, labels=labels) diff --git a/prometheus_es_exporter/utils.py b/prometheus_es_exporter/utils.py new file mode 100644 index 0000000..3067d83 --- /dev/null +++ b/prometheus_es_exporter/utils.py @@ -0,0 +1,15 @@ +def merge_dicts(*dict_args, **extra_entries): + """ + Given an arbitrary number of dictionaries, merge them into a + single new dictionary. Later dictionaries take precedence if + a key is shared by multiple dictionaries. + + Extra entries can also be provided via kwargs. These entries + have the highest precedence. + """ + res = {} + + for d in dict_args + (extra_entries,): + res.update(d) + + return res diff --git a/setup.py b/setup.py index 6245dc1..3203274 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='prometheus-es-exporter', - version='0.4.1', + version='0.4.2', description='Elasticsearch query Prometheus exporter', url='https://github.com/Braedon/prometheus-es-exporter', author='Braedon Vickers',