Improve log_metric for use in Notebooks
This commit is contained in:
parent
58286f6f1c
commit
e717234f66
1 changed files with 11 additions and 4 deletions
|
|
@ -5,7 +5,7 @@ from ..context import get_context
|
||||||
from ..tracing import TracingContext
|
from ..tracing import TracingContext
|
||||||
|
|
||||||
|
|
||||||
def log_metric(argument_name: str, value: Any) -> None:
|
def log_metric(argument_name: str, value: Any, disable_logging: bool=False) -> None:
|
||||||
"""Log a key (argument_name)-value pair that is persisted inside the trace.
|
"""Log a key (argument_name)-value pair that is persisted inside the trace.
|
||||||
|
|
||||||
The name of the function from where this is called is also stored.
|
The name of the function from where this is called is also stored.
|
||||||
|
|
@ -13,12 +13,19 @@ def log_metric(argument_name: str, value: Any) -> None:
|
||||||
Args:
|
Args:
|
||||||
argument_name: The key for storing the value.
|
argument_name: The key for storing the value.
|
||||||
value: Value to log. Must be JSON-serialisable.
|
value: Value to log. Must be JSON-serialisable.
|
||||||
|
disable_logging: If True, only persist in trace but don't show in console
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tracing_context = TracingContext.get_current_tracing_context()
|
tracing_context = TracingContext.get_current_tracing_context()
|
||||||
caller = inspect.stack()[1].function
|
try:
|
||||||
actual_name = f"metric:{caller}:{argument_name}"
|
caller = inspect.stack()[1].function
|
||||||
|
actual_name = f"metric:{caller}:{argument_name}"
|
||||||
|
except:
|
||||||
|
# inspect might not work in notebooks
|
||||||
|
actual_name = f"metric:{argument_name}"
|
||||||
|
|
||||||
if tracing_context:
|
if tracing_context:
|
||||||
tracing_context.log_value(name=actual_name, value=value)
|
tracing_context.log_value(name=actual_name, value=value)
|
||||||
|
|
||||||
get_context().logger.info(f"{actual_name}={value}")
|
if not disable_logging:
|
||||||
|
get_context().logger.info(f"{actual_name}={value}")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue