Skip to content

Commit 0d2d6e3

Browse files
committed
feat: sample_rate option
1 parent 6ec18ef commit 0d2d6e3

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

sentry_sdk/client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import uuid
3+
import random
34

45
from .utils import Dsn, SkipEvent, ContextVar
56
from .transport import Transport
@@ -27,14 +28,15 @@ def __init__(self, dsn=None, *args, **kwargs):
2728
options = dict(DEFAULT_OPTIONS)
2829
options.update(*args, **kwargs)
2930
self.options = options
30-
self._transport = self.options.pop('transport')
31+
self._transport = self.options.pop("transport")
3132
if self._transport is None and dsn is not None:
3233
self._transport = Transport(dsn)
3334
self._transport.start()
3435
elif passed_dsn is not None and self._transport is not None:
3536
raise ValueError("Cannot pass DSN and a custom transport.")
3637

3738
from .integrations import logging as logging_integration
39+
3840
integrations = list(options.pop("integrations") or ())
3941

4042
logging_configured = any(
@@ -78,6 +80,12 @@ def _prepare_event(self, event, scope):
7880
if event.get("platform") is None:
7981
event["platform"] = "python"
8082

83+
if (
84+
self.options["sample_rate"] < 1.0
85+
and random.random() >= self.options["sample_rate"]
86+
):
87+
raise SkipEvent()
88+
8189
event = strip_event(event)
8290
event = flatten_metadata(event)
8391
return event

sentry_sdk/consts.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
"drain_timeout": 2.0,
1212
"integrations": [],
1313
"default_integrations": True,
14-
'repos': {},
15-
'dist': None,
16-
'transport': None
14+
"repos": {},
15+
"dist": None,
16+
"transport": None,
17+
"sample_rate": 1.0,
1718
}
1819

1920
SDK_INFO = {"name": "sentry-python", "version": VERSION}

tests/test_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from sentry_sdk import Client
33
from sentry_sdk.transport import Transport
44

5+
56
def test_transport_option(monkeypatch):
6-
dsn = 'https://[email protected]/123'
7-
dsn2 = 'https://[email protected]/124'
7+
dsn = "https://[email protected]/123"
8+
dsn2 = "https://[email protected]/124"
89
assert str(Client(dsn=dsn).dsn) == dsn
910
assert Client().dsn is None
1011
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)