diff --git a/keystone_api/apps/health/tests/views/test_HealthCheckPrometheusView.py b/keystone_api/apps/health/tests/views/test_HealthCheckPrometheusView.py index e0c03614..0ea0bd7b 100644 --- a/keystone_api/apps/health/tests/views/test_HealthCheckPrometheusView.py +++ b/keystone_api/apps/health/tests/views/test_HealthCheckPrometheusView.py @@ -17,10 +17,15 @@ def test_return_matches_health_checks(self) -> None: 'plugin2': create_mock_plugin(0, 'Error', False) } - expected_response = '\n'.join([ - 'plugin1{critical_service="True",message="OK"} 200.0', + expected_response = ( + '# HELP plugin1 unittest.mockMagicMock\n' + '# TYPE plugin1 gauge\n' + 'plugin1{critical_service="True",message="OK"} 200.0\n' + '\n' + '# HELP plugin2 unittest.mockMagicMock\n' + '# TYPE plugin2 gauge\n' 'plugin2{critical_service="False",message="Error"} 500.0' - ]) + ) response = HealthCheckPrometheusView.render_response(health_checks) self.assertEqual(response.status_code, 200) diff --git a/keystone_api/apps/health/views.py b/keystone_api/apps/health/views.py index 50d83436..0f79fdeb 100644 --- a/keystone_api/apps/health/views.py +++ b/keystone_api/apps/health/views.py @@ -110,16 +110,23 @@ def render_response(plugins: dict) -> HttpResponse: An HTTP response """ + prom_format = ( + '# HELP {name} {module}\n' + '# TYPE {name} gauge\n' + '{name}{{critical_service="{critical_service}",message="{message}"}} {status:.1f}' + ) + status_data = [ - '{name}{{critical_service="{critical_service}",message="{message}"}} {status:.1f}'.format( + prom_format.format( name=plugin_name, critical_service=plugin.critical_service, message=plugin.pretty_status(), - status=200 if plugin.status else 500 + status=200 if plugin.status else 500, + module=plugin.__class__.__module__ + plugin.__class__.__name__ ) for plugin_name, plugin in plugins.items() ] - return HttpResponse('\n'.join(status_data), status=200, content_type="text/plain") + return HttpResponse('\n\n'.join(status_data), status=200, content_type="text/plain") @extend_schema(responses={ '200': inline_serializer('health_prom_ok', fields=dict()),