Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Enhance observability by adding debug observation points in browser profile and session classes

Magnus Müller5bde6dbe985f
diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.py
index c2ca014b0..a5caf17b6 100644
--- a/browser_use/browser/profile.py
+++ b/browser_use/browser/profile.py
@@ -7,6 +7,7 @@ from re import Pattern
from typing import Annotated, Any, Literal, Self
from urllib.parse import urlparse
+from observability import observe_debug
from pydantic import AfterValidator, AliasChoices, BaseModel, ConfigDict, Field, model_validator
from uuid_extensions import uuid7str
@@ -47,7 +48,7 @@ CHROME_DISABLED_COMPONENTS = [
'ThirdPartyStoragePartitioning',
# See https://github.com/microsoft/playwright/issues/16126
'Translate',
- ###########################3
+ # 3
# Added by us:
'AutomationControlled',
'BackForwardCache',
@@ -737,6 +738,7 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro
# logger.info(f'🔍 Preinstalling extension {extension_id}...')
# # TODO: copy this from ArchiveBox implementation
+ @observe_debug(name='detect_display_configuration')
def detect_display_configuration(self) -> None:
"""
Detect the system display size and initialize the display-related config defaults:
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 0259b18b5..e23fea492 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -674,6 +674,7 @@ class BrowserSession(BaseModel):
self.logger.info(f'🎥 Saving browser context trace to {final_trace_path}...')
await self.browser_context.tracing.stop(path=str(final_trace_path))
+ @observe_debug(name='connect_or_launch_browser')
async def _connect_or_launch_browser(self) -> None:
"""Try all connection methods in order of precedence."""
# Try connecting via passed objects first
@@ -816,6 +817,7 @@ class BrowserSession(BaseModel):
semaphore_lax=False,
semaphore_timeout=5, # 5s to wait for global playwright object
)
+ @observe_debug(name='setup_playwright')
async def setup_playwright(self) -> None:
"""
Set up playwright library client object: usually the result of (await async_playwright().start())
@@ -1290,6 +1292,7 @@ class BrowserSession(BaseModel):
# self.browser_profile.user_data_dir = fork_path
# self.browser_profile.prepare_user_data_dir()
+ @observe_debug(name='setup_current_page_change_listeners')
async def _setup_current_page_change_listeners(self) -> None:
# Uses a combination of:
# - visibilitychange events
@@ -1535,6 +1538,7 @@ class BrowserSession(BaseModel):
if self.browser_profile.keep_alive is None:
self.browser_profile.keep_alive = keep_alive
+ @observe_debug(name='is_connected')
async def is_connected(self, restart: bool = True) -> bool:
"""
Check if the browser session has valid, connected browser and context objects.
@@ -1619,6 +1623,7 @@ class BrowserSession(BaseModel):
if not already_disconnected:
self.logger.debug(f'⚰️ Browser {self._connection_str} disconnected')
+ @observe_debug(name='prepare_user_data_dir')
def prepare_user_data_dir(self) -> None:
"""Create and unlock the user data dir and ensure all recording paths exist."""
diff --git a/eval/service.py b/eval/service.py
index 7f9d91e57..42123ee49 100644
--- a/eval/service.py
+++ b/eval/service.py
@@ -15,7 +15,7 @@ from uuid import UUID
import anyio
from dotenv import load_dotenv
-from lmnr import AsyncLaminarClient, Laminar, observe
+from lmnr import AsyncLaminarClient, Instruments, Laminar
from pydantic import BaseModel
from browser_use import ActionResult, Agent, BrowserSession, Controller
@@ -78,7 +78,7 @@ if not SERPER_API_KEY:
# Tracking and Observations
# ================================================
-Laminar.initialize()
+Laminar.initialize(disabled_instruments={Instruments.BROWSER_USE}, disable_batch=True)
laminar_client = AsyncLaminarClient()
# Resource monitoring functions moved to resource_monitoring.py module
@@ -519,7 +519,7 @@ async def judge_task_result(
# ================================================
-@observe_debug(name='executor', span_type='EXECUTOR') # type: ignore[arg-type]
+@observe(name='executor', span_type='EXECUTOR') # type: ignore[arg-type]
async def run_agent_with_browser(
browser_session: BrowserSession,
task: Task,