Compare commits

..

No commits in common. "9b2e96d40132788c220868b17043d5a97581e553" and "d27eed093bba34196e8ae85ea058426dcd7134a1" have entirely different histories.

2 changed files with 38 additions and 22 deletions

27
utils/metrics.py Normal file
View File

@ -0,0 +1,27 @@
from queue import Queue
from threading import Thread
class MetricsSender(Thread):
queue: Queue = Queue()
def put(self, metric):
self.queue.put(metric)
def run(self):
while True:
metric = self.queue.get()
if not metric:
continue
try:
metric = metric()
if metric.status_code == 202:
print('metric ok')
else:
print(f'metric failed: {metric.status_code}')
except Exception as e:
print(f'metric failed: {e}')
metrics_sender = MetricsSender()
metrics_sender.start()

View File

@ -1,8 +1,8 @@
import datetime
import os
from threading import Thread
import requests
import time
from utils.metrics import metrics_sender
stage = os.getenv("STAGE", 'local')
@ -17,26 +17,6 @@ class QueuesException(Exception):
class TasksHandlerMixin:
def _send_metric(self, start, success, end):
try:
metric = requests.post('http://monitoring:1237/api/v1/metrics/task', json={
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
'service': 'botalka',
'environment': stage,
'queue': self.queue_name,
'success': success,
'execution_time_ms': (end - start).microseconds // 1000,
})
if metric.status_code == 202:
print('metric ok')
else:
print(f'metric failed: {metric.status_code}')
except Exception as e:
print(f'metric failed: {e}')
def send_metric(self, start, success, end):
Thread(target=self._send_metric, args=(start, success, end)).start()
def poll(self):
while True:
try:
@ -63,7 +43,16 @@ class TasksHandlerMixin:
raise QueuesException
except:
print(f'Failed to finish task id={task["id"]}')
self.send_metric(start, success, end)
metrics_sender.put(lambda: requests.post('http://monitoring:1237/api/v1/metrics/task', json={
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
'service': 'botalka',
'environment': stage,
'queue': self.queue_name,
'success': success,
'execution_time_ms': (end - start).microseconds // 1000,
}))
@property
def queue_name(self):