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
cleanup for PR + tests
elijahbenizzye56060b4c7dc
diff --git a/burr/integrations/opentelemetry.py b/burr/integrations/opentelemetry.pyindex 2e799574..f5804722 100644--- a/burr/integrations/opentelemetry.py+++ b/burr/integrations/opentelemetry.py@@ -1,8 +1,9 @@import dataclasses+import jsonimport loggingimport randomfrom contextvars import ContextVar-from typing import Any, Dict, List, Optional, Tuple+from typing import Any, Dict, List, Optional, Sequence, Tuplefrom burr.integrations.base import require_plugin@@ -66,21 +67,21 @@ def get_cached_span(span_id: int) -> Optional[FullSpanContext]:tracker_context = ContextVar[Optional[SyncTrackingClient]]("tracker_context", default=None)-def _is_heterogeneous_array(value: list):+def _is_homogeneous_sequence(value: Sequence):if len(value) == 0:return Truefirst_type = type(value[0])- return any([not isinstance(val, first_type) for val in value])+ return all([isinstance(val, first_type) for val in value])def convert_to_otel_attribute(attr: Any):if isinstance(attr, (str, bool, float, int)):return attr- elif isinstance(attr, list):- if _is_heterogeneous_array(attr):- return attr+ elif isinstance(attr, Sequence):+ if _is_homogeneous_sequence(attr):+ return list(attr)try:- return serde.serialize(attr)+ return json.dumps(serde.serialize(attr))except Exception as e:logger.error(f"Failed to serialize attribute: {attr}, got error: {e}")return str(attr)diff --git a/burr/tracking/server/backend.py b/burr/tracking/server/backend.pyindex f245b66a..d01552c2 100644--- a/burr/tracking/server/backend.py+++ b/burr/tracking/server/backend.py@@ -289,44 +289,6 @@ class LocalBackend(BackendBase):async with aiofiles.open(log_file, "rb") as f:lines = await f.readlines()steps = Step.from_logs(lines)- #- # for line in await f.readlines():- # json_line = safe_json_load(line)- # # TODO -- make these into constants- # 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=[], attributes=[]- # )- # elif json_line["type"] == "end_entry":- # # this assumes they'll be in order- # step_end_log = EndEntryModel.parse_obj(json_line)- # step = steps_by_sequence_id[step_end_log.sequence_id]- # step.step_end_log = step_end_log- # elif json_line["type"] == "begin_span":- # span = BeginSpanModel.parse_obj(json_line)- # spans_by_id[span.span_id] = schema.Span(- # begin_entry=span,- # end_entry=None,- # )- # elif json_line["type"] == "end_span":- # end_span = EndSpanModel.parse_obj(json_line)- # import pdb- # pdb.set_trace()- # 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/pyproject.toml b/pyproject.tomlindex f7328186..b09f2138 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -61,6 +61,7 @@ tests = ["pydantic","pyarrow","redis",+ "burr[opentelemetry]"]documentation = [diff --git a/tests/integrations/test_burr_opentelemetry.py b/tests/integrations/test_burr_opentelemetry.pynew file mode 100644index 00000000..d19ed6ea--- /dev/null+++ b/tests/integrations/test_burr_opentelemetry.py@@ -0,0 +1,28 @@+import json++import pydantic+import pytest++from burr.core import serde+from burr.integrations.opentelemetry import convert_to_otel_attribute+++class TestModel(pydantic.BaseModel):+ foo: int+ bar: bool+++ "value, expected",+ [+ ("hello", "hello"),+ (1, 1),+ ((1, 1), [1, 1]),+ ((1.0, 1.0), [1.0, 1.0]),+ ((True, True), [True, True]),+ (("hello", "hello"), ["hello", "hello"]),+ (TestModel(foo=1, bar=True), json.dumps(serde.serialize(TestModel(foo=1, bar=True)))),+ ],+)+def test_convert_to_otel_attribute(value, expected):+ assert convert_to_otel_attribute(value) == expected