Skip to content

Commit

Permalink
Rename config.registry.statsd to config.registry.metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Oct 7, 2024
1 parent 51b2ef0 commit ee9aba8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
6 changes: 3 additions & 3 deletions kinto/core/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,10 @@ def setup_listeners(config):
listener = listener_mod.load_from_config(config, prefix)

# If StatsD is enabled, monitor execution time of listeners.
if getattr(config.registry, "statsd", None):
statsd_client = config.registry.statsd
if getattr(config.registry, "metrics", None):
metrics = config.registry.metrics
key = f"listeners.{name}"
listener = statsd_client.timer(key)(listener.__call__)
listener = metrics.timer(key)(listener.__call__)

# Optional filter by event action.
actions_setting = prefix + "actions"
Expand Down
6 changes: 3 additions & 3 deletions kinto/plugins/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def includeme(config):
# Activate end-points.
config.scan("kinto.plugins.history.views")

# If StatsD is enabled, monitor execution time of listener.
# If metrics are enabled, monitor execution time of listener.
listener = on_resource_changed
if config.registry.statsd:
if config.registry.metrics:
key = "plugins.history"
listener = config.registry.statsd.timer(key)(on_resource_changed)
listener = config.registry.metrics.timer(key)(on_resource_changed)

# Listen to every resources (except history)
config.add_subscriber(
Expand Down
6 changes: 3 additions & 3 deletions kinto/plugins/quotas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def includeme(config):
url="https://kinto.readthedocs.io",
)

# If StatsD is enabled, monitor execution time of listener.
# If metrics are enabled, monitor execution time of listener.
listener = on_resource_changed
if config.registry.statsd:
if config.registry.metrics:
key = "plugins.quotas"
listener = config.registry.statsd.timer(key)(on_resource_changed)
listener = config.registry.metrics.timer(key)(on_resource_changed)

# Listen to every resources (except history)
config.add_subscriber(
Expand Down
13 changes: 11 additions & 2 deletions kinto/plugins/statsd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,27 @@ def load_from_config(config):
return Client(uri.hostname, uri.port, prefix)


def deprecated_registry(self):
warnings.warn(
"``config.registry.statsd`` is now deprecated. Check release notes.",
DeprecationWarning,
)
return self.metrics


def includeme(config):
settings = config.get_settings()
config.registry.statsd = None
config.registry.metrics = None

if not settings["statsd_url"]:
return

statsd_mod = settings["statsd_backend"]
statsd_mod = config.maybe_dotted(statsd_mod)
client = statsd_mod.load_from_config(config)
config.registry.metrics = client

config.registry.statsd = client
config.registry.__class__.statsd = property(deprecated_registry)

client.watch_execution_time(config.registry.cache, prefix="backend")
client.watch_execution_time(config.registry.storage, prefix="backend")
Expand Down
5 changes: 3 additions & 2 deletions tests/core/resource/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,9 @@ def get_app_settings(cls, *args, **kwargs):
return settings

def test_statds_tracks_listeners_execution_duration(self):
statsd_client = self.app.app.registry.statsd._client
with mock.patch.object(statsd_client, "timing") as mocked:
# This test may break when introducing a generic interface for Prometheus.
metrics_client = self.app.app.registry.metrics._client
with mock.patch.object(metrics_client, "timing") as mocked:
self.app.post_json(self.plural_url, {"data": {"name": "pouet"}}, headers=self.headers)
timers = set(c[0][0] for c in mocked.call_args_list)
self.assertIn("listeners.test", timers)
33 changes: 21 additions & 12 deletions tests/plugins/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,45 @@ def test_statsd_isnt_called_if_statsd_url_is_not_set(self):
def test_statsd_is_set_to_none_if_statsd_url_not_set(self):
self.config.add_settings({"statsd_url": None})
self.config.include("kinto.plugins.statsd")
self.assertEqual(self.config.registry.statsd, None)
self.assertEqual(self.config.registry.metrics, None)

def test_statsd_is_called_if_statsd_url_is_set(self):
# For some reasons, when using ``self.config.include("kinto.plugins.statsd")``
# the config object is recreated breaks ``assert_called_with(self.config)``.
# the config object is recreated and breaks ``assert_called_with(self.config)``.
statsd.includeme(self.config)
self.mocked.assert_called_with(self.config)

def test_statsd_is_expose_in_the_registry_if_url_is_set(self):
def test_metrics_attr_is_exposed_in_the_registry_if_url_is_set(self):
self.config.include("kinto.plugins.statsd")
self.assertEqual(self.config.registry.statsd, self.mocked.return_value)
self.assertEqual(self.config.registry.metrics, self.mocked.return_value)

def test_statsd_attr_is_exposed_in_the_registry_if_url_is_set(self):
self.config.include("kinto.plugins.statsd")
with mock.patch("warnings.warn") as mocked_warnings:
self.config.registry.statsd.count("key")
mocked_warnings.assert_called_with(
"``config.registry.statsd`` is now deprecated. Check release notes.",
DeprecationWarning,
)

def test_statsd_is_set_on_cache(self):
self.config.include("kinto.plugins.statsd")
c = self.config.registry.statsd
c = self.config.registry.metrics
c.watch_execution_time.assert_any_call({}, prefix="backend")

def test_statsd_is_set_on_storage(self):
self.config.include("kinto.plugins.statsd")
c = self.config.registry.statsd
c = self.config.registry.metrics
c.watch_execution_time.assert_any_call({}, prefix="backend")

def test_statsd_is_set_on_permission(self):
self.config.include("kinto.plugins.statsd")
c = self.config.registry.statsd
c = self.config.registry.metrics
c.watch_execution_time.assert_any_call({}, prefix="backend")

def test_statsd_is_set_on_authentication(self):
self.config.include("kinto.plugins.statsd")
c = self.config.registry.statsd
c = self.config.registry.metrics
c.watch_execution_time.assert_any_call(None, prefix="authentication")

def test_statsd_counts_nothing_on_anonymous_requests(self):
Expand Down Expand Up @@ -179,12 +188,12 @@ def test_load_from_config_uses_project_name_if_defined(self, module_mock):

def test_statsd_count_handle_unconfigured_statsd_client(self):
request = mock.MagicMock()
request.registry.statsd = None
request.registry.metrics = None
statsd.statsd_count(request, "toto") # Doesn't raise

def test_statsd_count_call_the_client_if_configured(self):
request = mock.MagicMock()
request.registry.statsd = self.mocked_client
request.registry.metrics = self.mocked_client
statsd.statsd_count(request, "toto")
self.mocked_client.count.assert_called_with("toto")

Expand All @@ -201,13 +210,13 @@ def get_app_settings(cls, *args, **kwargs):
return settings

def test_statds_tracks_listeners_execution_duration(self):
statsd_client = self.app.app.registry.statsd._client
statsd_client = self.app.app.registry.metrics._client
with mock.patch.object(statsd_client, "timing") as mocked:
self.app.get("/", headers=self.headers)
self.assertTrue(mocked.called)

def test_statds_tracks_authentication_policies(self):
statsd_client = self.app.app.registry.statsd._client
statsd_client = self.app.app.registry.metrics._client
with mock.patch.object(statsd_client, "timing") as mocked:
self.app.get("/", headers=self.headers)
timers = set(c[0][0] for c in mocked.call_args_list)
Expand Down

0 comments on commit ee9aba8

Please sign in to comment.