feat: add /metrics endpoint and middleware for usage statistics#227
feat: add /metrics endpoint and middleware for usage statistics#227SenseiSuraj24 wants to merge 6 commits into
Conversation
|
Hey @imDarshanGK I have completely resolved all the issues and have also implemented the required functions and checked through all the tests , i have updated the info in the same way , you can go ahead for the merge , |
There was a problem hiding this comment.
Pull request overview
This PR adds basic in-memory usage metrics to the FastAPI backend by introducing a /metrics endpoint and a middleware that counts requests and analyses, plus a test validating the metrics payload.
Changes:
- Added global in-memory metrics state (request count, analysis count, languages histogram) and server start timestamp.
- Introduced an HTTP middleware that increments counters and attempts to infer language from successful analysis responses.
- Added
GET /metricsendpoint and a newtest_metrics_endpointin the existing endpoint test suite.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
backend/app/main.py |
Adds metrics state, metrics middleware, and /metrics endpoint implementation. |
backend/tests/test_endpoints.py |
Adds a new test that resets metrics, performs a few requests, and asserts on /metrics output. |
Comments suppressed due to low confidence (2)
backend/app/main.py:89
- This middleware fully buffers every successful analysis response (
response_body = [...]/b"".join(...)) to extract the language. That defeats streaming and adds extra allocations per request. Prefer extracting the language from the request payload (or a dedicated header/field) so you don’t need to consume and rebuildresponse.body_iterator.
try:
response_body = [chunk async for chunk in response.body_iterator]
response.body_iterator = iterate_in_threadpool(iter(response_body))
body_bytes = b"".join(response_body)
data = json.loads(body_bytes)
backend/app/main.py:83
- The middleware claims to track analyses across
/explanation/,/debugging/,/suggestions/, and/analyze/, buttest_metrics_endpointonly exercises/explanation/and/analyze/. Add assertions covering the/debugging/and/suggestions/branches (including howlanguages_detectedshould behave for those routes) to prevent regressions.
if request.url.path in ("/explanation/", "/debugging/", "/suggestions/", "/analyze/") and request.method == "POST":
if response.status_code == 200:
METRICS["total_analyses"] += 1
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
hey @imDarshanGK I have resolved all the conflicts which were suggested by co pilot too , please do add the needful tags and labels mainly gssoc:approved and go ahead for a merge !! |
Here is the complete, filled-out pull request template ready for you to copy and paste into GitHub.
I've also included a suggested PR title at the top for you to use!
Suggested PR Title:
feat: add /metrics endpoint and middleware for usage statisticsDescription
This PR introduces a new
/metricsendpoint that returns real-time usage statistics about the running server, using in-memory counters as requested.Specifically, this PR:
METRICSdictionary inbackend/app/main.pyto storetotal_requests,total_analyses, and a dictionary forlanguages_detected.metrics_middleware) that safely incrementstotal_requestsfor every incoming request and parses successful POST responses to analysis endpoints (/explanation/,/debugging/,/suggestions/,/analyze/) to incrementtotal_analysesand the specificlanguages_detected.GET /metricsroute returning the properly formatted JSON payload including calculateduptime_secondsand the current APIversion.test_metrics_endpointfunction inbackend/tests/test_endpoints.pyto ensure the metrics accurately reflect the server's traffic and state.Related Issue
Fixes #
Type of change
Checklist
mainpytest -vand all tests passfeat/fix/docs/test: short descriptionScreenshots (if frontend change)
N/A — Backend only changes.
Test evidence