Skip to content

Commit 2dff223

Browse files
committed
sync metrics updates
1 parent e1a51d3 commit 2dff223

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/robusta/utils/task_queue.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import threading
23
import time
34
from threading import Thread
45
from queue import Queue, Full
@@ -27,6 +28,7 @@ def __init_metrics(self):
2728
self.metrics_thread = Thread(target=self.__report_metrics)
2829
self.metrics_thread.daemon = True
2930
self.metrics_thread.start()
31+
self.metrics_lock = threading.Lock()
3032

3133
def __report_metrics(self):
3234
while True:
@@ -47,10 +49,12 @@ def add_task(self, task, *args, **kwargs):
4749
try:
4850
self.put((task, args, kwargs), block=False)
4951
except Full:
50-
self.metrics.rejected += 1
52+
with self.metrics_lock:
53+
self.metrics.rejected += 1
5154
return
5255

53-
self.metrics.queued += 1
56+
with self.metrics_lock:
57+
self.metrics.queued += 1
5458

5559
def __start_workers(self):
5660
for i in range(self.num_workers):
@@ -61,8 +65,10 @@ def __start_workers(self):
6165
def worker(self):
6266
while True:
6367
item, args, kwargs = self.get()
64-
self.metrics.processed += 1
68+
with self.metrics_lock:
69+
self.metrics.processed += 1
6570
start_time = time.time()
6671
item(*args, **kwargs)
67-
self.metrics.total_process_time += (time.time() - start_time)
72+
with self.metrics_lock:
73+
self.metrics.total_process_time += (time.time() - start_time)
6874
self.task_done()

0 commit comments

Comments
 (0)