Skip to content

Conversation

@nmveeresh
Copy link
Collaborator

🎯 Goal

Enable unified Prometheus-compatible metrics across all FastAPI services for observability, SLO tracking, and proactive alerting.

🧩 Key Changes

  • Added mcpgateway/services/metrics.py to configure Prometheus metrics instrumentation using prometheus-fastapi-instrumentator.
  • Auto-instrumented HTTP metrics:
    • http_requests_total: Request count by method, endpoint, and status code.
    • http_request_duration_seconds: Latency histogram (buckets: 0.05, 0.1, 0.3, 1, 3, 5).
    • http_request_size_bytes and http_response_size_bytes: Request and response size histograms.
  • Exposed metrics endpoint at /metrics/prometheus .
  • Added support for app_info gauge with METRICS_CUSTOM_LABELS for cluster-level filtering.
  • Configurable exclusion of handlers via METRICS_EXCLUDED_HANDLERS regex.
  • Added environment variables:
    • ENABLE_METRICS (default: true)
    • METRICS_EXCLUDED_HANDLERS
    • METRICS_NAMESPACE, METRICS_SUBSYSTEM
    • METRICS_CUSTOM_LABELS
  • Updated test coverage to validate metrics endpoint, default metric exposure, and exclusion patterns.
  • Metrics overhead verified to stay below 3% CPU under load test.

⚙️ Env Example

ENABLE_METRICS=true
METRICS_EXCLUDED_HANDLERS=.admin.,.internal.
METRICS_NAMESPACE=myplatform
METRICS_SUBSYSTEM=fastapi_service
METRICS_CUSTOM_LABELS=service=myservice,environment=staging,team=platform

✅ Acceptance Criteria

  • /metrics/prometheus returns 200 OK and includes standard Prometheus metrics.
  • Metrics appear within one scrape interval (≤15s).
  • Excluded endpoints are not instrumented.
  • Instrumentation toggle works correctly via env var.

🧪 Testing

Unit and E2E tests added for:

  • Endpoint accessibility
  • Metric exposure

@nmveeresh nmveeresh marked this pull request as ready for review October 21, 2025 13:52
@rakdutta rakdutta self-requested a review October 24, 2025 07:30
Copy link
Collaborator

@rakdutta rakdutta left a comment

Choose a reason for hiding this comment

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

The individual router is functioning correctly; however, documentation for these changes is missing. the router registration in the main application but it was not included the endpoint in the OpenAPI schema so it appears in Swagger UI.

@rakdutta rakdutta force-pushed the feature/218-prometheus-metrics-instrumentation branch from 8ddbed7 to 805e7ef Compare October 27, 2025 11:06
@rakdutta rakdutta self-requested a review October 27, 2025 11:13
@rakdutta
Copy link
Collaborator

Updated the document and include the endpoint in the OpenAPI schema so it appears in Swagger UI.
PR is ready to merge.

Copy link
Collaborator

@rakdutta rakdutta left a comment

Choose a reason for hiding this comment

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

Updated the document and modify the code to include the endpoint in the OpenAPI schema so it appears in Swagger UI.
PR is ready to merge.

nmveeresh and others added 11 commits October 29, 2025 00:03
Signed-off-by: Veeresh K <[email protected]>
Signed-off-by: Veeresh K <[email protected]>
Signed-off-by: Veeresh K <[email protected]>
Signed-off-by: Veeresh K <[email protected]>
Signed-off-by: rakdutta <[email protected]>
Signed-off-by: rakdutta <[email protected]>
Signed-off-by: rakdutta <[email protected]>
Add comprehensive documentation for Prometheus metrics configuration
variables to .env.example:

- ENABLE_METRICS: Toggle metrics collection (default: true)
- METRICS_EXCLUDED_HANDLERS: Regex patterns for endpoint exclusion
- METRICS_NAMESPACE: Metrics name prefix (default: "default")
- METRICS_SUBSYSTEM: Secondary metrics prefix
- METRICS_CUSTOM_LABELS: Static labels for app_info gauge

Includes examples, security warnings about high-cardinality labels,
and formatting consistent with existing configuration sections.

Related to PR #1313

Signed-off-by: Mihai Criveti <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai crivetimihai force-pushed the feature/218-prometheus-metrics-instrumentation branch from 805e7ef to 6c829fc Compare October 29, 2025 00:21
@crivetimihai crivetimihai merged commit bc2f512 into main Oct 29, 2025
45 checks passed
@crivetimihai crivetimihai deleted the feature/218-prometheus-metrics-instrumentation branch October 29, 2025 00:29
@crivetimihai crivetimihai mentioned this pull request Oct 30, 2025
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.

4 participants