forked from VRAutomatize/browser-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.py
More file actions
66 lines (62 loc) · 2.54 KB
/
Copy pathtelemetry.py
File metadata and controls
66 lines (62 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Dict, Optional, Any
from datetime import datetime
import asyncio
import psutil
import json
import logging
from database import get_db, Task
from logging_config import log_info, log_error
from browser import BrowserManager
from settings import TaskRequest
import aiohttp
import traceback
import os
# Logging configuration
from settings import METRICS_WEBHOOK_URL, ERROR_WEBHOOK_URL, NOTIFY_WEBHOOK_URL
logger = logging.getLogger('browser-use.telemetry')
async def send_metrics_to_webhook(metrics: Dict[str, Any]):
"""Send system metrics to webhook"""
try:
async with aiohttp.ClientSession() as session:
payload = {
"metrics": metrics,
"timestamp": datetime.utcnow().isoformat()
}
async with session.post(METRICS_WEBHOOK_URL, json=payload) as response:
if response.status != 200:
logger.error(f"Failed to send metrics to webhook: {response.status}")
except Exception as e:
logger.error(f"Error sending metrics to webhook: {str(e)}")
async def send_error_to_webhook(error: str, context: str, task_id: Optional[str] = None):
"""Send error information to webhook"""
try:
async with aiohttp.ClientSession() as session:
payload = {
"error": error,
"context": context,
"task_id": task_id,
"timestamp": datetime.utcnow().isoformat(),
"stack_trace": traceback.format_exc()
}
async with session.post(ERROR_WEBHOOK_URL, json=payload) as response:
if response.status != 200:
logger.error(f"Failed to send error to webhook: {response.status}")
except Exception as e:
logger.error(f"Error sending to webhook: {str(e)}")
async def notify_new_run(task_id: int, task: str, config: Dict[str, Any]):
"""Notify webhook about a new task"""
try:
async with aiohttp.ClientSession() as session:
payload = {
"task_id": task_id,
"task": task,
"config": config,
"timestamp": datetime.utcnow().isoformat()
}
async with session.post(NOTIFY_WEBHOOK_URL, json=payload) as response:
if response.status != 200:
logger.error(f"Failed to notify about new task: {response.status}")
except Exception as e:
logger.error(f"Error notifying about new task: {str(e)}")