Skip to content

Commit

Permalink
Patch to replace syntax that's incompatible with Python 3.4
Browse files Browse the repository at this point in the history
Unpacking a dictionary inside a dictionary display was only introduced in
PEP 448 in Python 3.5
  • Loading branch information
braedon committed Jun 22, 2017
1 parent 3c2f31e commit 56c4809
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 3 additions & 1 deletion prometheus_es_exporter/cluster_health_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .utils import merge_dicts

singular_forms = {
'indices': 'index',
'shards': 'shard'
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions prometheus_es_exporter/indices_stats_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .utils import merge_dicts

singular_forms = {
'fields': 'field'
}
Expand All @@ -24,15 +26,15 @@ 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:
bucket_name_key = bucket_list_keys[key]

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

Expand Down
8 changes: 5 additions & 3 deletions prometheus_es_exporter/nodes_stats_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .utils import merge_dicts

singular_forms = {
'pools': 'pool',
'collectors': 'collector',
Expand Down Expand Up @@ -35,21 +37,21 @@ 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:
bucket_name_key = bucket_list_keys[key]

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)

Expand Down
15 changes: 15 additions & 0 deletions prometheus_es_exporter/utils.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 56c4809

Please sign in to comment.