Skip to content

Commit 7395b58

Browse files
committed
use singleton pattern for sampler
1 parent 4463c4e commit 7395b58

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/scout_apm/core/tracked_request.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ class TrackedRequest(object):
2424
their keyname
2525
"""
2626

27+
_sampler = None
28+
29+
@classmethod
30+
def get_sampler(cls):
31+
if cls._sampler is None:
32+
cls._sampler = Sampler(scout_config)
33+
return cls._sampler
34+
2735
__slots__ = (
2836
"sampler",
2937
"request_id",
@@ -50,7 +58,6 @@ def instance(cls):
5058
return context.get_tracked_request()
5159

5260
def __init__(self):
53-
self.sampler = Sampler(scout_config)
5461
self.request_id = "req-" + str(uuid4())
5562
self.start_time = dt.datetime.now(dt.timezone.utc)
5663
self.end_time = None
@@ -153,12 +160,10 @@ def finish(self):
153160
self.end_time = dt.datetime.now(dt.timezone.utc)
154161

155162
if self.is_real_request:
156-
# Use the sampler to determine if the request should be sampled
157-
logger.debug("Checking if request should be sampled")
158163
if (
159164
not self.is_ignored()
160165
and not self.sent
161-
and self.sampler.should_sample(self.operation)
166+
and self.get_sampler().should_sample(self.operation)
162167
):
163168
self.tag("mem_delta", self._get_mem_delta())
164169
self.sent = True

tests/unit/core/test_sampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_should_sample_unknown_operation(sampler):
7777

7878
def test_should_sample_no_sampling_enabled(config):
7979
config.set(
80-
sample_rate=100, # 100% global sampling (effectively no sampling)
80+
sample_rate=100, # Return config to defaults
8181
sample_endpoints={},
8282
sample_jobs={},
8383
ignore_endpoints=[],

0 commit comments

Comments
 (0)