chore: import upstream snapshot with attribution
Test Browser Use CLI Install / uv pip install (ubuntu-latest) (push) Failing after 1s
Test Browser Use CLI Install / uvx browser-use from local wheel (push) Failing after 1s
Test Browser Use CLI Install / uvx browser-use[cli] from PyPI (push) Failing after 1s
package / pip-install-on-macos-latest-py-3.11 (push) Has been skipped
package / pip-install-on-macos-latest-py-3.13 (push) Has been skipped
package / pip-install-on-ubuntu-latest-py-3.11 (push) Has been skipped
package / pip-install-on-windows-latest-py-3.13 (push) Has been skipped
cloud_evals / trigger_cloud_eval_image_build (push) Failing after 1s
docker / build_publish_image (push) Failing after 1s
Test Browser Use CLI Install / browser-use skill sync (push) Failing after 1s
lint / code-style (push) Failing after 0s
lint / type-checker (push) Failing after 1s
package / pip-build (push) Failing after 1s
lint / syntax-errors (push) Failing after 3s
package / pip-install-on-ubuntu-latest-py-3.13 (push) Has been skipped
package / pip-install-on-windows-latest-py-3.11 (push) Has been skipped
test / ${{ matrix.test_filename }} (push) Has been skipped
test / evaluate-tasks (push) Has been skipped
test / setup-chromium (push) Failing after 2s
test / find_tests (push) Failing after 2s
Test Browser Use CLI Install / uv pip install (windows-latest) (push) Has been cancelled
Test Browser Use CLI Install / uv pip install (macos-latest) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 12:02:32 +08:00
commit 4cd2d4af2b
475 changed files with 121829 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
"""
Telemetry for Browser Use.
"""
from typing import TYPE_CHECKING
# Type stubs for lazy imports
if TYPE_CHECKING:
from browser_use.telemetry.service import ProductTelemetry
from browser_use.telemetry.views import (
BaseTelemetryEvent,
MCPClientTelemetryEvent,
MCPServerTelemetryEvent,
)
# Lazy imports mapping
_LAZY_IMPORTS = {
'ProductTelemetry': ('browser_use.telemetry.service', 'ProductTelemetry'),
'BaseTelemetryEvent': ('browser_use.telemetry.views', 'BaseTelemetryEvent'),
'MCPClientTelemetryEvent': ('browser_use.telemetry.views', 'MCPClientTelemetryEvent'),
'MCPServerTelemetryEvent': ('browser_use.telemetry.views', 'MCPServerTelemetryEvent'),
}
def __getattr__(name: str):
"""Lazy import mechanism for telemetry components."""
if name in _LAZY_IMPORTS:
module_path, attr_name = _LAZY_IMPORTS[name]
try:
from importlib import import_module
module = import_module(module_path)
attr = getattr(module, attr_name)
# Cache the imported attribute in the module's globals
globals()[name] = attr
return attr
except ImportError as e:
raise ImportError(f'Failed to import {name} from {module_path}: {e}') from e
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
__all__ = [
'BaseTelemetryEvent',
'ProductTelemetry',
'MCPClientTelemetryEvent',
'MCPServerTelemetryEvent',
]
+141
View File
@@ -0,0 +1,141 @@
import logging
import os
from dotenv import load_dotenv
from uuid_extensions import uuid7str
from browser_use.telemetry.views import BaseTelemetryEvent
from browser_use.utils import singleton
load_dotenv()
from browser_use.config import CONFIG
logger = logging.getLogger(__name__)
POSTHOG_EVENT_SETTINGS = {
'process_person_profile': True,
}
POSTHOG_PROJECT_API_KEY = 'phc_F8JMNjW1i2KbGUTaW1unnDdLSPCoyc52SGRU0JecaUh'
POSTHOG_HOST = 'https://eu.i.posthog.com'
DEVICE_ID_PATH = str(CONFIG.BROWSER_USE_CONFIG_DIR / 'device_id')
_device_id: str | None = None
def get_or_create_device_id() -> str:
"""Return the anonymous device id shared by telemetry"""
global _device_id
if _device_id:
return _device_id
_device_id = os.environ.get('BROWSER_USE_DEVICE_ID') or _persisted_device_id() or _machine_fingerprint() or uuid7str()
return _device_id
def _persisted_device_id() -> str | None:
try:
if os.path.exists(DEVICE_ID_PATH):
with open(DEVICE_ID_PATH) as f:
return f.read().strip() or None
os.makedirs(os.path.dirname(DEVICE_ID_PATH), exist_ok=True)
new_device_id = uuid7str()
tmp_path = f'{DEVICE_ID_PATH}.{os.getpid()}.tmp'
with open(tmp_path, 'w') as f:
f.write(new_device_id)
os.replace(tmp_path, DEVICE_ID_PATH)
return new_device_id
except Exception:
return None
def _machine_fingerprint() -> str | None:
"""Hashed hardware-derived id"""
import hashlib
import socket
import uuid
node = uuid.getnode()
if (node >> 40) & 0x01: # multicast bit set: getnode() failed and returned a random id
return None
return 'bu_' + hashlib.sha256(f'browser-use:{node}:{socket.gethostname()}'.encode()).hexdigest()[:32]
@singleton
class ProductTelemetry:
"""
Service for capturing anonymized telemetry data.
If the environment variable `ANONYMIZED_TELEMETRY=False`, anonymized telemetry will be disabled.
"""
PROJECT_API_KEY = POSTHOG_PROJECT_API_KEY
HOST = POSTHOG_HOST
_curr_user_id = None
def __init__(self) -> None:
telemetry_disabled = not CONFIG.ANONYMIZED_TELEMETRY
self.debug_logging = CONFIG.BROWSER_USE_LOGGING_LEVEL == 'debug'
if telemetry_disabled:
self._posthog_client = None
else:
from posthog import Posthog
logger.info('Using anonymized telemetry, see https://docs.browser-use.com/development/monitoring/telemetry.')
self._posthog_client = Posthog(
project_api_key=self.PROJECT_API_KEY,
host=self.HOST,
disable_geoip=False,
enable_exception_autocapture=True,
)
# Silence posthog's logging
if not self.debug_logging:
posthog_logger = logging.getLogger('posthog')
posthog_logger.disabled = True
if self._posthog_client is None:
logger.debug('Telemetry disabled')
def capture(self, event: BaseTelemetryEvent) -> None:
if self._posthog_client is None:
return
self._direct_capture(event)
def _direct_capture(self, event: BaseTelemetryEvent) -> None:
"""
Should not be thread blocking because posthog magically handles it
"""
if self._posthog_client is None:
return
try:
self._posthog_client.capture(
distinct_id=self.user_id,
event=event.name,
properties={**event.properties, **POSTHOG_EVENT_SETTINGS},
)
except Exception as e:
logger.error(f'Failed to send telemetry event {event.name}: {e}')
def flush(self) -> None:
if self._posthog_client:
try:
self._posthog_client.flush()
logger.debug('PostHog client telemetry queue flushed.')
except Exception as e:
logger.error(f'Failed to flush PostHog client: {e}')
else:
logger.debug('PostHog client not available, skipping flush.')
@property
def user_id(self) -> str:
if self._curr_user_id:
return self._curr_user_id
self._curr_user_id = get_or_create_device_id()
return self._curr_user_id
+88
View File
@@ -0,0 +1,88 @@
from abc import ABC, abstractmethod
from collections.abc import Sequence
from dataclasses import asdict, dataclass
from typing import Any, Literal
from browser_use.config import is_running_in_docker
@dataclass
class BaseTelemetryEvent(ABC):
@property
@abstractmethod
def name(self) -> str:
pass
@property
def properties(self) -> dict[str, Any]:
props = {k: v for k, v in asdict(self).items() if k != 'name'}
# Add Docker context if running in Docker
props['is_docker'] = is_running_in_docker()
return props
@dataclass
class AgentTelemetryEvent(BaseTelemetryEvent):
# start details
task: str
model: str
model_provider: str
max_steps: int
max_actions_per_step: int
use_vision: bool | Literal['auto']
version: str
source: str
cdp_url: str | None
agent_type: str | None
# step details
action_errors: Sequence[str | None]
action_history: Sequence[list[dict] | None]
urls_visited: Sequence[str | None]
# end details
steps: int
total_input_tokens: int
total_output_tokens: int
prompt_cached_tokens: int
total_tokens: int
total_duration_seconds: float
success: bool | None
final_result_response: str | None
error_message: str | None
# judge details
judge_verdict: bool | None = None
judge_reasoning: str | None = None
judge_failure_reason: str | None = None
judge_reached_captcha: bool | None = None
judge_impossible_task: bool | None = None
name: str = 'agent_event'
@dataclass
class MCPClientTelemetryEvent(BaseTelemetryEvent):
"""Telemetry event for MCP client usage"""
server_name: str
command: str
tools_discovered: int
version: str
action: str # 'connect', 'disconnect', 'tool_call'
tool_name: str | None = None
duration_seconds: float | None = None
error_message: str | None = None
name: str = 'mcp_client_event'
@dataclass
class MCPServerTelemetryEvent(BaseTelemetryEvent):
"""Telemetry event for MCP server usage"""
version: str
action: str # 'start', 'stop', 'tool_call'
tool_name: str | None = None
duration_seconds: float | None = None
error_message: str | None = None
parent_process_cmdline: str | None = None
name: str = 'mcp_server_event'