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

feat: add prometheus metrics to /metrics #466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tgalske
Copy link

@tgalske tgalske commented Oct 13, 2024

Summary

This PR adds the Prometheus Python client as a dependency, and serves up the out-of-the-box metrics on the /metrics endpoint.

The metrics mounting code was taken almost directly from Prometheus' documentation on how to add metrics to a FastAPI application: https://prometheus.github.io/client_python/exporting/http/fastapi-gunicorn/

Since this would be my first contribution and in an effort to keep the change on the small side, this PR does not expose any custom metrics.

Testing

  • make docker-test, make check pass
  • Manually verified the metrics by running make docker-start-api and observing the metrics at http://localhost:8000/metrics
Example output
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 10468.0
python_gc_objects_collected_total{generation="1"} 4124.0
python_gc_objects_collected_total{generation="2"} 2134.0
# HELP python_gc_objects_uncollectable_total Uncollectable objects found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 952.0
python_gc_collections_total{generation="1"} 86.0
python_gc_collections_total{generation="2"} 6.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="11",patchlevel="10",version="3.11.10"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 4.717744128e+09
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 8.1219584e+08
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.72882768797e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 9.059999999999999
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 20.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1.048576e+06

@tgalske tgalske force-pushed the tgalske/prometheus-metrics branch from de53ea0 to b43fef0 Compare October 14, 2024 12:17
@tgalske tgalske marked this pull request as ready for review October 14, 2024 13:46
@@ -31,6 +32,9 @@
openapi_tags=[{"name": "general"}],
)

metrics_app = make_asgi_app()
app.mount("/metrics", metrics_app)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointing out that /metrics calls are not being logged already:

# Filter out /metrics noise
class MetricsCheckFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return record.getMessage().find("/metrics") == -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant