The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Add sentry sample rate (#10283)
Vinnie-Singleton-NN178a614d4a50
diff --git a/docs/my-website/docs/observability/sentry.md b/docs/my-website/docs/observability/sentry.mdindex 5b1770fbadb..b7992e35c54 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", "contentprint(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 LoggingSet `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.mdindex 99d59e6deb7..a4d5009e853 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```shellexport 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%)``````yamldiff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.pyindex e0a08021e59..cb5673f6228 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: PLR0915if "SENTRY_API_TRACE_RATE" in os.environelse "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_exceptionadd_breadcrumb = sentry_sdk_instance.add_breadcrumbdiff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.pyindex 6d4b9c4a421..f6f6c889178 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