-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_utils.py
More file actions
303 lines (251 loc) · 10.1 KB
/
mock_utils.py
File metadata and controls
303 lines (251 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""Centralized mock request utilities matching Node SDK's mockResponseUtils.ts.
This module provides utilities for finding mock responses in REPLAY mode,
centralizing schema generation logic that was previously duplicated across
instrumentations.
"""
from __future__ import annotations
import logging
import time
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from .communication.types import MockResponseOutput
from .drift_sdk import TuskDrift
from .json_schema_helper import SchemaMerges
from .types import CleanSpanData
from .json_schema_helper import JsonSchemaHelper
from .types import (
Duration,
PackageType,
SpanKind,
SpanStatus,
StatusCode,
Timestamp,
replay_trace_id_context,
)
logger = logging.getLogger(__name__)
def convert_mock_request_to_clean_span(
*,
trace_id: str,
span_id: str,
name: str,
package_name: str,
package_type: PackageType,
instrumentation_name: str,
submodule_name: str,
input_value: dict[str, Any],
kind: SpanKind,
input_schema_merges: SchemaMerges | None = None,
stack_trace: str | None = None,
is_pre_app_start: bool = False,
) -> CleanSpanData:
"""Convert mock request data to CleanSpanData with schema generation.
This centralizes the schema generation logic that was previously duplicated
across instrumentations. Matches Node SDK's convertMockRequestDataToCleanSpanData.
Args:
trace_id: Trace ID for the span
span_id: Span ID
name: Span name (e.g., "GET /api/users")
package_name: Package name (e.g., "http", "https", "redis")
package_type: Package type (HTTP, DATABASE, etc.)
instrumentation_name: Instrumentation name
submodule_name: Submodule name (e.g., "GET", "SET")
input_value: Input value dictionary
kind: Span kind (typically CLIENT for outbound requests)
input_schema_merges: Schema merge hints for input
stack_trace: Optional stack trace
is_pre_app_start: Whether this span occurred before app start
Returns:
CleanSpanData with generated schemas and hashes
"""
from .types import CleanSpanData
# Generate schema and hashes from input value and merges
input_result = JsonSchemaHelper.generate_schema_and_hash(input_value, input_schema_merges)
# Get current timestamp
timestamp_ms = time.time() * 1000
timestamp_seconds = int(timestamp_ms // 1000)
timestamp_nanos = int((timestamp_ms % 1000) * 1_000_000)
return CleanSpanData(
trace_id=trace_id,
span_id=span_id,
parent_span_id="",
name=name,
package_name=package_name,
package_type=package_type,
instrumentation_name=instrumentation_name,
submodule_name=submodule_name,
input_value=input_value,
output_value=None,
input_schema=input_result.schema,
input_schema_hash=input_result.decoded_schema_hash,
input_value_hash=input_result.decoded_value_hash,
output_schema=None, # type: ignore[arg-type] - Must be None to avoid betterproto serialization issues
output_schema_hash="",
output_value_hash="",
kind=kind,
status=SpanStatus(code=StatusCode.OK, message="OK"),
timestamp=Timestamp(seconds=timestamp_seconds, nanos=timestamp_nanos),
duration=Duration(seconds=0, nanos=0),
is_root_span=False,
is_pre_app_start=is_pre_app_start,
stack_trace=stack_trace or "",
)
def find_mock_response_sync(
*,
sdk: TuskDrift,
trace_id: str,
span_id: str,
name: str,
package_name: str,
package_type: PackageType,
instrumentation_name: str,
submodule_name: str,
input_value: dict[str, Any],
kind: SpanKind = SpanKind.CLIENT,
input_schema_merges: SchemaMerges | None = None,
stack_trace: str | None = None,
is_pre_app_start: bool = False,
) -> MockResponseOutput | None:
"""Find mock response for outbound request in REPLAY mode (synchronous).
Centralizes the common logic of:
1. Getting the replay trace ID from context
2. Generating schemas from input value and merges
3. Creating CleanSpanData for mock request
4. Making the mock request to CLI
5. Handling the response and error cases
Matches Node SDK's findMockResponseSync.
Args:
sdk: TuskDrift instance
trace_id: Trace ID for the outbound span
span_id: Span ID for the outbound span
name: Span name (e.g., "GET /api/users")
package_name: Package name (e.g., "http", "https")
package_type: Package type (typically HTTP)
instrumentation_name: Instrumentation name
submodule_name: Submodule name (e.g., "GET", "POST")
input_value: Input value dictionary
kind: Span kind (default CLIENT)
input_schema_merges: Schema merge hints for input
stack_trace: Optional stack trace
is_pre_app_start: Whether this span occurred before app start
Returns:
MockResponseOutput if found, None otherwise
"""
try:
# Get replay trace ID from context
replay_trace_id = replay_trace_id_context.get()
# Convert to CleanSpanData with schema generation
outbound_span = convert_mock_request_to_clean_span(
trace_id=trace_id,
span_id=span_id,
name=name,
package_name=package_name,
package_type=package_type,
instrumentation_name=instrumentation_name,
submodule_name=submodule_name,
input_value=input_value,
kind=kind,
input_schema_merges=input_schema_merges,
stack_trace=stack_trace,
is_pre_app_start=is_pre_app_start,
)
logger.debug(f"Finding mock for {trace_id} with replay trace ID: {replay_trace_id}")
# Request mock from CLI
from .communication.types import MockRequestInput
mock_request = MockRequestInput(
test_id=replay_trace_id or "",
outbound_span=outbound_span,
)
mock_response = sdk.request_mock_sync(mock_request)
if not mock_response or not mock_response.found:
logger.debug(
f"No matching mock found for {trace_id} with input value: {input_value}, input schema: {input_schema_merges}, input schema hash: {outbound_span.input_schema_hash}, input value hash: {outbound_span.input_value_hash}"
)
return None
logger.debug(f"Found mock response for {trace_id}")
# Update time travel to match mock's recorded timestamp
_update_time_travel(mock_response, replay_trace_id)
return mock_response
except Exception as e:
logger.error(f"Error finding mock response for {trace_id}: {e}")
return None
async def find_mock_response_async(
*,
sdk: TuskDrift,
trace_id: str,
span_id: str,
name: str,
package_name: str,
package_type: PackageType,
instrumentation_name: str,
submodule_name: str,
input_value: dict[str, Any],
kind: SpanKind = SpanKind.CLIENT,
input_schema_merges: SchemaMerges | None = None,
stack_trace: str | None = None,
is_pre_app_start: bool = False,
) -> MockResponseOutput | None:
"""Find mock response for outbound request in REPLAY mode (asynchronous).
Async version of find_mock_response_sync. See that function for details.
Matches Node SDK's findMockResponseAsync.
"""
try:
# Get replay trace ID from context
replay_trace_id = replay_trace_id_context.get()
# Convert to CleanSpanData with schema generation
outbound_span = convert_mock_request_to_clean_span(
trace_id=trace_id,
span_id=span_id,
name=name,
package_name=package_name,
package_type=package_type,
instrumentation_name=instrumentation_name,
submodule_name=submodule_name,
input_value=input_value,
kind=kind,
input_schema_merges=input_schema_merges,
stack_trace=stack_trace,
is_pre_app_start=is_pre_app_start,
)
logger.debug(f"Finding mock for {trace_id} with replay trace ID: {replay_trace_id}")
# Request mock from CLI
from .communication.types import MockRequestInput
mock_request = MockRequestInput(
test_id=replay_trace_id or "",
outbound_span=outbound_span,
)
mock_response = await sdk.request_mock_async(mock_request)
if not mock_response or not mock_response.found:
logger.debug(f"No matching mock found for {trace_id} with input value: {input_value}")
return None
logger.debug(f"Found mock response for {trace_id}")
# Update time travel to match mock's recorded timestamp
_update_time_travel(mock_response, replay_trace_id)
return mock_response
except Exception as e:
logger.error(f"Error finding mock response for {trace_id}: {e}")
return None
def _update_time_travel(
mock_response: MockResponseOutput,
replay_trace_id: str | None,
) -> None:
"""Update time travel to match the mock response's recorded timestamp.
Sets the clock to the timestamp from each mock response, keeping time
in sync with when the original requests were made during recording.
Args:
mock_response: The mock response containing the timestamp
replay_trace_id: The replay trace ID for this session
"""
if not replay_trace_id:
return
try:
from drift.instrumentation.datetime import start_time_travel
response_data = mock_response.response
# Timestamp is extracted from MockInteraction and added to response by communicator
timestamp = response_data.get("timestamp") if isinstance(response_data, dict) else None
if timestamp:
logger.debug(f"Setting time travel to timestamp: {timestamp}")
start_time_travel(timestamp, trace_id=replay_trace_id)
else:
logger.debug("No timestamp in mock response, skipping time travel")
except Exception as e:
logger.debug(f"Failed to start time travel: {e}")