Skip to content

Commit

Permalink
feat($trace): support function calling trace with SQLite persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Sep 3, 2022
1 parent db46b14 commit 3b3b57a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions python_boilerplate/common/trace.py
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
26 changes: 26 additions & 0 deletions python_boilerplate/repository/model/trace_log.py
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)

0 comments on commit 3b3b57a

Please sign in to comment.