Skip to content

Commit c22793f

Browse files
committed
better dx
1 parent fe10ee7 commit c22793f

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

py/src/braintrust/logger.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,20 +3687,10 @@ def _start_span_parent_args(
36873687
f"Mismatch between expected span parent object type {parent_object_type} and provided type {parent_components.object_type}"
36883688
)
36893689

3690-
parent_components_object_id_lambda = _span_components_to_object_id_lambda(parent_components)
3691-
3692-
if parent_object_id_override is None:
3693-
3694-
def compute_parent_object_id():
3695-
parent_components_object_id = parent_components_object_id_lambda()
3696-
assert parent_object_id.get() == parent_components_object_id, (
3697-
f"Mismatch between expected span parent object id {parent_object_id.get()} and provided id {parent_components_object_id}"
3698-
)
3699-
return parent_object_id.get()
3700-
3701-
arg_parent_object_id = LazyValue(compute_parent_object_id, use_mutex=False)
3702-
else:
3690+
if parent_object_id_override is not None:
37033691
arg_parent_object_id = LazyValue(lambda: parent_object_id_override, use_mutex=False)
3692+
else:
3693+
arg_parent_object_id = parent_object_id
37043694
if parent_components.row_id:
37053695
arg_parent_span_ids = ParentSpanIds(
37063696
span_id=parent_components.span_id, root_span_id=parent_components.root_span_id

py/src/braintrust/test_logger.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,27 @@ def test_parent_precedence_explicit_parent_with_project_override(with_memory_log
26012601
assert outer_span_id in (forced_log.get("span_parents") or [])
26022602

26032603

2604+
def test_logger_start_span_with_exported_parent_uses_receiver_project(with_memory_logger):
2605+
primary_logger = init_test_logger("test-project-a")
2606+
secondary_logger = init_test_logger("test-project-b")
2607+
2608+
with primary_logger.start_span(name="exported_parent") as exported_parent:
2609+
exported_parent.log(input="parent")
2610+
exported_parent_export = exported_parent.export()
2611+
exported_parent_span_id = exported_parent.span_id
2612+
exported_parent_root_span_id = exported_parent.root_span_id
2613+
2614+
with secondary_logger.start_span(name="child", parent=exported_parent_export) as child:
2615+
child.log(input="test")
2616+
2617+
logs = with_memory_logger.pop()
2618+
child_log = next(l for l in logs if l.get("span_attributes", {}).get("name") == "child")
2619+
2620+
assert child_log["project_id"] == "test-project-b"
2621+
assert child_log["root_span_id"] == exported_parent_root_span_id
2622+
assert exported_parent_span_id in (child_log.get("span_parents") or [])
2623+
2624+
26042625
def test_parent_object_id_override_preserves_object_type_checks(with_memory_logger, with_simulate_login):
26052626
"""Test that overriding project id does not bypass parent object type validation."""
26062627
test_logger = init_test_logger("test-logger")
@@ -2894,22 +2915,21 @@ def test_span_start_span_with_exported_span_parent(with_memory_logger):
28942915
)
28952916

28962917

2897-
def test_span_start_span_with_exported_span_parent_and_project_override(with_memory_logger):
2898-
"""Test that span.start_span() supports overriding the project id with an exported parent."""
2899-
test_logger = init_test_logger("test-project-a")
2918+
def test_span_start_span_with_exported_span_parent_uses_receiver_project(with_memory_logger):
2919+
primary_logger = init_test_logger("test-project-a")
2920+
secondary_logger = init_test_logger("test-project-b")
29002921

2901-
with test_logger.start_span(name="exported_parent") as exported_parent:
2922+
with primary_logger.start_span(name="exported_parent") as exported_parent:
29022923
exported_parent.log(input="parent")
29032924
exported_parent_export = exported_parent.export()
29042925
exported_parent_span_id = exported_parent.span_id
29052926
exported_parent_root_span_id = exported_parent.root_span_id
29062927

2907-
with test_logger.start_span(name="active_context") as active_context:
2928+
with secondary_logger.start_span(name="active_context") as active_context:
29082929
active_context_span_id = active_context.span_id
29092930

29102931
with active_context.start_span(
29112932
parent=exported_parent_export,
2912-
parent_object_id="test-project-b",
29132933
name="child",
29142934
) as child:
29152935
child.log(input="test")

0 commit comments

Comments
 (0)