Merge pull request 'fix' (#51) from master into dev
Reviewed-on: https://gitea.chocomarsh.com/self/botalka/pulls/51
This commit is contained in:
commit
d27eed093b
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 requests
|
||||
import time
|
||||
from utils.metrics import metrics_sender
|
||||
|
||||
|
||||
stage = os.getenv("STAGE", 'local')
|
||||
@ -42,21 +43,15 @@ class TasksHandlerMixin:
|
||||
raise QueuesException
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
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}')
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user