Skip to content

Commit

Permalink
exception logging on all endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertRossJoh committed Feb 10, 2025
1 parent f6b4955 commit 0e8b801
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 40 deletions.
16 changes: 0 additions & 16 deletions flask_monitoringdashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,6 @@ def bind(app, schedule=True, include_dashboard=True):
# register the blueprint to the app
app.register_blueprint(blueprint, url_prefix='/' + config.link)

# intercepts exceptions for dashboard purposes
def rec_strace(tb):
s = f"Endpoint: {tb.tb_frame.f_code.co_name} at line number: {tb.tb_lineno} in file: {tb.tb_frame.f_code.co_filename}\n"
if tb.tb_next is None:
return s
return rec_strace(tb.tb_next)+s

def exc_intercept():
old_print_exception = traceback.print_exception
def exc_log(etype, value, tb, limit=None, file=None):
print("åååhh neeej ikke igen")
print(rec_strace(tb))
old_print_exception(etype, value, tb, limit, file)
traceback.print_exception = exc_log
exc_intercept()

# flush cache to db before shutdown
import atexit
from flask_monitoringdashboard.core.cache import flush_cache
Expand Down
4 changes: 2 additions & 2 deletions flask_monitoringdashboard/core/exception_logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import traceback
import linecache
from types import TracebackType
from flask_monitoringdashboard.core.types import ExcInfo
from flask_monitoringdashboard.database import CodeLine
from flask_monitoringdashboard.database.exception_info import add_exception_stack_line, add_exception_info

Expand All @@ -14,13 +13,14 @@ def create_codeline(fs: traceback.FrameSummary):
return c_line

class ExceptionLogger():
def __init__(self, exc_info: ExcInfo):
def __init__(self, exc_info):
self.type : type[BaseException] = exc_info[0]
self.value : BaseException = exc_info[1]
self.tb : TracebackType = exc_info[2]

def log(self, request_id: int, session):
add_exception_info(session, request_id, str(self.type.__name__), str(self.value))
print(f"hit request_id: {request_id}")
for idx, fs in enumerate(traceback.extract_tb(self.tb)[1:]):
c_line = create_codeline(fs)
add_exception_stack_line(session, request_id, idx, c_line)
Expand Down
13 changes: 5 additions & 8 deletions flask_monitoringdashboard/core/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import time
from functools import wraps
import traceback
from typing import Any, cast, TYPE_CHECKING
from flask_monitoringdashboard.core.exception_logger import ExceptionLogger
from flask_monitoringdashboard.core.types import ExcInfo, OptExcInfo
from werkzeug.exceptions import HTTPException

from flask_monitoringdashboard import config
Expand Down Expand Up @@ -106,9 +104,7 @@ def status_code_from_response(result) -> int:
def ptb(tb):
fsl : list[traceback.FrameSummary] = traceback.extract_tb(tb)
sl : list[traceback.FrameSummary] = traceback.extract_stack()
print("hitttttttttttttttttttttt")
print_fs(sl)
print("hitttttttttttttttttttttt")
print_fs(fsl)

def evaluate(route_handler, args, kwargs):
Expand All @@ -126,13 +122,14 @@ def evaluate(route_handler, args, kwargs):

return result, status_code, None
except HTTPException as e:
exc_info : OptExcInfo = sys.exc_info()
exc_info = sys.exc_info()
return None, e.code, (ExceptionLogger(exc_info) if exc_info[0] is not None else None)
except Exception as _:
except (Exception, BaseException) as _:
exc_info = sys.exc_info()
return None, 500, (ExceptionLogger(exc_info) if exc_info[0] is not None else None)



def add_wrapper1(endpoint, fun):
@wraps(fun)
def wrapper(*args, **kwargs):
Expand Down Expand Up @@ -161,7 +158,7 @@ def wrapper(*args, **kwargs):
result, status_code, e_logger = evaluate(fun, args, kwargs)

duration = time.time() - start_time
outlier.stop(duration, status_code)
outlier.stop(duration, status_code, e_logger)

if e_logger:
raise e_logger.value
Expand All @@ -181,7 +178,7 @@ def wrapper(*args, **kwargs):
result, status_code, e_logger = evaluate(fun, args, kwargs)

duration = time.time() - start_time
thread.stop(duration, status_code)
thread.stop(duration, status_code, e_logger)

#if raised_exception:
# raise raised_exception
Expand Down
5 changes: 3 additions & 2 deletions flask_monitoringdashboard/core/profiler/outlier_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def run(self):
self._cpu_percent = str(psutil.cpu_percent(interval=None, percpu=True))
self._memory = str(psutil.virtual_memory())

def stop(self, duration, status_code, e_logger : ExceptionLogger):
def stop(self, duration, status_code, e_logger : ExceptionLogger | None):
self._exit.set()
update_duration_cache(endpoint_name=self._endpoint.name, duration=duration * 1000)
with session_scope() as session:
Expand All @@ -70,7 +70,8 @@ def stop(self, duration, status_code, e_logger : ExceptionLogger):
group_by=self._group_by,
status_code=status_code,
)
e_logger.log(request_id, session)
if e_logger is not None:
e_logger.log(request_id, session)
if self._memory:
add_outlier(
session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def __init__(self, endpoint, ip, duration, group_by, e_logger, status_code=200):
self._endpoint = endpoint
self._group_by = group_by
self._status_code = status_code
self.e_logger = e_logger
self.e_logger : ExceptionLogger | None = e_logger

def run(self):
update_duration_cache(endpoint_name=self._endpoint.name, duration=self._duration)
with session_scope() as session:
rid = add_request(
request_id = add_request(
session,
duration=self._duration,
endpoint_id=self._endpoint.id,
Expand All @@ -32,4 +32,4 @@ def run(self):
status_code=self._status_code,
)
if self.e_logger is not None:
self.e_logger.log(rid, session)
self.e_logger.log(request_id, session)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import time
import traceback
from collections import defaultdict

from flask_monitoringdashboard import config
from flask_monitoringdashboard.core.cache import update_duration_cache
from flask_monitoringdashboard.core.exception_logger import ExceptionLogger
from flask_monitoringdashboard.core.logger import log
from flask_monitoringdashboard.core.profiler.util import order_histogram
from flask_monitoringdashboard.core.profiler.util.path_hash import PathHash
Expand Down Expand Up @@ -38,6 +38,7 @@ def __init__(self, thread_to_monitor, endpoint, ip, group_by, outlier_profiler=N
self._total = 0
self._outlier_profiler = outlier_profiler
self._status_code = 404
self.e_logger : ExceptionLogger | None = None

def run(self):
"""
Expand Down Expand Up @@ -79,12 +80,13 @@ def run(self):

self._on_thread_stopped()

def stop(self, duration, status_code):
def stop(self, duration, status_code, e_logger : ExceptionLogger | None):
self._duration = duration * 1000
self._status_code = status_code
if self._outlier_profiler:
self._outlier_profiler.stop_by_profiler()
self._keeprunning = False
self.e_logger = e_logger

def _on_thread_stopped(self):
update_duration_cache(endpoint_name=self._endpoint.name, duration=self._duration)
Expand All @@ -99,6 +101,8 @@ def _on_thread_stopped(self):
)
self._lines_body = order_histogram(self._histogram.items())
self.insert_lines_db(session, request_id)
if self.e_logger is not None:
self.e_logger.log(request_id, session)
if self._outlier_profiler:
self._outlier_profiler.add_outlier(session, request_id)

Expand Down
7 changes: 0 additions & 7 deletions flask_monitoringdashboard/core/types.py

This file was deleted.

0 comments on commit 0e8b801

Please sign in to comment.