Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Adds testing for attribute logging
elijahbenizzyaeb6e06deccb
diff --git a/tests/tracking/test_local_tracking_client.py b/tests/tracking/test_local_tracking_client.pyindex cc95ae0b..942d36aa 100644--- a/tests/tracking/test_local_tracking_client.py+++ b/tests/tracking/test_local_tracking_client.py@@ -14,6 +14,7 @@ from burr.tracking.client import _allowed_project_namefrom burr.tracking.common.models import (ApplicationMetadataModel,ApplicationModel,+ AttributeModel,BeginEntryModel,BeginSpanModel,ChildApplicationModel,@@ -25,8 +26,9 @@ from burr.visibility import TracerFactory@action(reads=["counter", "break_at"], writes=["counter"])def counter(state: State, __tracer: TracerFactory) -> Tuple[dict, State]:- with __tracer("increment"):+ with __tracer("increment") as t:result = {"counter": state["counter"] + 1}+ t.log_attributes(counter=result["counter"])if state["break_at"] == result["counter"]:raise ValueError("Broken")return result, state.update(**result)@@ -93,11 +95,15 @@ def test_application_tracks_end_to_end(tmpdir: str):span_end_model = [EndSpanModel.model_validate(line) for line in log_contents if line["type"] == "end_span"]+ attributes = [+ AttributeModel.model_validate(line) for line in log_contents if line["type"] == "attribute"+ ]assert len(pre_run) == 3assert len(post_run) == 3assert len(span_start_model) == 2 # two custom-defined spansassert len(span_end_model) == 2 # dittoassert not any(item.exception for item in post_run)+ assert len(attributes) == 2 # two attributes loggeddef test_application_tracks_end_to_end_broken(tmpdir: str):diff --git a/tests/visibility/test_tracing.py b/tests/visibility/test_tracing.pyindex 4550a452..3d63c210 100644--- a/tests/visibility/test_tracing.py+++ b/tests/visibility/test_tracing.py@@ -1,8 +1,9 @@import asynciofrom contextvars import ContextVar-from typing import Any, Optional+from typing import Any, Dict, Optionalfrom burr.lifecycle.base import (+ DoLogAttributeHook,PostEndSpanHook,PostEndSpanHookAsync,PreStartSpanHook,@@ -360,3 +361,95 @@ async def test_pre_span_lifecycle_hooks_called_async(request):"1:0.1","1:0",]+++async def test_log_attributes_called(request):+ class AttributeHook(DoLogAttributeHook):+ def do_log_attributes(+ self,+ *,+ attributes: Dict[str, Any],+ action: str,+ action_sequence_id: int,+ span: Optional["ActionSpan"],+ tags: dict,+ **future_kwargs: Any,+ ):+ self.artifacts.append(+ (attributes, action, action_sequence_id, span.uid if span is not None else None)+ )++ def __init__(self):+ self.artifacts = []++ hook = AttributeHook()+ adapter_set = LifecycleAdapterSet(hook)+ context_var: ContextVar[Optional[ActionSpan]] = ContextVar(request.node.name, default=None)+ tracer_factory_0 = TracerFactory(+ action="test_action",+ sequence_id=0,+ lifecycle_adapters=adapter_set,+ _context_var=context_var,+ app_id="test_app_id",+ partition_key=None,+ )+ # 0:0+ tracer_factory_0.log_attributes(key="root:0")+ async with tracer_factory_0("0") as t:+ t.log_attributes(key="0:0")+ # 0:0.0+ async with tracer_factory_0("0.0") as t:+ t.log_attributes(key="0.0:0")+ t.log_attributes(key="0.0:1")+ # 0:0.1+ async with tracer_factory_0("0.1") as t:+ t.log_attributes(key="0:0.1")+ # 0:1+ async with tracer_factory_0("1") as t:+ t.log_attributes(key="1:0")+ t.log_attributes(key="1:1")+ # 0:1.0+ async with tracer_factory_0("1.0") as t:+ t.log_attributes(key="1.0:0")+ t.log_attributes(key="1.0:1")+ # 0:1.0.0+ async with tracer_factory_0("1.0.0") as t:+ t.log_attributes(key="1.0.0:0")+ t.log_attributes(key="1.0.0:1")+ # 0:1.0.1+ async with tracer_factory_0("1.0.1") as t:+ t.log_attributes(key="1.0.1:0")+ t.log_attributes(key="1.0.1:1")+ tracer_factory_1 = TracerFactory(+ action="test_action",+ sequence_id=tracer_factory_0.action_sequence_id + 1,+ lifecycle_adapters=adapter_set,+ _context_var=context_var,+ app_id="test_app_id",+ partition_key=None,+ )+ # 1:0+ async with tracer_factory_1("2"):+ # 1:0.0+ async with tracer_factory_1("2.0"):+ pass+ # 1:0.1+ async with tracer_factory_1("2.1"):+ pass++ # order of this is exactly as expected -- in order traversal+ assert hook.artifacts == [+ ({"key": "root:0"}, "test_action", 0, None),+ ({"key": "0:0"}, "test_action", 0, "0:0"),+ ({"key": "0.0:0"}, "test_action", 0, "0:0.0"),+ ({"key": "0.0:1"}, "test_action", 0, "0:0.0"),+ ({"key": "0:0.1"}, "test_action", 0, "0:0.1"),+ ({"key": "1:0"}, "test_action", 0, "0:1"),+ ({"key": "1:1"}, "test_action", 0, "0:1"),+ ({"key": "1.0:0"}, "test_action", 0, "0:1.0"),+ ({"key": "1.0:1"}, "test_action", 0, "0:1.0"),+ ({"key": "1.0.0:0"}, "test_action", 0, "0:1.0.0"),+ ({"key": "1.0.0:1"}, "test_action", 0, "0:1.0.0"),+ ({"key": "1.0.1:0"}, "test_action", 0, "0:1.0.1"),+ ({"key": "1.0.1:1"}, "test_action", 0, "0:1.0.1"),+ ]