Skip to content

Commit 9dd0ba5

Browse files
committed
feat: metric controller
metric can be enable or disable by controller
1 parent e2b0f3b commit 9dd0ba5

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

sentry_dynamic_sampling_lib/sampler.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ def update_config(self):
6262
return
6363

6464
data = resp.json()
65-
self.config.sample_rate = data["active_sample_rate"]
66-
self.config.ignored_paths = data["wsgi_ignore_path"]
67-
self.config.ignored_tasks = data["celery_ignore_task"]
65+
self.config.update(data)
66+
self.metrics.set_mode(MetricType.CELERY, data["celery_collect_metrics"])
67+
self.metrics.set_mode(MetricType.WSGI, data["wsgi_collect_metrics"])
6868

6969
def update_metrics(self):
7070
for metric_type in MetricType:
71+
# check if metric is enable
72+
mode = self.metrics.get_mode(metric_type)
73+
if not mode:
74+
continue
75+
7176
counter = self.metrics.get_and_reset(metric_type)
7277
if len(counter) == 0:
7378
return

sentry_dynamic_sampling_lib/shared.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def ignored_tasks(self):
4747
def ignored_tasks(self, new_ignored_tasks):
4848
self._ignored_tasks = set(new_ignored_tasks)
4949

50+
@synchronized
51+
def update(self, data):
52+
self._sample_rate = data["active_sample_rate"]
53+
self._ignored_paths = data["wsgi_ignore_path"]
54+
self._ignored_tasks = data["celery_ignore_task"]
55+
5056

5157
class MetricType(Enum):
5258
WSGI = "WSGI"
@@ -56,18 +62,30 @@ class MetricType(Enum):
5662
class Metric:
5763
def __init__(self) -> None:
5864
self._lock = RLock()
65+
self._activate = {
66+
MetricType.WSGI: False,
67+
MetricType.CELERY: False,
68+
}
5969
self._counters = {
6070
MetricType.WSGI: Counter(),
6171
MetricType.CELERY: Counter(),
6272
}
6373

74+
def set_mode(self, _type, mode):
75+
self._activate[_type] = mode
76+
77+
def get_mode(self, _type):
78+
return self._activate[_type]
79+
6480
@synchronized
6581
def count_path(self, path):
66-
self._counters[MetricType.WSGI][path] += 1
82+
if self._activate[MetricType.WSGI]:
83+
self._counters[MetricType.WSGI][path] += 1
6784

6885
@synchronized
6986
def count_task(self, path):
70-
self._counters[MetricType.CELERY][path] += 1
87+
if self._activate[MetricType.CELERY]:
88+
self._counters[MetricType.CELERY][path] += 1
7189

7290
@synchronized
7391
def get_and_reset(self, _type):

0 commit comments

Comments
 (0)