mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-21 08:26:34 +00:00
Add sentry sample rate (#10283)
* Add SENTRY_API_SAMPLE_RATE configuration option for Sentry SDK * removed print line * Update Sentry documentation with sample rate information --------- Co-authored-by: Vinnie <vinnie@Vinnies-MacBook-Pro.local>
This commit is contained in:
co-authored by
Vinnie
parent
56c32ef503
commit
178a614d4a
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user