Spaces:
Sleeping
Sleeping
File size: 6,209 Bytes
d7b3d84 |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# @file purpose: Observability module for browser-use that handles optional lmnr integration with debug mode support
"""
Observability module for browser-use
This module provides observability decorators that optionally integrate with lmnr (Laminar) for tracing.
If lmnr is not installed, it provides no-op wrappers that accept the same parameters.
Features:
- Optional lmnr integration - works with or without lmnr installed
- Debug mode support - observe_debug only traces when in debug mode
- Full parameter compatibility with lmnr observe decorator
- No-op fallbacks when lmnr is unavailable
"""
import logging
import os
from collections.abc import Callable
from functools import wraps
from typing import Any, Literal, TypeVar, cast
logger = logging.getLogger(__name__)
from dotenv import load_dotenv
load_dotenv()
# Type definitions
F = TypeVar('F', bound=Callable[..., Any])
# Check if we're in debug mode
def _is_debug_mode() -> bool:
"""Check if we're in debug mode based on environment variables or logging level."""
lmnr_debug_mode = os.getenv('LMNR_LOGGING_LEVEL', '').lower()
if lmnr_debug_mode == 'debug':
# logger.info('Debug mode is enabled for observability')
return True
# logger.info('Debug mode is disabled for observability')
return False
# Try to import lmnr observe
_LMNR_AVAILABLE = False
_lmnr_observe = None
try:
from lmnr import observe as _lmnr_observe # type: ignore
if os.environ.get('BROWSER_USE_VERBOSE_OBSERVABILITY', 'false').lower() == 'true':
logger.debug('Lmnr is available for observability')
_LMNR_AVAILABLE = True
except ImportError:
if os.environ.get('BROWSER_USE_VERBOSE_OBSERVABILITY', 'false').lower() == 'true':
logger.debug('Lmnr is not available for observability')
_LMNR_AVAILABLE = False
def _create_no_op_decorator(
name: str | None = None,
ignore_input: bool = False,
ignore_output: bool = False,
metadata: dict[str, Any] | None = None,
**kwargs: Any,
) -> Callable[[F], F]:
"""Create a no-op decorator that accepts all lmnr observe parameters but does nothing."""
import asyncio
def decorator(func: F) -> F:
if asyncio.iscoroutinefunction(func):
@wraps(func)
async def async_wrapper(*args, **kwargs):
return await func(*args, **kwargs)
return cast(F, async_wrapper)
else:
@wraps(func)
def sync_wrapper(*args, **kwargs):
return func(*args, **kwargs)
return cast(F, sync_wrapper)
return decorator
def observe(
name: str | None = None,
ignore_input: bool = False,
ignore_output: bool = False,
metadata: dict[str, Any] | None = None,
span_type: Literal['DEFAULT', 'LLM', 'TOOL'] = 'DEFAULT',
**kwargs: Any,
) -> Callable[[F], F]:
"""
Observability decorator that traces function execution when lmnr is available.
This decorator will use lmnr's observe decorator if lmnr is installed,
otherwise it will be a no-op that accepts the same parameters.
Args:
name: Name of the span/trace
ignore_input: Whether to ignore function input parameters in tracing
ignore_output: Whether to ignore function output in tracing
metadata: Additional metadata to attach to the span
**kwargs: Additional parameters passed to lmnr observe
Returns:
Decorated function that may be traced depending on lmnr availability
Example:
@observe(name="my_function", metadata={"version": "1.0"})
def my_function(param1, param2):
return param1 + param2
"""
kwargs = {
'name': name,
'ignore_input': ignore_input,
'ignore_output': ignore_output,
'metadata': metadata,
'span_type': span_type,
'tags': ['observe', 'observe_debug'], # important: tags need to be created on laminar first
**kwargs,
}
if _LMNR_AVAILABLE and _lmnr_observe:
# Use the real lmnr observe decorator
return cast(Callable[[F], F], _lmnr_observe(**kwargs))
else:
# Use no-op decorator
return _create_no_op_decorator(**kwargs)
def observe_debug(
name: str | None = None,
ignore_input: bool = False,
ignore_output: bool = False,
metadata: dict[str, Any] | None = None,
span_type: Literal['DEFAULT', 'LLM', 'TOOL'] = 'DEFAULT',
**kwargs: Any,
) -> Callable[[F], F]:
"""
Debug-only observability decorator that only traces when in debug mode.
This decorator will use lmnr's observe decorator if both lmnr is installed
AND we're in debug mode, otherwise it will be a no-op.
Debug mode is determined by:
- DEBUG environment variable set to 1/true/yes/on
- BROWSER_USE_DEBUG environment variable set to 1/true/yes/on
- Root logging level set to DEBUG or lower
Args:
name: Name of the span/trace
ignore_input: Whether to ignore function input parameters in tracing
ignore_output: Whether to ignore function output in tracing
metadata: Additional metadata to attach to the span
**kwargs: Additional parameters passed to lmnr observe
Returns:
Decorated function that may be traced only in debug mode
Example:
@observe_debug(ignore_input=True, ignore_output=True,name="debug_function", metadata={"debug": True})
def debug_function(param1, param2):
return param1 + param2
"""
kwargs = {
'name': name,
'ignore_input': ignore_input,
'ignore_output': ignore_output,
'metadata': metadata,
'span_type': span_type,
'tags': ['observe_debug'], # important: tags need to be created on laminar first
**kwargs,
}
if _LMNR_AVAILABLE and _lmnr_observe and _is_debug_mode():
# Use the real lmnr observe decorator only in debug mode
return cast(Callable[[F], F], _lmnr_observe(**kwargs))
else:
# Use no-op decorator (either not in debug mode or lmnr not available)
return _create_no_op_decorator(**kwargs)
# Convenience functions for checking availability and debug status
def is_lmnr_available() -> bool:
"""Check if lmnr is available for tracing."""
return _LMNR_AVAILABLE
def is_debug_mode() -> bool:
"""Check if we're currently in debug mode."""
return _is_debug_mode()
def get_observability_status() -> dict[str, bool]:
"""Get the current status of observability features."""
return {
'lmnr_available': _LMNR_AVAILABLE,
'debug_mode': _is_debug_mode(),
'observe_active': _LMNR_AVAILABLE,
'observe_debug_active': _LMNR_AVAILABLE and _is_debug_mode(),
}
|