Skip to content

Commit

Permalink
Observe request duration
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Oct 29, 2024
1 parent 367dfa3 commit 5b2a9d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kinto/core/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,18 @@ def on_new_response(event):
],
)

try:
current = utils.msec_time()
duration = current - request._received_at
metrics_service.observe(
"request_duration",
duration,
labels=[("endpoint", utils.strip_uri_prefix(request.path))],
)
except AttributeError: # pragma: no cover
# Logging was not setup in this Kinto app (unlikely but possible)
pass

# Observe response size.
metrics_service.observe(
"request_size",
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ def test_statsd_observe_request_size(self):
labels=[("endpoint", "/__heartbeat__")],
)

def test_statsd_observe_request_duration(self):
kinto.core.initialize(self.config, "0.0.1", "settings_prefix")
app = webtest.TestApp(self.config.make_wsgi_app())
app.get("/v0/__heartbeat__")
self.mocked().observe.assert_any_call(
"request_duration",
mock.ANY,
labels=[("endpoint", "/__heartbeat__")],
)

def test_statsd_counts_views_and_methods(self):
kinto.core.initialize(self.config, "0.0.1", "settings_prefix")
app = webtest.TestApp(self.config.make_wsgi_app())
Expand Down

0 comments on commit 5b2a9d1

Please sign in to comment.