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

[WIP] Fixing instrumentation for workflows #16290

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
55 changes: 48 additions & 7 deletions llama-index-core/llama_index/core/instrumentation/dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio
from functools import partial
from contextlib import contextmanager
from contextvars import ContextVar, Token
from typing import Any, Callable, Generator, List, Optional, Dict, Protocol
Expand Down Expand Up @@ -251,6 +253,7 @@ def wrapper(func: Callable, instance: Any, args: list, kwargs: dict) -> Any:
bound_args = inspect.signature(func).bind(*args, **kwargs)
id_ = f"{func.__qualname__}-{uuid.uuid4()}"
tags = active_instrument_tags.get()
result = None

token = active_span_id.set(id_)
parent_id = None if token.old_value is Token.MISSING else token.old_value
Expand All @@ -261,20 +264,58 @@ def wrapper(func: Callable, instance: Any, args: list, kwargs: dict) -> Any:
parent_id=parent_id,
tags=tags,
)

def handle_future_result(
future: asyncio.Future,
span_id: str,
bound_args: inspect.BoundArguments,
instance: Any,
) -> None:
try:
result = future.result()
self.span_exit(
id_=span_id,
bound_args=bound_args,
instance=instance,
result=result,
)
return result
except BaseException as e:
self.event(SpanDropEvent(span_id=span_id, err_str=str(e)))
self.span_drop(
id_=span_id, bound_args=bound_args, instance=instance, err=e
)
raise
finally:
active_span_id.reset(token)

try:
result = func(*args, **kwargs)
if isinstance(result, asyncio.Future):
# If the result is a Future, wrap it
new_future = asyncio.ensure_future(result)
new_future.add_done_callback(
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes sense to me! Wonder where that Context is being spun up in the arize error you shared

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the quick fix @logan-markewich! Just wanted to callout that the same error occurs with MLflow tracing (while the trace itself is logged correctly).

From my observation, I think the error is because of trying to reset ContextVar by a token that is created in the main thread, while resetting it in the async task - Arize and MLflow works fine nevertheless because neither relies on active_span_id. I think setting active_span_id inside handle_future_result should fix it (and it makes more sense that the async task manages its own active span).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@B-Step62 I don't think I can move the active_span_id into handle_future_result, since its needed for span_enter, and handle_future_result only runs once the span ends 🤔

partial(
handle_future_result,
span_id=id_,
bound_args=bound_args,
instance=instance,
)
)
return new_future
else:
# For non-Future results, proceed as before
self.span_exit(
id_=id_, bound_args=bound_args, instance=instance, result=result
)
return result
except BaseException as e:
self.event(SpanDropEvent(span_id=id_, err_str=str(e)))
self.span_drop(id_=id_, bound_args=bound_args, instance=instance, err=e)
raise
else:
self.span_exit(
id_=id_, bound_args=bound_args, instance=instance, result=result
)
return result
finally:
# clean up
active_span_id.reset(token)
if not isinstance(result, asyncio.Future):
logan-markewich marked this conversation as resolved.
Show resolved Hide resolved
active_span_id.reset(token)

@wrapt.decorator
async def async_wrapper(
Expand Down
Loading