fix
This commit is contained in:
parent
d91ae82f6e
commit
151327ae0a
27
utils/metrics.py
Normal file
27
utils/metrics.py
Normal 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()
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user