From 8e4271dbf70ae530d2d7f69a937b47e38907a50d Mon Sep 17 00:00:00 2001 From: Emmanuel Evbuomwan Date: Mon, 26 Aug 2024 15:41:18 +0200 Subject: [PATCH] rapu: fire shutdown metric on app shutdown - we add the `karapace_shutdown_count` metric, to the base app and fire the metric on app shutdown - the `close_by_app()` is added to the app shutdown hooks and thus is called on app shutdown --- karapace/rapu.py | 3 +++ tests/unit/test_rapu.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/karapace/rapu.py b/karapace/rapu.py index bfccebfe9..c4e4c5ee3 100644 --- a/karapace/rapu.py +++ b/karapace/rapu.py @@ -164,6 +164,7 @@ def __init__( self.app_name = app_name self.config = config self.app_request_metric = f"{app_name}_request" + self.app_shutdown_count_metric = f"{app_name}_shutdown_count" self.app = self._create_aiohttp_application(config=config) self.log = logging.getLogger(self.app_name) self.stats = StatsClient(config=config) @@ -183,6 +184,8 @@ async def close(self) -> None: set as hook because the awaitables have to run inside the event loop created by the aiohttp library. """ + self.log.warning("=======> Received shutdown signal, closing Application <=======") + self.stats.increase(self.app_shutdown_count_metric) self.stats.close() @staticmethod diff --git a/tests/unit/test_rapu.py b/tests/unit/test_rapu.py index 304af2603..cde68e2be 100644 --- a/tests/unit/test_rapu.py +++ b/tests/unit/test_rapu.py @@ -2,13 +2,16 @@ Copyright (c) 2023 Aiven Ltd See LICENSE for details """ +from _pytest.logging import LogCaptureFixture from aiohttp.client_exceptions import ClientConnectionError from aiohttp.web import Request from karapace.config import DEFAULTS from karapace.karapace import KarapaceBase from karapace.rapu import HTTPRequest, REST_ACCEPT_RE, REST_CONTENT_TYPE_RE +from karapace.statsd import StatsClient from unittest.mock import Mock +import logging import pytest @@ -180,3 +183,18 @@ async def test_raise_connection_error_handling(connection_error: BaseException) assert response.status == 503 request_mock.read.assert_has_calls([]) callback_mock.assert_not_called() + + +async def test_close_by_app(caplog: LogCaptureFixture) -> None: + app = KarapaceBase(config=DEFAULTS) + app.stats = Mock(spec=StatsClient) + + with caplog.at_level(logging.WARNING, logger="karapace.rapu"): + await app.close_by_app(app=app.app) + + app.stats.increase.assert_called_once_with("karapace_shutdown_count") + app.stats.close.assert_called_once() + for log in caplog.records: + assert log.name == "karapace" + assert log.levelname == "WARNING" + assert log.message == "=======> Received shutdown signal, closing Application <======="