-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($trace): support function calling trace with SQLite persistence
- Loading branch information
1 parent
db46b14
commit 3b3b57a
Showing
2 changed files
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import inspect | ||
import json | ||
from typing import Callable | ||
|
||
from python_boilerplate.configuration.thread_pool_configuration import executor | ||
from python_boilerplate.repository.model.trace_log import TraceLog | ||
|
||
|
||
def trace(func: Callable): | ||
def wrapped(*arg, **kwarg): | ||
function_arguments = {"arg": arg, "kwarg": kwarg} | ||
trace_log = TraceLog( | ||
called_by=inspect.stack()[1][3], | ||
function_qualified_name=func.__qualname__, | ||
function_arguments=json.dumps( | ||
function_arguments, default=lambda x: x.__dict__ | ||
), | ||
) | ||
executor.submit(lambda x: x.save(), trace_log) | ||
return func(*arg, **kwarg) | ||
|
||
return wrapped |
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,26 @@ | ||
import threading | ||
from datetime import datetime | ||
|
||
from peewee import CharField, DateTimeField, TextField | ||
|
||
from python_boilerplate.common.common_function import get_login_user | ||
from python_boilerplate.common.orm import peewee_table | ||
from python_boilerplate.repository.model.base_model import BaseModel | ||
|
||
|
||
@peewee_table | ||
class TraceLog(BaseModel): | ||
# called_by is inspect.stack()[1][3] | ||
called_by = CharField(max_length=200, null=False, index=True) | ||
function_qualified_name = CharField(max_length=200, null=False, index=True) | ||
function_arguments = TextField(null=True) | ||
thread = CharField( | ||
max_length=100, null=False, default=threading.current_thread().name, index=True | ||
) | ||
start_time = DateTimeField(null=False, default=datetime.now) | ||
|
||
# common 4 fields | ||
created_by = CharField(max_length=50, null=False, default=get_login_user) | ||
created_time = DateTimeField(null=False, default=datetime.now) | ||
modified_by = CharField(max_length=50, null=False, default=get_login_user) | ||
modified_time = DateTimeField(null=False, default=datetime.now) |