Skip to content

Commit

Permalink
Count objects in mappings field counts
Browse files Browse the repository at this point in the history
  • Loading branch information
braedon committed Jun 10, 2022
1 parent b7a5536 commit 9553e3c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Sum by index to calculate the total fields per index. Useful for checking if an
```
sum(es_indices_mappings_field_count) by(index) > 900
```
Note that these counts don't include system fields (ones prefixed with `_`, e.g. `_id`), so may be slightly lower than the field count used by Elasticsearch to check the field limit.

# Installation
The exporter requires Python 3 and Pip 3 to be installed.
Expand Down
6 changes: 6 additions & 0 deletions prometheus_es_exporter/indices_mappings_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def count_object_fields(object_mappings, counts=None):
for field, mapping in object_mappings['properties'].items():
# This field is an object, so count its fields.
if 'properties' in mapping:
field_type = 'object'
if field_type in counts:
counts[field_type] += 1
else:
counts[field_type] = 1

counts = count_object_fields(mapping, counts=counts)

else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='prometheus-es-exporter',
version='0.14.1a1',
version='0.14.1',
description='Elasticsearch query Prometheus exporter',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
28 changes: 22 additions & 6 deletions tests/test_indices_mappings_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Test(unittest.TestCase):
# server populated with the following data (http command = Httpie utility):
# > http -v POST localhost:9200/foo/_doc/1 val:=1 group1=a group2=a
# > http -v POST localhost:9200/foo/_doc/2 val:=2 group1=a group2=b
# > http -v POST localhost:9200/foo/_doc/3 val:=3 group1=b group2=b
# > http -v POST localhost:9200/foo/_doc/3 val:=3 group1=b group2=b sub:='{"sub_val": 4}'
def test_endpoint(self):
# Endpoint: /_mappings?pretty
response = {
Expand All @@ -36,6 +36,13 @@ def test_endpoint(self):
}
}
},
"sub": {
"properties": {
"sub_val": {
"type": "long"
}
}
},
"val": {
"type": "long"
}
Expand All @@ -46,8 +53,9 @@ def test_endpoint(self):

expected = {
'field_count{index="foo",field_type="keyword"}': 2,
'field_count{index="foo",field_type="long"}': 1,
'field_count{index="foo",field_type="text"}': 2
'field_count{index="foo",field_type="long"}': 2,
'field_count{index="foo",field_type="text"}': 2,
'field_count{index="foo",field_type="object"}': 1
}
result = convert_result(parse_response(response))
self.assertEqual(expected, result)
Expand Down Expand Up @@ -75,7 +83,7 @@ def test_endpoint_no_properties(self):
# server populated with the following data (http command = Httpie utility):
# > http -v POST localhost:9200/foo/bar/1 val:=1 group1=a group2=a
# > http -v POST localhost:9200/foo/bar/2 val:=2 group1=a group2=b
# > http -v POST localhost:9200/foo/bar/3 val:=3 group1=b group2=b
# > http -v POST localhost:9200/foo/bar/3 val:=3 group1=b group2=b sub:='{"sub_val": 4}'
def test_old_endpoint(self):
# Endpoint: /_mappings?pretty
response = {
Expand All @@ -101,6 +109,13 @@ def test_old_endpoint(self):
}
}
},
"sub": {
"properties": {
"sub_val": {
"type": "long"
}
}
},
"val": {
"type": "long"
}
Expand All @@ -112,8 +127,9 @@ def test_old_endpoint(self):

expected = {
'field_count{index="foo",field_type="keyword"}': 2,
'field_count{index="foo",field_type="long"}': 1,
'field_count{index="foo",field_type="text"}': 2
'field_count{index="foo",field_type="long"}': 2,
'field_count{index="foo",field_type="text"}': 2,
'field_count{index="foo",field_type="object"}': 1
}
result = convert_result(parse_response(response))
self.assertEqual(expected, result)
Expand Down

0 comments on commit 9553e3c

Please sign in to comment.