-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6615c6d
commit bc44d23
Showing
7 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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
Empty file.
This file contains 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 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,3 @@ | ||
from burr.visibility.tracing import TracerFactory as Tracer | ||
|
||
__all__ = ["Tracer"] |
This file contains 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,100 @@ | ||
import dataclasses | ||
import uuid | ||
from contextlib import AbstractAsyncContextManager, AbstractContextManager | ||
from contextvars import ContextVar | ||
from typing import Any, List, Optional, Type | ||
|
||
from burr.lifecycle.internal import LifecycleAdapterSet | ||
|
||
|
||
@dataclasses.dataclass | ||
class ActionSpan: | ||
action: str | ||
name: str | ||
id: str | ||
parent: Optional["ActionSpan"] | ||
|
||
@classmethod | ||
def create(cls, action: str, name: str, parent: Optional["ActionSpan"]) -> "ActionSpan": | ||
return ActionSpan(action=action, name=name, id=create_span_id(), parent=parent) | ||
|
||
|
||
execution_context_var: ContextVar[Optional[ActionSpan]] = ContextVar( | ||
"execution_context", | ||
default=None, | ||
) | ||
|
||
|
||
def create_span_id() -> str: | ||
return f"span_{str(uuid.uuid4())}" | ||
|
||
|
||
class ActionSpanTracer(AbstractContextManager, AbstractAsyncContextManager): | ||
"""Context manager for use within tracing actions""" | ||
|
||
def __init__( | ||
self, | ||
action: str, | ||
span_name: str, | ||
lifecycle_adapters: LifecycleAdapterSet, | ||
span_dependencies: List[str], | ||
): | ||
self.action = action | ||
self.lifecycle_adapters = lifecycle_adapters | ||
self.span_name = span_name | ||
self.span_dependencies = span_dependencies | ||
|
||
async def __aexit__( | ||
self, | ||
__exc_type: Type[BaseException], | ||
__exc_value: Optional[Type[BaseException]], | ||
__traceback: Optional[Type[BaseException]], | ||
) -> Optional[bool]: | ||
raise NotImplementedError | ||
|
||
def __enter__(self): | ||
current_execution_context = execution_context_var.get() | ||
execution_context_var.set( | ||
ActionSpan.create( | ||
action=self.action, name=self.span_name, parent=current_execution_context | ||
) | ||
) | ||
|
||
def __exit__( | ||
self, | ||
__exc_type: Type[BaseException], | ||
__exc_value: Optional[Type[BaseException]], | ||
__traceback: Optional[Type[BaseException]], | ||
) -> Optional[bool]: | ||
"""Raise any exception triggered within the runtime context.""" | ||
current_execution_context = execution_context_var.get() | ||
execution_context_var.set(current_execution_context.parent) | ||
return None | ||
|
||
def log_artifact(self, name: str, value: Any): | ||
raise NotImplementedError | ||
|
||
async def __aenter__(self) -> "ActionSpanTracer": | ||
raise NotImplementedError | ||
|
||
|
||
class TracerFactory: | ||
def __init__(self, action: str, lifecycle_adapters: LifecycleAdapterSet): | ||
self.action = action | ||
self.lifecycle_adapters = lifecycle_adapters | ||
|
||
def __call__( | ||
self, span_name: str, span_dependencies: Optional[List[str]] = None | ||
) -> ActionSpanTracer: | ||
return ActionSpanTracer( | ||
action=self.action, | ||
span_name=span_name, | ||
lifecycle_adapters=self.lifecycle_adapters, | ||
span_dependencies=span_dependencies, | ||
) | ||
|
||
|
||
# with TracerFactory("action", "span", lifecycle.LifecycleAdapterSet()) as tracer: | ||
# with tracer("span", ["edge"]) as span: | ||
# pass | ||
# pass |
This file contains 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