Skip to content

Commit

Permalink
Add StatsD timer to measure E/S indexation (fixes #54)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Sep 6, 2017
1 parent f2e1cc3 commit 4755416
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Changelog
0.3.0 (unreleased)
------------------

- Nothing changed yet.
**New features**

- Add StatsD timer to measure E/S indexation (fixes #54)

0.2.1 (2017-06-14)
------------------
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pytest-capturelog
mock
unittest2
webtest
kinto[postgresql]
kinto[postgresql,monitoring]
11 changes: 10 additions & 1 deletion kinto_elasticsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ def includeme(config):
# Activate end-points.
config.scan("kinto_elasticsearch.views")

config.add_subscriber(listener.on_record_changed, AfterResourceChanged,
on_record_changed_listener = listener.on_record_changed

# If StatsD is enabled, monitor execution time of listener.
if config.registry.statsd:
statsd_client = config.registry.statsd
key = 'plugins.elasticsearch.index'
on_record_changed_listener = statsd_client.timer(key)(on_record_changed_listener)

config.add_subscriber(on_record_changed_listener, AfterResourceChanged,
for_resources=("record",))

config.add_subscriber(listener.on_server_flushed, ServerFlushed)
config.add_subscriber(listener.on_collection_created, AfterResourceChanged,
for_resources=("collection",), for_actions=("create",))
Expand Down
5 changes: 4 additions & 1 deletion tests/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ kinto.userid_hmac_secret = some-secret-string

kinto.includes = kinto_elasticsearch
kinto.plugins.flush

# We need indices to be refreshed immediately for assertions.
kinto.elasticsearch.force_refresh = true
kinto.elasticsearch.force_refresh = true

statsd_url = udp://127.0.0.1:8125
9 changes: 9 additions & 0 deletions tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def test_response_is_served_if_indexer_fails(self):
headers=self.headers)
assert r.status_code == 201

def test_a_statsd_timer_is_used_if_configured(self):
statsd_client = self.app.app.registry.statsd._client
with mock.patch.object(statsd_client, 'timing') as mocked:
self.app.post_json("/buckets/bid/collections/cid/records",
{"data": {"hola": "mundo"}},
headers=self.headers)
timers = set(c[0][0] for c in mocked.call_args_list)
assert 'plugins.elasticsearch.index' in timers


class ParentDeletion(BaseWebTest, unittest.TestCase):

Expand Down

0 comments on commit 4755416

Please sign in to comment.