fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 6s
Deploy Dev / Push (pull_request) Successful in 8s
Deploy Dev / Deploy dev (pull_request) Successful in 22s

This commit is contained in:
Egor Matveev 2025-06-15 02:32:47 +03:00
parent d91ae82f6e
commit 151327ae0a
2 changed files with 37 additions and 15 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

@ -2,6 +2,7 @@ import datetime
import os import os
import requests import requests
import time import time
from utils.metrics import metrics_sender
stage = os.getenv("STAGE", 'local') stage = os.getenv("STAGE", 'local')
@ -42,21 +43,15 @@ class TasksHandlerMixin:
raise QueuesException raise QueuesException
except: except:
print(f'Failed to finish task id={task["id"]}') print(f'Failed to finish task id={task["id"]}')
try:
metric = requests.post('http://monitoring:1237/api/v1/metrics/task', json={ metrics_sender.put(lambda: requests.post('http://monitoring:1237/api/v1/metrics/task', json={
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z", 'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
'service': 'botalka', 'service': 'botalka',
'environment': stage, 'environment': stage,
'queue': self.queue_name, 'queue': self.queue_name,
'success': success, 'success': success,
'execution_time_ms': (end - start).microseconds // 1000, '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}')
@property @property