Add log_argument
Signed-off-by: András Schmelczer <andras@schmelczer.dev>
This commit is contained in:
parent
a03e78bb73
commit
85a06096bf
1 changed files with 330 additions and 56 deletions
26
good_ai/src/good_ai/good_ai/metrics/log_metric.py
Normal file
26
good_ai/src/good_ai/good_ai/metrics/log_metric.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from functools import wraps
|
||||
from typing import Any, Callable, Dict, List
|
||||
|
||||
from ..helper import filter_args, get_args
|
||||
from ..tracing import TracingContext
|
||||
|
||||
|
||||
def log_metric(
|
||||
argument_name: str, *, calculate: Callable[[Any], bool] = lambda _: True
|
||||
) -> Callable[..., Any]:
|
||||
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||
actual_name = f"{func.__name__}:{argument_name}"
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(*args: List[Any], **kwargs: Dict[str, Any]) -> Any:
|
||||
arguments = get_args(func, args, kwargs)
|
||||
metric = calculate(**filter_args(arguments, calculate))
|
||||
|
||||
context = TracingContext.get_current_context()
|
||||
if context:
|
||||
context.log_value(name=actual_name, value=metric)
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
Loading…
Reference in a new issue