Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds HELP and TYPE to prometheus metrics #311

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions keystone_api/apps/health/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
Loading