Skip to content

Commit 5177e47

Browse files
correctly send inbound span for replay
1 parent f5d60f9 commit 5177e47

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

drift/core/batch_processor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ def _export_batch(self) -> None:
165165
else:
166166
adapter.export_spans(batch) # type: ignore
167167

168-
logger.debug(f"Exported {len(batch)} spans via {adapter.name}")
169-
170168
except Exception as e:
171169
logger.error(f"Failed to export batch via {adapter.name}: {e}")
172170

drift/core/communication/communicator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Any
1212

1313
from tusk.drift.core.v1 import GetMockRequest as ProtoGetMockRequest
14-
14+
from ..span_serialization import clean_span_to_proto
1515
from ...version import MIN_CLI_VERSION, SDK_VERSION
1616
from ..types import CleanSpanData, calling_library_context
1717
from .types import (
@@ -438,7 +438,7 @@ async def send_inbound_span_for_replay(self, span: CleanSpanData) -> None:
438438
if not self._socket:
439439
return
440440

441-
proto_span = span_to_proto(span)
441+
proto_span = clean_span_to_proto(span)
442442

443443
request = SendInboundSpanForReplayRequest(span=proto_span)
444444

drift/core/tracing/td_span_processor.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from ..sampling import should_sample
1717
from ..trace_blocking_manager import TraceBlockingManager, should_block_span
18-
from ..types import TD_INSTRUMENTATION_LIBRARY_NAME, TuskDriftMode
18+
from ..types import TD_INSTRUMENTATION_LIBRARY_NAME, TuskDriftMode, SpanKind as TdSpanKind, replay_trace_id_context
1919
from .otel_converter import otel_span_to_clean_span_data
2020

2121
if TYPE_CHECKING:
@@ -152,9 +152,17 @@ def on_end(self, span: ReadableSpan) -> None:
152152
return
153153

154154
# Handle REPLAY mode inbound spans
155-
if self._mode == TuskDriftMode.REPLAY and clean_span.kind.value == 2: # SERVER
155+
if self._mode == TuskDriftMode.REPLAY and clean_span.kind == TdSpanKind.SERVER:
156156
from ..drift_sdk import TuskDrift
157157

158+
# Set the trace ID to the replay trace ID, CLI will use this to match the trace test with the result
159+
replay_trace_id = replay_trace_id_context.get()
160+
if replay_trace_id:
161+
clean_span.trace_id = replay_trace_id
162+
else:
163+
logger.error("No replay trace ID found, cannot send inbound span for replay")
164+
return
165+
158166
sdk = TuskDrift.get_instance()
159167
# Check for running loop BEFORE creating the coroutine to avoid
160168
# "coroutine was never awaited" warnings. If we call the async
@@ -167,6 +175,13 @@ def on_end(self, span: ReadableSpan) -> None:
167175

168176
if loop is not None:
169177
loop.create_task(sdk.send_inbound_span_for_replay(clean_span))
178+
else:
179+
# No running loop - run synchronously
180+
try:
181+
asyncio.run(sdk.send_inbound_span_for_replay(clean_span))
182+
except RuntimeError:
183+
logger.error("No running loop, cannot send inbound span for replay")
184+
pass
170185

171186
# Export span via batch processor (RECORD mode)
172187
if self._mode == TuskDriftMode.RECORD:

0 commit comments

Comments
 (0)