Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds attribute logging to the tracking client

elijahbenizzy552e7801b13a
diff --git a/burr/tracking/client.py b/burr/tracking/client.py
index 517abb6f..f57d7087 100644
--- a/burr/tracking/client.py
+++ b/burr/tracking/client.py
@@ -1,6 +1,8 @@
import abc
import datetime
+from burr.lifecycle.base import DoLogAttributeHook
+
# this is a quick hack to get it to work on windows
# we'll have to implement a proper lock later
# but its better that it works than breaks on import
@@ -45,6 +47,7 @@ from burr.lifecycle import (
from burr.tracking.common.models import (
ApplicationMetadataModel,
ApplicationModel,
+ AttributeModel,
BeginEntryModel,
BeginSpanModel,
ChildApplicationModel,
@@ -95,6 +98,7 @@ class SyncTrackingClient(
PostRunStepHook,
PreStartSpanHook,
PostEndSpanHook,
+ DoLogAttributeHook,
ABC,
):
@abc.abstractmethod
@@ -454,6 +458,26 @@ class LocalTrackingClient(
)
self._append_write_line(end_span_model)
+ def do_log_attributes(
+ self,
+ *,
+ attributes: Dict[str, Any],
+ tags: dict,
+ action: str,
+ action_sequence_id: int,
+ span: Optional["ActionSpan"],
+ **future_kwargs: Any,
+ ):
+ for attribute_name, attribute in attributes.items():
+ attribute_model = AttributeModel(
+ key=attribute_name,
+ action_sequence_id=action_sequence_id,
+ span_id=span.uid if span is not None else None,
+ value=serde.serialize(attribute, **self.serde_kwargs),
+ tags=tags,
+ )
+ self._append_write_line(attribute_model)
+
def __del__(self):
if self.f is not None:
self.f.close()
diff --git a/burr/tracking/common/models.py b/burr/tracking/common/models.py
index e4ed8a4d..4f13c84a 100644
--- a/burr/tracking/common/models.py
+++ b/burr/tracking/common/models.py
@@ -1,5 +1,5 @@
import datetime
-from typing import Any, Dict, List, Literal, Optional
+from typing import Any, Dict, List, Literal, Optional, Union
from burr.common import types as burr_types
from burr.core import Action
@@ -191,3 +191,16 @@ class EndSpanModel(IdentifyingModel):
@property
def sequence_id(self) -> int:
return self.action_sequence_id
+
+
+class AttributeModel(IdentifyingModel):
+ """Represents a logged artifact"""
+
+ key: str
+ action_sequence_id: int
+ span_id: Optional[
+ str
+ ] # It doesn't have to relate to a span, it can be at the level of an action as well
+ value: Union[dict, str, int, float, bool, list, None]
+ tags: Dict[str, str]
+ type: str = "attribute"
diff --git a/burr/tracking/server/backend.py b/burr/tracking/server/backend.py
index 4beccdc0..fed8716f 100644
--- a/burr/tracking/server/backend.py
+++ b/burr/tracking/server/backend.py
@@ -1,9 +1,10 @@
import abc
+import collections
import importlib
import json
import os.path
import sys
-from typing import Any, Optional, Sequence, Type, TypeVar
+from typing import Any, List, Optional, Sequence, Type, TypeVar
import aiofiles
import aiofiles.os as aiofilesos
@@ -13,6 +14,7 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
from burr.tracking.common import models
from burr.tracking.common.models import (
+ AttributeModel,
BeginEntryModel,
BeginSpanModel,
ChildApplicationModel,
@@ -291,6 +293,7 @@ class LocalBackend(BackendBase):
steps_by_sequence_id = {}
spans_by_id = {}
# TODO -- use the Step.from_logs method
+ attributes_by_step: dict[int, List[AttributeModel]] = collections.defaultdict(list)
if os.path.exists(log_file):
async with aiofiles.open(log_file, "rb") as f:
for line in await f.readlines():
@@ -299,7 +302,7 @@ class LocalBackend(BackendBase):
if json_line["type"] == "begin_entry":
begin_step = BeginEntryModel.parse_obj(json_line)
steps_by_sequence_id[begin_step.sequence_id] = schema.Step(
- step_start_log=begin_step, step_end_log=None, spans=[]
+ step_start_log=begin_step, step_end_log=None, spans=[], attributes=[]
)
elif json_line["type"] == "end_entry":
# this assumes they'll be in order
@@ -316,10 +319,18 @@ class LocalBackend(BackendBase):
end_span = EndSpanModel.parse_obj(json_line)
span = spans_by_id[end_span.span_id]
span.end_entry = end_span
+ elif json_line["type"] == "attribute":
+ attribute = models.AttributeModel.parse_obj(json_line)
+ attributes_by_step[attribute.action_sequence_id].append(attribute)
for span in spans_by_id.values():
# They should have one, the other, or both set
step = steps_by_sequence_id[span.begin_entry.action_sequence_id]
step.spans.append(span)
+
+ for action_sequence_id, attributes in attributes_by_step.items():
+ step = steps_by_sequence_id[action_sequence_id]
+ step.attributes = attributes
+
children = []
if os.path.exists(children_file):
async with aiofiles.open(children_file) as f:
diff --git a/burr/tracking/server/schema.py b/burr/tracking/server/schema.py
index c707c99f..588f74d7 100644
--- a/burr/tracking/server/schema.py
+++ b/burr/tracking/server/schema.py
@@ -7,6 +7,7 @@ from pydantic import fields
from burr.tracking.common.models import (
ApplicationModel,
+ AttributeModel,
BeginEntryModel,
BeginSpanModel,
ChildApplicationModel,
@@ -68,6 +69,7 @@ class Step(pydantic.BaseModel):
step_start_log: BeginEntryModel
step_end_log: Optional[EndEntryModel]
spans: List[Span]
+ attributes: List[AttributeModel]
@staticmethod
def from_logs(log_lines: List[bytes]) -> List["Step"]: