Skip to content

Commit

Permalink
Merge pull request #940 from Aiven-Open/nosahama/shutdown-metric
Browse files Browse the repository at this point in the history
rapu: fire shutdown metric on app shutdown
  • Loading branch information
eliax1996 committed Aug 26, 2024
2 parents 36c7858 + 8e4271d commit dff48ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions karapace/rapu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_rapu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 <======="

0 comments on commit dff48ce

Please sign in to comment.