-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add Agno Workflow tracing support #46
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4d44770
Add Agno Workflow tracing support
Innex a2e0a60
Make workflow import optional to avoid fastapi dependency
Innex bd54976
feat: Improvements to agno workflows PR
AbhiPrasad 5db2a8c
some test cleanup + better workflow spans
AbhiPrasad 017e112
refactor fixtures
AbhiPrasad 8e810f9
remove conftest
AbhiPrasad 20cda66
test clobbering
AbhiPrasad 4bf2953
one more bug around span output
AbhiPrasad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import time | ||
| from typing import Any | ||
|
|
||
| from braintrust.logger import start_span | ||
| from braintrust.span_types import SpanTypeAttribute | ||
| from wrapt import wrap_function_wrapper | ||
|
|
||
| from .utils import ( | ||
| _aggregate_agent_chunks, | ||
| _try_to_dict, | ||
| extract_metadata, | ||
| extract_metrics, | ||
| extract_streaming_metrics, | ||
| is_patched, | ||
| mark_patched, | ||
| ) | ||
|
|
||
|
|
||
| def _extract_workflow_input(args: Any, kwargs: Any) -> dict[str, Any]: | ||
| """Extract the input from _execute parameters. | ||
|
|
||
| _execute signature: (self, session, execution_input, workflow_run_response, run_context, ...) | ||
| - args[0]: session (WorkflowSession) | ||
| - args[1]: execution_input (WorkflowExecutionInput) - contains .input | ||
| - args[2]: workflow_run_response (WorkflowRunOutput) - contains .input, accumulates results | ||
| """ | ||
| execution_input = args[1] if len(args) > 1 else kwargs.get("execution_input") | ||
| workflow_run_response = args[2] if len(args) > 2 else kwargs.get("workflow_run_response") | ||
|
|
||
| result: dict[str, Any] = {} | ||
|
|
||
| if execution_input: | ||
| if hasattr(execution_input, "input"): | ||
| result["input"] = execution_input.input | ||
| result["execution_input"] = _try_to_dict(execution_input) | ||
|
|
||
| if workflow_run_response: | ||
| result["run_response"] = _try_to_dict(workflow_run_response) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def wrap_workflow(Workflow: Any) -> Any: | ||
| if is_patched(Workflow): | ||
| return Workflow | ||
|
|
||
| def execute_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any): | ||
| workflow_name = getattr(instance, "name", None) or "Workflow" | ||
| span_name = f"{workflow_name}.run" | ||
|
|
||
| input_data = _extract_workflow_input(args, kwargs) | ||
|
|
||
| with start_span( | ||
| name=span_name, | ||
| type=SpanTypeAttribute.TASK, | ||
| input=input_data, | ||
| metadata=extract_metadata(instance, "workflow"), | ||
| ) as span: | ||
| result = wrapped(*args, **kwargs) | ||
| span.log( | ||
| output=result, | ||
| metrics=extract_metrics(result), | ||
| ) | ||
| return result | ||
|
|
||
| if hasattr(Workflow, "_execute"): | ||
| wrap_function_wrapper(Workflow, "_execute", execute_wrapper) | ||
|
|
||
| def execute_stream_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any): | ||
| workflow_name = getattr(instance, "name", None) or "Workflow" | ||
| span_name = f"{workflow_name}.run_stream" | ||
|
|
||
| input_data = _extract_workflow_input(args, kwargs) | ||
|
|
||
| def _trace_stream(): | ||
| start = time.time() | ||
| span = start_span( | ||
| name=span_name, | ||
| type=SpanTypeAttribute.TASK, | ||
| input=input_data, | ||
| metadata=extract_metadata(instance, "workflow"), | ||
| ) | ||
| span.set_current() | ||
|
|
||
| should_unset = True | ||
| try: | ||
| first = True | ||
| all_chunks = [] | ||
|
|
||
| for chunk in wrapped(*args, **kwargs): | ||
| if first: | ||
| span.log( | ||
| metrics={ | ||
| "time_to_first_token": time.time() - start, | ||
| } | ||
| ) | ||
| first = False | ||
| all_chunks.append(chunk) | ||
| yield chunk | ||
|
|
||
| aggregated = _aggregate_agent_chunks(all_chunks) | ||
|
|
||
| span.log( | ||
| output=aggregated, | ||
| metrics=extract_streaming_metrics(aggregated, start), | ||
| ) | ||
| except GeneratorExit: | ||
| should_unset = False | ||
| raise | ||
| except Exception as e: | ||
| span.log( | ||
| error=str(e), | ||
| ) | ||
| raise | ||
| finally: | ||
| if should_unset: | ||
| span.unset_current() | ||
| span.end() | ||
|
|
||
| return _trace_stream() | ||
|
|
||
| if hasattr(Workflow, "_execute_stream"): | ||
| wrap_function_wrapper(Workflow, "_execute_stream", execute_stream_wrapper) | ||
|
|
||
| async def aexecute_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any): | ||
| workflow_name = getattr(instance, "name", None) or "Workflow" | ||
| span_name = f"{workflow_name}.arun" | ||
|
|
||
| input_data = _extract_workflow_input(args, kwargs) | ||
|
|
||
| with start_span( | ||
| name=span_name, | ||
| type=SpanTypeAttribute.TASK, | ||
| input=input_data, | ||
| metadata=extract_metadata(instance, "workflow"), | ||
| ) as span: | ||
| result = await wrapped(*args, **kwargs) | ||
| span.log( | ||
| output=result, | ||
| metrics=extract_metrics(result), | ||
| ) | ||
| return result | ||
|
|
||
| if hasattr(Workflow, "_aexecute"): | ||
| wrap_function_wrapper(Workflow, "_aexecute", aexecute_wrapper) | ||
|
|
||
| def aexecute_stream_wrapper(wrapped: Any, instance: Any, args: Any, kwargs: Any): | ||
| workflow_name = getattr(instance, "name", None) or "Workflow" | ||
| span_name = f"{workflow_name}.arun_stream" | ||
|
|
||
| input_data = _extract_workflow_input(args, kwargs) | ||
|
|
||
| async def _trace_stream(): | ||
| start = time.time() | ||
| span = start_span( | ||
| name=span_name, | ||
| type=SpanTypeAttribute.TASK, | ||
| input=input_data, | ||
| metadata=extract_metadata(instance, "workflow"), | ||
| ) | ||
| span.set_current() | ||
|
|
||
| should_unset = True | ||
| try: | ||
| first = True | ||
| all_chunks = [] | ||
|
|
||
| async for chunk in wrapped(*args, **kwargs): | ||
| if first: | ||
| span.log( | ||
| metrics={ | ||
| "time_to_first_token": time.time() - start, | ||
| } | ||
| ) | ||
| first = False | ||
| all_chunks.append(chunk) | ||
| yield chunk | ||
|
|
||
| aggregated = _aggregate_agent_chunks(all_chunks) | ||
|
|
||
| span.log( | ||
| output=aggregated, | ||
| metrics=extract_streaming_metrics(aggregated, start), | ||
| ) | ||
| except GeneratorExit: | ||
| should_unset = False | ||
| raise | ||
| except Exception as e: | ||
| span.log( | ||
| error=str(e), | ||
| ) | ||
| raise | ||
| finally: | ||
| if should_unset: | ||
| span.unset_current() | ||
| span.end() | ||
|
|
||
| return _trace_stream() | ||
|
|
||
| if hasattr(Workflow, "_aexecute_stream"): | ||
| wrap_function_wrapper(Workflow, "_aexecute_stream", aexecute_stream_wrapper) | ||
|
|
||
| mark_patched(Workflow) | ||
| return Workflow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm this should generate a new vcr cassette. I seem to have messed something up with my CI changes. I will push something up to this PR to fix that!