Skip to content

Commit

Permalink
Fix test_trace_ignore_exception_from_tracing_logic test (#12233)
Browse files Browse the repository at this point in the history
Signed-off-by: B-Step62 <yuki.watanabe@databricks.com>
Signed-off-by: Yuki Watanabe <31463517+B-Step62@users.noreply.github.com>
Co-authored-by: Harutaka Kawamura <hkawamura0130@gmail.com>
  • Loading branch information
B-Step62 and harupy committed Jun 4, 2024
1 parent 2c63ccf commit a3f831e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
5 changes: 4 additions & 1 deletion mlflow/tracing/fluent.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def wrapper(*args, **kwargs):

with start_span(name=span_name, span_type=span_type, attributes=attributes) as span:
span.set_attribute(SpanAttributeKey.FUNCTION_NAME, fn.__name__)
span.set_inputs(capture_function_input_args(fn, args, kwargs))
try:
span.set_inputs(capture_function_input_args(fn, args, kwargs))
except Exception:
_logger.warning(f"Failed to capture inputs for function {fn.__name__}.")
result = fn(*args, **kwargs)
span.set_outputs(result)
return result
Expand Down
20 changes: 8 additions & 12 deletions mlflow/tracing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,16 @@


def capture_function_input_args(func, args, kwargs) -> Dict[str, Any]:
try:
# Avoid capturing `self`
func_signature = inspect.signature(func)
bound_arguments = func_signature.bind(*args, **kwargs)
bound_arguments.apply_defaults()
# Avoid capturing `self`
func_signature = inspect.signature(func)
bound_arguments = func_signature.bind(*args, **kwargs)
bound_arguments.apply_defaults()

# Remove `self` from bound arguments if it exists
if bound_arguments.arguments.get("self"):
del bound_arguments.arguments["self"]
# Remove `self` from bound arguments if it exists
if bound_arguments.arguments.get("self"):
del bound_arguments.arguments["self"]

return bound_arguments.arguments
except Exception:
_logger.warning(f"Failed to capture inputs for function {func.__name__}.")
return {}
return bound_arguments.arguments


class TraceJSONEncoder(json.JSONEncoder):
Expand Down
7 changes: 5 additions & 2 deletions tests/tracing/test_fluent.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,15 @@ def predict(self, x, y):
TRACE_BUFFER.clear()

# Exception during inspecting inputs: trace is logged without inputs field
with mock.patch("mlflow.tracing.utils.inspect.signature", side_effect=ValueError("Some error")):
with mock.patch(
"mlflow.tracing.fluent.capture_function_input_args", side_effect=ValueError("Some error")
) as mock_input_args:
output = model.predict(2, 5)
mock_input_args.assert_called_once()

assert output == 7
trace = mlflow.get_last_active_trace()
assert trace.info.request_metadata[TraceMetadataKey.INPUTS] == "{}"
assert trace.info.request_metadata[TraceMetadataKey.INPUTS] == ""
assert trace.info.request_metadata[TraceMetadataKey.OUTPUTS] == "7"
TRACE_BUFFER.clear()

Expand Down

0 comments on commit a3f831e

Please sign in to comment.