-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #471 from AlbertRossJoh/feature/exn-hook
Feature/exn hook
- Loading branch information
Showing
10 changed files
with
197 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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 | ||
|
||
def create_codeline(fs: traceback.FrameSummary): | ||
c_line = CodeLine() | ||
c_line.filename = fs.filename | ||
c_line.line_number = fs.lineno | ||
c_line.function_name = fs.name | ||
c_line.code = linecache.getline(c_line.filename, c_line.line_number).strip() | ||
return c_line | ||
|
||
class ExceptionLogger(): | ||
def __init__(self, exc_info: ExcInfo): | ||
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)) | ||
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
from types import TracebackType | ||
from typing import TypeAlias | ||
|
||
ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType] | ||
OptExcInfo: TypeAlias = ExcInfo | tuple[None, None, None] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Contains all functions that access an ExceptionInfo object. | ||
""" | ||
|
||
from flask_monitoringdashboard.database import CodeLine, ExceptionInfo, ExceptionStackLine | ||
from flask_monitoringdashboard.database.code_line import get_code_line | ||
|
||
def get_exception_info(session, request_id: int): | ||
""" | ||
Retrieve an ExceptionInfo record by request_id. | ||
""" | ||
return session.query(ExceptionInfo).filter_by(request_id=request_id).first() | ||
|
||
|
||
def add_exception_info(session, request_id: int, exception_type: str, exception_msg: str): | ||
""" | ||
Add a new ExceptionInfo record. | ||
""" | ||
exception_info = ExceptionInfo( | ||
request_id=request_id, | ||
exception_type=exception_type, | ||
exception_msg=exception_msg | ||
) | ||
session.add(exception_info) | ||
session.commit() | ||
return exception_info | ||
|
||
def add_exception_stack_line(session, request_id, position, code_line: CodeLine): | ||
""" | ||
Adds a StackLine to the database (and possibly a CodeLine) | ||
:param session: Session for the database | ||
:param request_id: id of the request | ||
:param position: position of the StackLine | ||
:param indent: indent-value | ||
:param duration: duration of this line (in ms) | ||
:param code_line: quadruple that consists of: (filename, line_number, function_name, code) | ||
""" | ||
#fn, ln, name, code = code_line | ||
fn = code_line.filename | ||
ln = code_line.line_number | ||
name = code_line.function_name | ||
code = code_line.code | ||
db_code_line = get_code_line(session, fn, ln, name, code) | ||
session.add( | ||
ExceptionStackLine( | ||
request_id=request_id, | ||
position=position, | ||
code_id=db_code_line.id, | ||
) | ||
) |
Oops, something went wrong.