2024-02-14 19:22:56 +00:00
|
|
|
from sanic.log import logger, logging
|
|
|
|
from logging import LogRecord
|
|
|
|
from config import *
|
2024-03-12 20:35:52 +00:00
|
|
|
import traceback
|
2024-02-14 19:22:56 +00:00
|
|
|
|
|
|
|
METRICS_REQ_NO = 0
|
2024-02-22 18:15:33 +00:00
|
|
|
METRICS_ERR_NO = 0 # Errors served to the clients
|
2024-02-14 19:22:56 +00:00
|
|
|
METRICS_PRETIX_READ = 0
|
|
|
|
METRICS_PRETIX_WRITE = 0
|
2024-02-22 18:15:33 +00:00
|
|
|
METRICS_PRETIX_ERRORS = 0 # Errors requesting pretix's backend
|
2024-02-14 19:22:56 +00:00
|
|
|
|
|
|
|
def incPretixRead():
|
|
|
|
global METRICS_PRETIX_READ
|
|
|
|
METRICS_PRETIX_READ += 1
|
|
|
|
|
|
|
|
def incPretixWrite():
|
|
|
|
global METRICS_PRETIX_WRITE
|
|
|
|
METRICS_PRETIX_WRITE += 1
|
|
|
|
|
2024-02-22 18:15:33 +00:00
|
|
|
def incPretixErrors():
|
|
|
|
global METRICS_PRETIX_ERRORS
|
|
|
|
METRICS_PRETIX_ERRORS += 1
|
|
|
|
|
2024-02-14 19:22:56 +00:00
|
|
|
def incReqNo():
|
|
|
|
global METRICS_REQ_NO
|
|
|
|
METRICS_REQ_NO += 1
|
|
|
|
|
2024-02-22 18:15:33 +00:00
|
|
|
def incErrorNo(): # Errors served to the clients
|
|
|
|
global METRICS_ERR_NO
|
|
|
|
METRICS_ERR_NO += 1
|
|
|
|
|
|
|
|
|
2024-02-14 19:22:56 +00:00
|
|
|
def getMetricsText():
|
|
|
|
global METRICS_REQ_NO
|
2024-02-22 18:15:33 +00:00
|
|
|
global METRICS_ERR_NO
|
2024-02-14 19:22:56 +00:00
|
|
|
global METRICS_PRETIX_READ
|
|
|
|
global METRICS_PRETIX_WRITE
|
2024-02-22 18:15:33 +00:00
|
|
|
global METRICS_PRETIX_ERRORS
|
2024-02-14 19:22:56 +00:00
|
|
|
out = []
|
|
|
|
|
|
|
|
out.append(f'sanic_request_count{{}} {METRICS_REQ_NO}')
|
2024-02-22 18:15:33 +00:00
|
|
|
out.append(f'sanic_error_count{{}} {METRICS_ERR_NO}')
|
2024-02-14 19:22:56 +00:00
|
|
|
out.append(f'webint_pretix_read_count{{}} {METRICS_PRETIX_READ}')
|
|
|
|
out.append(f'webint_pretix_write_count{{}} {METRICS_PRETIX_WRITE}')
|
2024-02-22 18:15:33 +00:00
|
|
|
out.append(f'webint_pretix_error_count{{}} {METRICS_PRETIX_ERRORS}')
|
2024-02-14 19:22:56 +00:00
|
|
|
|
|
|
|
return "\n".join(out)
|
|
|
|
|
|
|
|
def getRoomCountersText(request):
|
|
|
|
out = []
|
2024-03-12 20:35:52 +00:00
|
|
|
try:
|
2024-02-14 19:22:56 +00:00
|
|
|
daily = 0
|
|
|
|
counters = {}
|
2024-02-27 18:53:07 +00:00
|
|
|
counters_early = {}
|
|
|
|
counters_late = {}
|
2024-02-14 19:22:56 +00:00
|
|
|
for id in ROOM_TYPE_NAMES.keys():
|
|
|
|
counters[id] = 0
|
2024-02-27 18:53:07 +00:00
|
|
|
counters_early[id] = 0
|
|
|
|
counters_late[id] = 0
|
2024-02-14 19:22:56 +00:00
|
|
|
|
|
|
|
for order in request.app.ctx.om.cache.values():
|
|
|
|
if(order.daily):
|
|
|
|
daily += 1
|
|
|
|
else:
|
2024-03-12 20:35:52 +00:00
|
|
|
# Order.status must reflect the one in the Order() constructor inside ext.py
|
|
|
|
if(order.status in ["pending", "paid"] and hasattr(order, "bed_in_room") and order.bed_in_room in counters):
|
|
|
|
counters[order.bed_in_room] += 1
|
|
|
|
if(order.has_early):
|
|
|
|
counters_early[order.bed_in_room] += 1
|
|
|
|
if(order.has_late):
|
|
|
|
counters_late[order.bed_in_room] += 1
|
2024-02-14 19:22:56 +00:00
|
|
|
|
|
|
|
for id, count in counters.items():
|
2024-02-27 18:53:07 +00:00
|
|
|
out.append(f'webint_order_room_counter{{days="normal", label="{ROOM_TYPE_NAMES[id]}"}} {count}')
|
|
|
|
for id, count in counters_early.items():
|
|
|
|
out.append(f'webint_order_room_counter{{days="early", label="{ROOM_TYPE_NAMES[id]}"}} {count}')
|
|
|
|
for id, count in counters_late.items():
|
|
|
|
out.append(f'webint_order_room_counter{{days="late", label="{ROOM_TYPE_NAMES[id]}"}} {count}')
|
2024-02-14 19:22:56 +00:00
|
|
|
out.append(f'webint_order_room_counter{{label="Daily"}} {daily}')
|
|
|
|
|
|
|
|
except Exception as e:
|
2024-03-12 20:35:52 +00:00
|
|
|
print(traceback.format_exc())
|
|
|
|
|
2024-02-14 19:22:56 +00:00
|
|
|
logger.warning("Error in loading metrics rooms")
|
|
|
|
return "\n".join(out)
|
|
|
|
|
|
|
|
class MetricsFilter(logging.Filter):
|
|
|
|
def filter(self, record : LogRecord):
|
|
|
|
return not (record.request.endswith("/manage/metrics") and record.status == 200)
|