Skip to content

Commit

Permalink
WIP: Updated tests for new tracer format
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianSchepersAA committed May 16, 2024
1 parent 16a2f00 commit 733164f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/documentation/classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down
3 changes: 3 additions & 0 deletions src/intelligence_layer/core/tracer/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ def record_output(self, output: PydanticSerializable) -> None:
def end(self, timestamp: Optional[datetime] = None) -> None:
pass

def export_for_viewing(self) -> Sequence[ExportedSpan]:
return []


class JsonSerializer(RootModel[PydanticSerializable]):
root: SerializeAsAny[PydanticSerializable]
Expand Down
16 changes: 4 additions & 12 deletions tests/core/tracer/test_composite_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ def test_composite_tracer(test_task: Task[str, str]) -> None:
trace_1 = tracer_1.export_for_viewing()[0]
trace_2 = tracer_2.export_for_viewing()[0]
assert trace_1.name == trace_2.name
assert trace_1.attributes = trace_2.attributes


def test_composite_tracer_id_consistent_across_children(
file_tracer: FileTracer, test_task: Task[str, str]
) -> None:
input = "input"
tracer1 = InMemoryTracer()

test_task.run(input, CompositeTracer([tracer1]))
assert isinstance(tracer1.entries[0], InMemorySpan)
assert tracer1.entries[0].id() == tracer1.entries[0].entries[0].id()
assert trace_1.attributes == trace_2.attributes
assert trace_1.status == trace_2.status
assert trace_1.context.trace_id != trace_2.context.trace_id
assert trace_1.context.span_id != trace_2.context.span_id
62 changes: 61 additions & 1 deletion tests/core/tracer/test_tracer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import pytest
from pytest import fixture
from pydantic import BaseModel

from intelligence_layer.core import CompositeTracer
from intelligence_layer.core import FileTracer
from intelligence_layer.core import InMemoryTracer
from intelligence_layer.core.tracer.tracer import (
SpanStatus,
SpanType,
Expand All @@ -15,7 +19,14 @@ class DummyObject(BaseModel):
content: str


tracer_fixtures = ["in_memory_tracer", "file_tracer"]
@fixture
def composite_tracer(
in_memory_tracer: InMemoryTracer,
file_tracer: FileTracer
):
return CompositeTracer(in_memory_tracer, file_tracer)

tracer_fixtures = ["in_memory_tracer", "file_tracer", "composite_tracer"]


@pytest.mark.parametrize(
Expand Down Expand Up @@ -155,3 +166,52 @@ def test_tracer_exports_unrelated_spans_correctly(
assert span_2.parent_id is None

assert span_1.context.trace_id != span_2.context.trace_id


@pytest.mark.parametrize(
"tracer_fixture",
tracer_fixtures,
)
def test_tracer_exports_part_of_a_trace_correctly(
tracer_fixture: str,
request: pytest.FixtureRequest,
) -> None:
tracer: Tracer = request.getfixturevalue(tracer_fixture)

with tracer.span("name") as root_span:
child_span = root_span.span("name-2")
child_span.log("test_message", "test_body")

unified_format = child_span.export_for_viewing()

assert len(unified_format) == 2
span_1, span_2 = unified_format[0], unified_format[1]

assert span_1.parent_id is None
assert span_2.parent_id is None

assert span_1.context.trace_id != span_2.context.trace_id


@pytest.mark.parametrize(
"tracer_fixture",
tracer_fixtures,
)
def test_tracer_can_not_log_on_closed_span(
tracer_fixture: str,
request: pytest.FixtureRequest,
) -> None:
tracer: Tracer = request.getfixturevalue(tracer_fixture)

span = tracer.span("name")
with pytest.raises(Exception):
span.log("test_message", "test_body")

with span:
span.log("test_message", "test_body")
with pytest.raises(Exception):
span.log("test_message", "test_body")




0 comments on commit 733164f

Please sign in to comment.