Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit 87d4987

Browse files
committed
Add sample_rate configuration
1 parent 6f0727c commit 87d4987

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

docs/advanced.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ The following are valid arguments which may be passed to the Raven client:
175175
String declaration is strict (ie. does not works for child exceptions)
176176
whereas class declaration handle inheritance (ie. child exceptions are also ignored).
177177

178+
.. describe:: sample_rate
179+
180+
The sampling factor to apply to events. A value of 0.00 will deny sending
181+
any events, and a value of 1.00 will send 100% of events.
182+
183+
.. code-block:: python
184+
185+
# send 50% of events
186+
sample_rate = 5.0
187+
178188
.. describe:: list_max_length
179189

180190
The maximum number of items a list-like container should store.

raven/base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from datetime import datetime
2020
from inspect import isclass
21+
from random import Random
2122
from types import FunctionType
2223
from threading import local
2324

@@ -150,7 +151,8 @@ class Client(object):
150151

151152
def __init__(self, dsn=None, raise_send_errors=False, transport=None,
152153
install_sys_hook=True, install_logging_hook=True,
153-
hook_libraries=None, enable_breadcrumbs=True, **options):
154+
hook_libraries=None, enable_breadcrumbs=True,
155+
_random_seed=None, **options):
154156
global Raven
155157

156158
o = options
@@ -195,12 +197,14 @@ def __init__(self, dsn=None, raise_send_errors=False, transport=None,
195197
self.environment = o.get('environment') or None
196198
self.release = o.get('release') or os.environ.get('HEROKU_SLUG_COMMIT')
197199
self.repos = self._format_repos(o.get('repos'))
200+
self.sample_rate = o.get('sample_rate') or 1
198201
self.transaction = TransactionStack()
199-
200202
self.ignore_exceptions = set(o.get('ignore_exceptions') or ())
201203

202204
self.module_cache = ModuleProxyCache()
203205

206+
self._random = Random(_random_seed)
207+
204208
if not self.is_enabled():
205209
self.logger.info(
206210
'Raven is not configured (logging is disabled). Please see the'
@@ -627,7 +631,9 @@ def capture(self, event_type, data=None, date=None, time_spent=None,
627631
event_type, data, date, time_spent, extra, stack, tags=tags,
628632
**kwargs)
629633

630-
self.send(**data)
634+
# should this event be sampled?
635+
if self._random.random() <= self.sample_rate:
636+
self.send(**data)
631637

632638
self._local_state.last_event_id = data['event_id']
633639

raven/utils/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def getopt(key, default=None):
5353
options.setdefault('repos', getopt('repos'))
5454
options.setdefault('environment', getopt('environment'))
5555
options.setdefault('ignore_exceptions', getopt('ignore_exceptions'))
56+
options.setdefault('sample_rate', getopt('sample_rate'))
5657

5758
transport = getopt('transport') or options.get('transport')
5859
if isinstance(transport, string_types):

0 commit comments

Comments
 (0)