Spaces:
Runtime error
Runtime error
| import requests | |
| import time | |
| import sys | |
| BASE_URL = "http://localhost:8000" | |
| def test_metrics(): | |
| print("π Testing Metrics Flow...") | |
| # 1. Send a Chat Request to trigger metrics | |
| chat_payload = { | |
| "message": "Hello, this is a test for metrics", | |
| "website_id": 1, | |
| "session_id": "metrics-test-session" | |
| } | |
| print(f"Sending chat request to {BASE_URL}/api/v2/chat...") | |
| try: | |
| response = requests.post(f"{BASE_URL}/api/v2/chat", json=chat_payload) | |
| if response.status_code == 200: | |
| print("β Chat Request Successful") | |
| else: | |
| print(f"β Chat Request Failed: {response.text}") | |
| # We continue even if failed, as error metrics should be recorded | |
| except Exception as e: | |
| print(f"β Connection Error (Chat): {e}") | |
| return | |
| # Wait a moment for async processing (though metrics are usually synchronous in my impl, best to be safe) | |
| time.sleep(1) | |
| # 2. Fetch Metrics | |
| print(f"Fetching metrics from {BASE_URL}/metrics...") | |
| try: | |
| metrics_response = requests.get(f"{BASE_URL}/metrics") | |
| if metrics_response.status_code != 200: | |
| print(f"β Failed to fetch metrics: {metrics_response.status_code}") | |
| return | |
| content = metrics_response.text | |
| # 3. Analyze Metrics | |
| checks = [ | |
| "chat_requests_total", | |
| "chat_request_duration_seconds", | |
| "model_confidence_score" | |
| ] | |
| print("\n--- Metric Analysis ---") | |
| all_passed = True | |
| for check in checks: | |
| if check in content: | |
| print(f"β Found metric: {check}") | |
| # Optional: Extract value | |
| for line in content.split('\n'): | |
| if line.startswith(check) and '{' in line: | |
| print(f" sample: {line}") | |
| break | |
| else: | |
| print(f"β Missing metric: {check}") | |
| all_passed = False | |
| if all_passed: | |
| print("\nπ Metrics System Verified Successfully!") | |
| else: | |
| print("\nβ οΈ Some metrics were missing.") | |
| except Exception as e: | |
| print(f"β Connection Error (Metrics): {e}") | |
| if __name__ == "__main__": | |
| test_metrics() | |