Skip to content

Added easy to interpret metrics for observability and related tests via Prometheus - #1034

Open
shantanushok wants to merge 7 commits into
imDarshanGK:mainfrom
shantanushok:fix/csrf-prometheus-metrics
Open

Added easy to interpret metrics for observability and related tests via Prometheus#1034
shantanushok wants to merge 7 commits into
imDarshanGK:mainfrom
shantanushok:fix/csrf-prometheus-metrics

Conversation

@shantanushok

Copy link
Copy Markdown

Description

Resolves #919

Added easy to interpret metrics for observability and related tests via Prometheus. This ensures that CSRF evaluations (allowances, rejections, and safe method skips) are correctly tracked and monitored.

Summary of Changes

  • backend/middleware/csrf.py:

    • Imported Counter from prometheus_client and defined CSRF_REQUESTS and CSRF_REJECTIONS metrics.
    • Updated OriginValidationMiddleware to increment csrf_requests_total for safe methods (skipped_safe_method), authorized origins (allowed), and unauthorized cross-origin requests (rejected).
    • Added tracking for csrf_rejections_total to specify the HTTP method and invalid_origin reason when a request is blocked.
  • backend/tests/test_csrf.py:

    • Fixed the failing test_prometheus_metrics regression test, which was previously failing due to AssertionError: assert 0.0 > 0.0 caused by the missing middleware implementation.

Verification

Ran backend CSRF validation and metrics regression tests:

python -m pytest backend/tests/test_csrf.py -v

Copilot AI review requested due to automatic review settings July 28, 2026 06:07
@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

@shantanushok is attempting to deploy a commit to the Darshan's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds Prometheus counters to the CSRF OriginValidationMiddleware so CSRF decision outcomes (safe-method skip, allowed, rejected) and rejection reasons can be tracked for observability, and it extends the CSRF test suite with a regression test that asserts these metrics increase as expected.

Changes:

  • Added Prometheus Counter metrics in backend/middleware/csrf.py and incremented them on safe-method skips, allows, and rejects (including method + reason for rejects).
  • Added a CSRF Prometheus metrics regression test in backend/tests/test_csrf.py to validate counter increments via real requests through the middleware.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
backend/middleware/csrf.py Introduces csrf_requests_total and csrf_rejections_total counters and increments them on CSRF decision paths.
backend/tests/test_csrf.py Adds a metrics-focused regression test that checks Prometheus counters increase after representative requests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/middleware/csrf.py Outdated
Comment on lines +37 to +41
from prometheus_client import Counter

logger = logging.getLogger(__name__)

# Prometheus Metrics
assert after_skipped > before_skipped

# 2. Trigger an allowed method
client.post("/api/sessions/", json={"title": "Metric allowed"})
@imDarshanGK imDarshanGK added SSoC26 Part of Social Summer of Code 2026 Medium Feature or backend work with moderate scope labels Jul 28, 2026

@imDarshanGK imDarshanGK left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@shantanushok

Copy link
Copy Markdown
Author

Hey @imDarshanGK please review the PR and hopefully merge it :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Medium Feature or backend work with moderate scope SSoC26 Part of Social Summer of Code 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add observability metrics to security middleware

4 participants