diff --git a/src/baseframe/statsd.py b/src/baseframe/statsd.py index b1fd9e72..232625b2 100644 --- a/src/baseframe/statsd.py +++ b/src/baseframe/statsd.py @@ -224,7 +224,8 @@ def set( # noqa: A003 def pipeline(self) -> Pipeline: return current_app.extensions['statsd_core'].pipeline() - def _request_started(self, app: Flask) -> None: + @staticmethod + def _request_started(app: Flask) -> None: if app.config['STATSD_RATE'] != 0: setattr(g, REQUEST_START_TIME_ATTR, time.time()) @@ -254,13 +255,16 @@ def _request_finished(self, app: Flask, response: Response) -> None: # we use both: timer (via `timing`) and counter (via `incr`). self.incr(metric_name, tags=tags) - def _before_render_template(self, app: Flask, template: Template, **kwargs) -> None: + @staticmethod + def _before_render_template(app: Flask, template: Template, **kwargs) -> None: + """Record start time when rendering a template.""" if app.config['STATSD_RATE'] != 0: if not hasattr(g, TEMPLATE_START_TIME_ATTR): setattr(g, TEMPLATE_START_TIME_ATTR, {}) getattr(g, TEMPLATE_START_TIME_ATTR)[template] = time.time() def _template_rendered(self, app: Flask, template: Template, **kwargs) -> None: + """Calculate time to render a template and log to statsd.""" start_time = getattr(g, TEMPLATE_START_TIME_ATTR, {}).get(template) if not start_time: current_app.logger.warning( diff --git a/tests/baseframe_tests/statsd_test.py b/tests/baseframe_tests/statsd_test.py index 6060204a..d04c7b6e 100644 --- a/tests/baseframe_tests/statsd_test.py +++ b/tests/baseframe_tests/statsd_test.py @@ -238,103 +238,105 @@ def test_tags(app, ctx, statsd) -> None: def test_request_handler_notags(app, statsd, view) -> None: - with patch('statsd.StatsClient.incr') as mock_incr: - with patch('statsd.StatsClient.timing') as mock_timing: - with app.test_client() as client: - client.get('/') - # First call - mock_incr.assert_any_call( - 'flask_app.baseframe_tests.statsd_test.request_handlers' - '.endpoint_index.status_code_200', - 1, - rate=1, - ) - # Second and last call - mock_incr.assert_called_with( - 'flask_app.baseframe_tests.statsd_test.request_handlers' - '.endpoint__overall.status_code_200', - 1, - rate=1, - ) - mock_timing.assert_called() + """Test request_handlers logging with tags disabled.""" + with patch('statsd.StatsClient.incr') as mock_incr, patch( + 'statsd.StatsClient.timing' + ) as mock_timing, app.test_client() as client: + client.get('/') + # First call + mock_incr.assert_any_call( + 'flask_app.baseframe_tests.statsd_test.request_handlers' + '.endpoint_index.status_code_200', + 1, + rate=1, + ) + # Second and last call + mock_incr.assert_called_with( + 'flask_app.baseframe_tests.statsd_test.request_handlers' + '.endpoint__overall.status_code_200', + 1, + rate=1, + ) + mock_timing.assert_called() def test_request_handler_tags(app, statsd, view) -> None: + """Test request_handlers logging with tags enabled.""" app.config['STATSD_TAGS'] = ',' - with patch('statsd.StatsClient.incr') as mock_incr: - with patch('statsd.StatsClient.timing') as mock_timing: - with app.test_client() as client: - client.get('/') - mock_incr.assert_called_once_with( - 'flask_app.request_handlers,endpoint=index,status_code=200' - ',app=baseframe_tests.statsd_test', - 1, - rate=1, - ) - mock_timing.assert_called_once() + with patch('statsd.StatsClient.incr') as mock_incr, patch( + 'statsd.StatsClient.timing' + ) as mock_timing, app.test_client() as client: + client.get('/') + mock_incr.assert_called_once_with( + 'flask_app.request_handlers,endpoint=index,status_code=200' + ',app=baseframe_tests.statsd_test', + 1, + rate=1, + ) + mock_timing.assert_called_once() def test_request_handler_disabled(app, view) -> None: + """Test request_handlers logging disabled.""" app.config['STATSD_REQUEST_LOG'] = False Statsd(app) - with patch('statsd.StatsClient.incr') as mock_incr: - with patch('statsd.StatsClient.timing') as mock_timing: - with app.test_client() as client: - client.get('/') - mock_incr.assert_not_called() - mock_timing.assert_not_called() + with patch('statsd.StatsClient.incr') as mock_incr, patch( + 'statsd.StatsClient.timing' + ) as mock_timing, app.test_client() as client: + client.get('/') + mock_incr.assert_not_called() + mock_timing.assert_not_called() def test_render_template_notags(app, statsd) -> None: - with patch('statsd.StatsClient.incr') as mock_incr: - with patch('statsd.StatsClient.timing') as mock_timing: - with app.app_context(): - render_template_string("Test template") - assert mock_incr.call_count == 2 - assert mock_timing.call_count == 2 - assert [c.args[0] for c in mock_incr.call_args_list] == [ - 'flask_app.baseframe_tests.statsd_test.render_template' - '.template__str', - 'flask_app.baseframe_tests.statsd_test.render_template' - '.template__overall', - ] - assert [c.args[0] for c in mock_incr.call_args_list] == [ - 'flask_app.baseframe_tests.statsd_test.render_template' - '.template__str', - 'flask_app.baseframe_tests.statsd_test.render_template' - '.template__overall', - ] + """Test render_template logging with tags disabled.""" + with patch('statsd.StatsClient.incr') as mock_incr, patch( + 'statsd.StatsClient.timing' + ) as mock_timing, app.app_context(): + render_template_string("Test template") + assert mock_incr.call_count == 2 + assert mock_timing.call_count == 2 + assert [c.args[0] for c in mock_incr.call_args_list] == [ + 'flask_app.baseframe_tests.statsd_test.render_template.template__str', + 'flask_app.baseframe_tests.statsd_test.render_template' + '.template__overall', + ] + assert [c.args[0] for c in mock_incr.call_args_list] == [ + 'flask_app.baseframe_tests.statsd_test.render_template.template__str', + 'flask_app.baseframe_tests.statsd_test.render_template' + '.template__overall', + ] def test_render_template_tags(app, statsd) -> None: + """Test render_template logging with tags enabled.""" app.config['STATSD_TAGS'] = ',' - with patch('statsd.StatsClient.incr') as mock_incr: - with patch('statsd.StatsClient.timing') as mock_timing: - with app.app_context(): - render_template_string("Test template") - assert mock_incr.call_count == 1 - assert mock_timing.call_count == 1 - assert ( - mock_incr.call_args.args[0] - == 'flask_app.render_template,template=_str,' - 'app=baseframe_tests.statsd_test' - ) - assert ( - mock_incr.call_args.args[0] - == 'flask_app.render_template,template=_str,' - 'app=baseframe_tests.statsd_test' - ) + with patch('statsd.StatsClient.incr') as mock_incr, patch( + 'statsd.StatsClient.timing' + ) as mock_timing, app.app_context(): + render_template_string("Test template") + assert mock_incr.call_count == 1 + assert mock_timing.call_count == 1 + assert ( + mock_incr.call_args.args[0] == 'flask_app.render_template,template=_str,' + 'app=baseframe_tests.statsd_test' + ) + assert ( + mock_incr.call_args.args[0] == 'flask_app.render_template,template=_str,' + 'app=baseframe_tests.statsd_test' + ) def test_render_template_disabled(app, view) -> None: + """Test render_template logging disabled.""" app.config['STATSD_RENDERTEMPLATE_LOG'] = False Statsd(app) - with patch('statsd.StatsClient.incr') as mock_incr: - with patch('statsd.StatsClient.timing') as mock_timing: - with app.app_context(): - render_template_string("Test template") - mock_incr.assert_not_called() - mock_timing.assert_not_called() + with patch('statsd.StatsClient.incr') as mock_incr, patch( + 'statsd.StatsClient.timing' + ) as mock_timing, app.app_context(): + render_template_string("Test template") + mock_incr.assert_not_called() + mock_timing.assert_not_called() def test_form_success(ctx, app, statsd, form) -> None: