customeragent-api / server /scripts /test_metrics.py
anasraza526's picture
Clean deploy to Hugging Face
ac90985
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()