Skip to content

Commit

Permalink
[OPIK-868] bug async generators do not club spans under a single trace (
Browse files Browse the repository at this point in the history
#1265)

* Draft implementation of new generator function tracking support

* Move temporary_context contextmanager to context_storage

* Improve type hints

* Update tracker outputs unit tests

* Remove capture_output=True from track decorator unit tests

* Refactor in base track decorator

* Fix typing

* Add docstring for _decorate

* Update docstring for _decorate

* Update default generations aggregation method from str() to concatenation of stringified items.

* Fix lint errors

* Fix docstring
  • Loading branch information
alexkuzmik authored Feb 13, 2025
1 parent 272e3c2 commit 5b42062
Show file tree
Hide file tree
Showing 14 changed files with 692 additions and 284 deletions.
25 changes: 24 additions & 1 deletion sdks/python/src/opik/context_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextvars
import contextlib

from typing import List, Optional
from typing import List, Optional, Generator
from opik.api_objects import span, trace

_current_trace_data_context: contextvars.ContextVar[Optional[trace.TraceData]] = (
Expand Down Expand Up @@ -76,3 +77,25 @@ def set_trace_data(trace: Optional[trace.TraceData]) -> None:
def clear_all() -> None:
_current_trace_data_context.set(None)
_spans_data_stack_context.set([])


@contextlib.contextmanager
def temporary_context(
span_data: span.SpanData, trace_data: Optional[trace.TraceData]
) -> Generator[None, None, None]:
"""
Temporary adds span and trace data to the context.
If trace_data is None, it has no effect on the current trace in the context.
"""
try:
original_trace = get_trace_data()

if trace_data is not None:
set_trace_data(trace=trace_data)

add_span_data(span_data)

yield
finally:
set_trace_data(original_trace)
pop_span_data()
Loading

0 comments on commit 5b42062

Please sign in to comment.