Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(weave): Add attributes to Call object and allow explicit root #1739

Merged
merged 15 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions weave/tests/test_client_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def my_op(a: int) -> int:
exception=None,
output=6,
summary={},
attributes={},
)


Expand Down
3 changes: 3 additions & 0 deletions weave/tests/test_weave_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def test_call_create(client):
exception=None,
summary={},
_children=[],
attributes={},
)
assert dataclasses.asdict(result._val) == dataclasses.asdict(expected)

Expand All @@ -227,6 +228,7 @@ def test_calls_query(client):
parent_id=None,
inputs={"a": 5, "b": 10},
id=call0.id,
attributes={},
)
assert result[1] == weave_client.Call(
op_name="weave:///shawn/test-project/op/x:tzUhDyzVm5bqQsuqh5RT4axEXSosyLIYZn9zbRyenaw",
Expand All @@ -235,6 +237,7 @@ def test_calls_query(client):
parent_id=call0.id,
inputs={"a": 6, "b": 11},
id=call1.id,
attributes={},
)
client.finish_call(call2, None)
client.finish_call(call1, None)
Expand Down
30 changes: 25 additions & 5 deletions weave/weave_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class Call:
output: Any = None
exception: Optional[str] = None
summary: Optional[dict] = None
attributes: Optional[dict] = None
# These are the live children during logging
_children: list["Call"] = dataclasses.field(default_factory=list)

Expand Down Expand Up @@ -227,6 +228,7 @@ def make_client_call(
inputs=from_json(server_call.inputs, server_call.project_id, server),
output=output,
summary=server_call.summary,
attributes=server_call.attributes,
)
if call.id is None:
raise ValueError("Call ID is None")
Expand Down Expand Up @@ -270,7 +272,6 @@ def __init__(
self.project = project
self.server = server
self._anonymous_ops: dict[str, Op] = {}
self.ensure_project_exists = ensure_project_exists

if ensure_project_exists:
self.server.ensure_project_exists(entity, project)
Expand Down Expand Up @@ -442,6 +443,28 @@ def create_call(
inputs: dict,
attributes: dict = {},
) -> Call:
if parent is None:
parent = run_context.get_current_run()
call = self.create_call_outside_execution(op, parent, inputs, attributes)
run_context.push_call(call)
return call

@trace_sentry.global_trace_sentry.watch()
def create_call_outside_execution(
self,
op: Union[str, Op],
parent: Optional[Call],
inputs: dict,
attributes: dict = {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should avoid using a mutable object as a default value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

) -> Call:
"""
Create a call without inferring the parent from the current run context,
or pushing the call onto the run context stack. This is useful in situations
where the information regarding the call is delivered outside of the normal
execution flow. For example, in a callback function. In such cases we still
want to log the call, but don't want this logging to interfere with the
normal stack context.
"""
if isinstance(op, str):
if op not in self._anonymous_ops:
self._anonymous_ops[op] = _build_anonymous_op(op)
Expand All @@ -456,9 +479,6 @@ def create_call(
inputs_with_refs = map_to_refs(inputs)
call_id = generate_id()

if parent is None:
parent = run_context.get_current_run()

if parent:
trace_id = parent.trace_id
parent_id = parent.id
Expand All @@ -472,6 +492,7 @@ def create_call(
parent_id=parent_id,
id=call_id,
inputs=inputs_with_refs,
attributes=attributes,
)
if parent is not None:
parent._children.append(call)
Expand All @@ -490,7 +511,6 @@ def create_call(
wb_run_id=current_wb_run_id,
)
self.server.call_start(CallStartReq(start=start))
run_context.push_call(call)
return call

@trace_sentry.global_trace_sentry.watch()
Expand Down
Loading