diff --git a/docs/my-website/docs/observability/sentry.md b/docs/my-website/docs/observability/sentry.md index 5b1770fbad..b7992e35c5 100644 --- a/docs/my-website/docs/observability/sentry.md +++ b/docs/my-website/docs/observability/sentry.md @@ -49,6 +49,18 @@ response = completion(model="gpt-3.5-turbo", messages=[{"role": "user", "content print(response) ``` +#### Sample Rate Options + +- **SENTRY_API_SAMPLE_RATE**: Controls what percentage of errors are sent to Sentry + - Value between 0 and 1 (default is 1.0 or 100% of errors) + - Example: 0.5 sends 50% of errors, 0.1 sends 10% of errors + +- **SENTRY_API_TRACE_RATE**: Controls what percentage of transactions are sampled for performance monitoring + - Value between 0 and 1 (default is 1.0 or 100% of transactions) + - Example: 0.5 traces 50% of transactions, 0.1 traces 10% of transactions + +These options are useful for high-volume applications where sampling a subset of errors and transactions provides sufficient visibility while managing costs. + ## Redacting Messages, Response Content from Sentry Logging Set `litellm.turn_off_message_logging=True` This will prevent the messages and responses from being logged to sentry, but request metadata will still be logged. diff --git a/docs/my-website/docs/proxy/logging.md b/docs/my-website/docs/proxy/logging.md index 99d59e6deb..a4d5009e85 100644 --- a/docs/my-website/docs/proxy/logging.md +++ b/docs/my-website/docs/proxy/logging.md @@ -2375,6 +2375,9 @@ pip install --upgrade sentry-sdk ```shell export SENTRY_DSN="your-sentry-dsn" +# Optional: Configure Sentry sampling rates +export SENTRY_API_SAMPLE_RATE="1.0" # Controls what percentage of errors are sent (default: 1.0 = 100%) +export SENTRY_API_TRACE_RATE="1.0" # Controls what percentage of transactions are sampled for performance monitoring (default: 1.0 = 100%) ``` ```yaml diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index e0a08021e5..cb5673f622 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -2691,9 +2691,15 @@ def set_callbacks(callback_list, function_id=None): # noqa: PLR0915 if "SENTRY_API_TRACE_RATE" in os.environ else "1.0" ) + sentry_sample_rate = ( + os.environ.get("SENTRY_API_SAMPLE_RATE") + if "SENTRY_API_SAMPLE_RATE" in os.environ + else "1.0" + ) sentry_sdk_instance.init( dsn=os.environ.get("SENTRY_DSN"), traces_sample_rate=float(sentry_trace_rate), # type: ignore + sample_rate=float(sentry_sample_rate), ) capture_exception = sentry_sdk_instance.capture_exception add_breadcrumb = sentry_sdk_instance.add_breadcrumb diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py index 6d4b9c4a42..f6f6c88917 100644 --- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py +++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py @@ -11,7 +11,7 @@ sys.path.insert( import time -from litellm.litellm_core_utils.litellm_logging import Logging as LitellmLogging +from litellm.litellm_core_utils.litellm_logging import Logging as LitellmLogging, set_callbacks @pytest.fixture @@ -34,6 +34,32 @@ def test_get_masked_api_base(logging_obj): assert type(masked_api_base) == str +def test_sentry_sample_rate(): + existing_sample_rate = os.getenv("SENTRY_API_SAMPLE_RATE") + try: + # test with default value by removing the environment variable + if existing_sample_rate: + del os.environ["SENTRY_API_SAMPLE_RATE"] + + set_callbacks(["sentry"]) + # Check if the default sample rate is set to 1.0 + assert os.environ.get("SENTRY_API_SAMPLE_RATE") == "1.0" + + # test with custom value + os.environ["SENTRY_API_SAMPLE_RATE"] = "0.5" + + set_callbacks(["sentry"]) + # Check if the custom sample rate is set correctly + assert os.environ.get("SENTRY_API_SAMPLE_RATE") == "0.5" + except Exception as e: + print(f"Error: {e}") + finally: + # Restore the original environment variable + if existing_sample_rate: + os.environ["SENTRY_API_SAMPLE_RATE"] = existing_sample_rate + else: + if "SENTRY_API_SAMPLE_RATE" in os.environ: + del os.environ["SENTRY_API_SAMPLE_RATE"] def test_use_custom_pricing_for_model(): from litellm.litellm_core_utils.litellm_logging import use_custom_pricing_for_model