Skip to content

Conversation

vk-playground
Copy link
Contributor

@vk-playground vk-playground commented Aug 26, 2025

Issue #699 - Metrics Enhancements

Overview

This document outlines the changes made to address a subset of the requirements in issue #699, which involved enhancing the metrics functionality in MCP Gateway, and provides instructions for testing these enhancements.

Changes Implemented

1. Enhanced Metrics Calculation Functions

Updated the following functions in mcpgateway/utils/metrics_common.py:

  • calculate_success_rate(): Improved to handle edge cases such as:

    • Division by zero (when total is 0)
    • None inputs
    • Type conversion errors
  • format_response_time(): Enhanced to format response times with:

    • Consistent 3 decimal places (x.xxx)
    • Proper handling of None values
    • Consistent display of trailing zeros

2. Admin API Enhancements

Modified mcpgateway/admin.py to:

  • Export ALL rows in metrics CSV exports (previously limited to top 5)
  • Format response times consistently with 3 decimal places
  • Properly handle empty states (display "N/A" when no data exists)

3. Services Improvements

Updated metrics retrieval in service modules to support:

  • Optional limit parameter to retrieve any number of results
  • Unlimited results when exporting metrics data

How to Test

Follow these steps to verify the metrics enhancements:

Prerequisites

  1. Create and activate a Python virtual environment
  2. Install dependencies: pip install -e .

Testing Core Metrics Functions

  1. Run the unit tests for the metrics functions:

    python -m pytest tests/test_metrics_functions.py -v
    

    This will verify:

    • Success rate calculation handles edge cases (0/0, None inputs)
    • Response time formatting is consistent with 3 decimal places
    • Empty states are handled correctly

Testing with Manual Data

  1. Start the MCP Gateway server with admin features enabled:

    $env:MCPGATEWAY_ADMIN_API_ENABLED="true"
    $env:MCPGATEWAY_UI_ENABLED="true"
    python -m uvicorn mcpgateway.main:app --host 0.0.0.0 --port 8008 --reload
    
  2. Create test data using the provided script:

    python tests/setup_test_data.py
    

    This creates test tools with different metrics patterns:

    • Tool 1: 80% success rate (8 successes, 2 failures)
    • Tool 2: 100% success rate (5 successes, 0 failures)
    • Tool 3: 0% success rate (0 successes, 3 failures)
image
  1. Test the admin UI:

    • Access http://localhost:8008/admin (username: admin, password: changeme)
    • Navigate to the Metrics tab
    • Verify all test tools appear with correct success rates
    • Verify response times are shown with 3 decimal places
  2. Test CSV export:

image
  1. Test empty state:
    • Using SQLite, delete all metrics for one tool
    • Verify the UI and export handle empty state by showing "N/A"

Verification Criteria

The issue is considered resolved when:

  1. ✅ Success rate calculation handles all edge cases
  2. ✅ Response times are consistently formatted with 3 decimal places
  3. ✅ CSV exports include ALL rows, not just top 5
  4. ✅ Empty states are handled gracefully with "N/A"

Additional Notes

  • The metrics calculations happen in the metrics_common.py utility file
  • The admin API handles converting None values to "N/A" for display
  • The CSV export uses the same formatting logic as the UI display

Test Files Location

  • Core tests: tests/test_metrics_functions.py
  • Test data generation: tests/test_setup_data.py

Troubleshooting

If the server fails to start with database errors:

  1. Check for unique constraint errors in the database
  2. Update the test data generation script to use unique tool names
  3. Try clearing the existing database or using a new one

Newly added on Sep 9:

Prompt Usage Metrics

  • Status: Added comprehensive metrics recording
  • Location: mcpgateway/services/prompt_service.py
  • Changes Made:
    • Added _record_prompt_metric() method
    • Modified get_prompt() method to record metrics with try-except-finally structure
    • Imports time module for response time calculation
    • Records success/failure, response time, and error messages

Server Interaction Metrics

  • Status: Added comprehensive server interaction metrics recording
  • Location: mcpgateway/federation/forward.py
  • Changes Made:
    • Added ServerMetric import
    • Added time module import
    • Added _record_server_metric() method to ForwardingService class
    • Modified _forward_to_gateway() method to:
      • Track start time using time.monotonic()
      • Record success/failure status
      • Record error messages
      • Always record metrics in finally block
      • Calculate response time accurately

🧪 TESTING RESULTS

Automated Test Results

UPDATED METRICS FUNCTIONALITY TEST
============================================================
Database Setup: ✓ PASS
Service Imports: ✓ PASS
ORM Properties: ✓ PASS  
UI Time Formatting: ✓ PASS
Metrics Infrastructure: ✓ PASS

Overall: 5/5 tests passed

🎉 All tests passed! The metrics functionality has been improved:
   • Tool execution metrics: ✓ Working
   • Prompt usage metrics: ✓ Added
   • Resource read metrics: ✓ Added
   • UI time formatting: ✓ Working
   • Counter incrementation: ✓ Working
   • Timestamp updates: ✓ Working

Database Status

  • tool_metrics: 72 records ✅
  • prompt_metrics: 30 records ✅
  • resource_metrics: 24 records ✅
  • server_metrics: 52 records ✅

🚀 TECHNICAL IMPLEMENTATION DETAILS

1. Prompt Metrics Recording

# Added to mcpgateway/services/prompt_service.py
async def _record_prompt_metric(self, db: Session, prompt: DbPrompt, start_time: float, success: bool, error_message: Optional[str]) -> None:
    end_time = time.monotonic()
    response_time = end_time - start_time
    metric = PromptMetric(
        prompt_id=prompt.id,
        response_time=response_time,
        is_success=success,
        error_message=error_message,
    )
    db.add(metric)
    db.commit()

2. Server Interaction Metrics Recording

# Added to mcpgateway/federation/forward.py
async def _record_server_metric(self, db: Session, gateway: DbGateway, start_time: float, success: bool, error_message: Optional[str]) -> None:
    end_time = time.monotonic()
    response_time = end_time - start_time
    metric = ServerMetric(
        server_id=gateway.id,
        response_time=response_time,
        is_success=success,
        error_message=error_message,
    )
    db.add(metric)
    db.commit()

3. Integration Points

  • Tool metrics: Recorded in tool execution methods ✅
  • Prompt metrics: Recorded in get_prompt() method ✅
  • Resource metrics: Recorded in read_resource() method ✅
  • Server metrics: Recorded in _forward_to_gateway() method ✅

📋 MANUAL TESTING INSTRUCTIONS

Prerequisites

cd mcp-context-forge
$env:MCPGATEWAY_ADMIN_API_ENABLED="true"
$env:MCPGATEWAY_UI_ENABLED="true"
python -m uvicorn mcpgateway.main:app --host 0.0.0.0 --port 8008

Testing Steps

  1. Access Admin UI: http://localhost:8008/admin (admin/changeme)
  2. Test Tool Metrics: Use tools → verify counter increment & time update
  3. Test Prompt Metrics: Use prompts → verify counter increment & time update (NEW)
  4. Test Resource Metrics: Access resources → verify counter increment & time update (NEW)
  5. Test Server Metrics: Trigger server interactions → verify counter increment & time update (NEW)
  6. Verify UI Time Formatting: All timestamps show relative time formatting

Screen Recording 2025-09-09 at 12.16.58 AM

🔍 SUCCESS CRITERIA STATUS UPDATE

IMPLEMENTED (Code Level)

  • Tool executions: Code to increment execution counters ✅
  • Resource reads: Code to increment execution counters ✅
  • Prompt uses: Code to increment execution counters ✅
  • Server interactions: Code to increment execution counters ✅
  • Update lastExecution/lastUsed timestamps: All entity types ✅
  • UI shows correct relative time: "3 minutes ago" format ✅
  • Core functionality preserved: No breaking changes ✅

TESTING STATUS

  • Code Implementation: ✅ COMPLETE - All metrics recording functions implemented
  • Database Schema: ✅ COMPLETE - All metrics tables exist and working
    =

📊 TEST DATA SOLUTION

RESOLVED: Created comprehensive test data in database:

✅ TEST TOOLS ADDED:
  • test-metrics-calculator (Test Metrics Calculator) - ✅ Enabled
  • test-metrics-search (Test Metrics Search) - ✅ Enabled  
  • test-metrics-tool-1 (Test Metrics Tool 1) - ✅ Enabled

✅ TEST PROMPTS ADDED:
  • test-metrics-prompt-1 - Test prompt for metrics verification
  • test-metrics-prompt-2 - Test summarization prompt for metrics

✅ TEST RESOURCES ADDED:
  • test-metrics-resource-1 - Test JSON data resource for metrics
  • test-metrics-resource-2 - Test YAML config resource for metrics

🎯 CURRENT STATUS

CODE IMPLEMENTATION: COMPLETE

All metrics recording functionality has been successfully implemented across all entity types.

FUNCTIONAL TESTING: READY

Test data has been added to database to enable proper testing of metrics functionality.

REQUIREMENTS VERIFICATION

Ready for Testing: The functionality can now be properly tested with:

  1. Database-registered test tools that will properly record metrics
  2. Admin UI access at http://localhost:8008/admin (admin/changeme)
  3. Tool testing that will increment counters and update timestamps
  4. Real-time verification of metrics updates

NEXT STEPS FOR COMPLETE VERIFICATION

  1. Fix the first-row summary above the table

@crivetimihai
Copy link
Member

Please rebase and retest

Vicky Kuo and others added 2 commits September 9, 2025 01:05
…ced, with the exception of the first-row summary above the table, while preserving core functionality.

Signed-off-by: Vicky <[email protected]>
@vk-playground vk-playground marked this pull request as draft September 10, 2025 21:18
@vk-playground
Copy link
Contributor Author

created a new PR here: #992

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.

2 participants