Skip to content

Commit df3d3d3

Browse files
fix formatting
1 parent ec5c8f6 commit df3d3d3

7 files changed

Lines changed: 273 additions & 270 deletions

File tree

drift/core/mode_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def handle_record_mode(
6969
# App is ready - check span context
7070
is_server_span = span_kind == OTelSpanKind.SERVER
7171

72-
if (not current_span_info and not is_server_span) or (
73-
current_span_info and current_span_info.is_pre_app_start
74-
):
72+
if (not current_span_info and not is_server_span) or (current_span_info and current_span_info.is_pre_app_start):
7573
# No span context and not a server request, OR within a pre-app-start span
7674
# Skip recording - call original function
7775
return original_function_call()

drift/instrumentation/django/middleware.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,11 @@ def __call__(self, request: HttpRequest) -> HttpResponse:
7575
# RECORD mode - use handle_record_mode for consistent is_pre_app_start logic
7676
return handle_record_mode(
7777
original_function_call=lambda: self.get_response(request),
78-
record_mode_handler=lambda is_pre_app_start: self._record_request(
79-
request, sdk, is_pre_app_start
80-
),
78+
record_mode_handler=lambda is_pre_app_start: self._record_request(request, sdk, is_pre_app_start),
8179
span_kind=OTelSpanKind.SERVER,
8280
)
8381

84-
def _handle_replay_request(
85-
self, request: HttpRequest, sdk
86-
) -> HttpResponse:
82+
def _handle_replay_request(self, request: HttpRequest, sdk) -> HttpResponse:
8783
"""Handle request in REPLAY mode.
8884
8985
Extracts trace ID from headers and sets up context for child spans.
@@ -98,23 +94,15 @@ def _handle_replay_request(
9894
"""
9995
# Extract trace ID from headers (case-insensitive lookup)
10096
# Django stores headers in request.META
101-
headers_lower = {
102-
k.lower(): v for k, v in request.META.items() if k.startswith("HTTP_")
103-
}
104-
logger.info(
105-
f"[DJANGO_MIDDLEWARE] REPLAY mode, headers: {list(headers_lower.keys())}"
106-
)
97+
headers_lower = {k.lower(): v for k, v in request.META.items() if k.startswith("HTTP_")}
98+
logger.info(f"[DJANGO_MIDDLEWARE] REPLAY mode, headers: {list(headers_lower.keys())}")
10799
# Convert HTTP_X_TD_TRACE_ID -> x-td-trace-id
108100
replay_trace_id = headers_lower.get("http_x_td_trace_id")
109-
logger.info(
110-
f"[DJANGO_MIDDLEWARE] replay_trace_id from header: {replay_trace_id}"
111-
)
101+
logger.info(f"[DJANGO_MIDDLEWARE] replay_trace_id from header: {replay_trace_id}")
112102

113103
if not replay_trace_id:
114104
# No trace context in REPLAY mode; proceed without span
115-
logger.warning(
116-
"[DJANGO_MIDDLEWARE] No replay_trace_id found in headers, proceeding without span"
117-
)
105+
logger.warning("[DJANGO_MIDDLEWARE] No replay_trace_id found in headers, proceeding without span")
118106
return self.get_response(request)
119107

120108
# Set replay trace context
@@ -166,9 +154,7 @@ def _handle_replay_request(
166154
replay_trace_id_context.reset(replay_token)
167155
span_info.span.end()
168156

169-
def _record_request(
170-
self, request: HttpRequest, sdk, is_pre_app_start: bool
171-
) -> HttpResponse:
157+
def _record_request(self, request: HttpRequest, sdk, is_pre_app_start: bool) -> HttpResponse:
172158
"""Handle request in RECORD mode.
173159
174160
Creates a span, processes the request, and captures the span.
@@ -188,9 +174,7 @@ def _record_request(
188174

189175
sampling_rate = sdk.get_sampling_rate()
190176
if not should_sample(sampling_rate, is_app_ready=True):
191-
logger.debug(
192-
f"[Django] Request not sampled (rate={sampling_rate}), path={request.path}"
193-
)
177+
logger.debug(f"[Django] Request not sampled (rate={sampling_rate}), path={request.path}")
194178
return self.get_response(request)
195179

196180
start_time_ns = time.time_ns()
@@ -240,9 +224,7 @@ def _record_request(
240224

241225
request_headers = extract_headers(request.META)
242226

243-
if self.transform_engine and self.transform_engine.should_drop_inbound_request(
244-
method, target, request_headers
245-
):
227+
if self.transform_engine and self.transform_engine.should_drop_inbound_request(method, target, request_headers):
246228
# Reset context before early return
247229
span_kind_context.reset(span_kind_token)
248230
span_info.span.end()
@@ -289,9 +271,7 @@ def process_view(
289271
if route:
290272
request._drift_route_template = route # type: ignore
291273

292-
def _capture_span(
293-
self, request: HttpRequest, response: HttpResponse, span_info: SpanInfo
294-
) -> None:
274+
def _capture_span(self, request: HttpRequest, response: HttpResponse, span_info: SpanInfo) -> None:
295275
"""Create and collect a span from request/response data.
296276
297277
Args:
@@ -458,9 +438,7 @@ def dict_to_schema_merges(merges_dict):
458438

459439
sdk.collect_span(clean_span)
460440

461-
def _capture_error_span(
462-
self, request: HttpRequest, exception: Exception, span_info: SpanInfo
463-
) -> None:
441+
def _capture_error_span(self, request: HttpRequest, exception: Exception, span_info: SpanInfo) -> None:
464442
"""Create and collect an error span.
465443
466444
Args:

drift/instrumentation/fastapi/instrumentation.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ async def wrapped_send(message: dict[str, Any]) -> None:
197197
response_data["status_message"] = _get_status_message(message.get("status", 200))
198198
raw_headers = message.get("headers", [])
199199
response_data["headers"] = {
200-
k.decode("utf-8", errors="replace") if isinstance(k, bytes) else k: v.decode(
201-
"utf-8", errors="replace"
202-
)
200+
k.decode("utf-8", errors="replace") if isinstance(k, bytes) else k: v.decode("utf-8", errors="replace")
203201
if isinstance(v, bytes)
204202
else v
205203
for k, v in raw_headers
@@ -324,9 +322,7 @@ async def wrapped_send(message: dict[str, Any]) -> None:
324322
response_data["status_message"] = _get_status_message(message.get("status", 200))
325323
raw_headers = message.get("headers", [])
326324
response_data["headers"] = {
327-
k.decode("utf-8", errors="replace") if isinstance(k, bytes) else k: v.decode(
328-
"utf-8", errors="replace"
329-
)
325+
k.decode("utf-8", errors="replace") if isinstance(k, bytes) else k: v.decode("utf-8", errors="replace")
330326
if isinstance(v, bytes)
331327
else v
332328
for k, v in raw_headers

drift/instrumentation/psycopg/instrumentation.py

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -350,27 +350,28 @@ def _noop_execute(self, cursor: Any) -> Any:
350350

351351
def _replay_execute(self, cursor: Any, sdk: TuskDrift, query_str: str, params: Any) -> Any:
352352
"""Handle REPLAY mode for execute - fetch mock from CLI."""
353-
span_info = SpanUtils.create_span(CreateSpanOptions(
354-
name="psycopg.query",
355-
kind=OTelSpanKind.CLIENT,
356-
attributes={
357-
TdSpanAttributes.NAME: "psycopg.query",
358-
TdSpanAttributes.PACKAGE_NAME: "psycopg",
359-
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
360-
TdSpanAttributes.SUBMODULE_NAME: "query",
361-
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
362-
TdSpanAttributes.IS_PRE_APP_START: not sdk.app_ready,
363-
},
364-
is_pre_app_start=not sdk.app_ready,
365-
))
353+
span_info = SpanUtils.create_span(
354+
CreateSpanOptions(
355+
name="psycopg.query",
356+
kind=OTelSpanKind.CLIENT,
357+
attributes={
358+
TdSpanAttributes.NAME: "psycopg.query",
359+
TdSpanAttributes.PACKAGE_NAME: "psycopg",
360+
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
361+
TdSpanAttributes.SUBMODULE_NAME: "query",
362+
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
363+
TdSpanAttributes.IS_PRE_APP_START: not sdk.app_ready,
364+
},
365+
is_pre_app_start=not sdk.app_ready,
366+
)
367+
)
366368

367369
if not span_info:
368370
raise RuntimeError("Error creating span in replay mode")
369371

370372
with SpanUtils.with_span(span_info):
371373
mock_result = self._try_get_mock(
372-
sdk, query_str, params,
373-
span_info.trace_id, span_info.span_id, span_info.parent_span_id
374+
sdk, query_str, params, span_info.trace_id, span_info.span_id, span_info.parent_span_id
374375
)
375376

376377
if mock_result is None:
@@ -397,19 +398,21 @@ def _record_execute(
397398
kwargs: dict,
398399
) -> Any:
399400
"""Handle RECORD mode for execute - create span and execute query."""
400-
span_info = SpanUtils.create_span(CreateSpanOptions(
401-
name="psycopg.query",
402-
kind=OTelSpanKind.CLIENT,
403-
attributes={
404-
TdSpanAttributes.NAME: "psycopg.query",
405-
TdSpanAttributes.PACKAGE_NAME: "psycopg",
406-
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
407-
TdSpanAttributes.SUBMODULE_NAME: "query",
408-
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
409-
TdSpanAttributes.IS_PRE_APP_START: is_pre_app_start,
410-
},
411-
is_pre_app_start=is_pre_app_start,
412-
))
401+
span_info = SpanUtils.create_span(
402+
CreateSpanOptions(
403+
name="psycopg.query",
404+
kind=OTelSpanKind.CLIENT,
405+
attributes={
406+
TdSpanAttributes.NAME: "psycopg.query",
407+
TdSpanAttributes.PACKAGE_NAME: "psycopg",
408+
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
409+
TdSpanAttributes.SUBMODULE_NAME: "query",
410+
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
411+
TdSpanAttributes.IS_PRE_APP_START: is_pre_app_start,
412+
},
413+
is_pre_app_start=is_pre_app_start,
414+
)
415+
)
413416

414417
if not span_info:
415418
# Fallback to original call if span creation fails
@@ -464,27 +467,28 @@ def _traced_executemany(
464467

465468
def _replay_executemany(self, cursor: Any, sdk: TuskDrift, query_str: str, params_list: list) -> Any:
466469
"""Handle REPLAY mode for executemany - fetch mock from CLI."""
467-
span_info = SpanUtils.create_span(CreateSpanOptions(
468-
name="psycopg.query",
469-
kind=OTelSpanKind.CLIENT,
470-
attributes={
471-
TdSpanAttributes.NAME: "psycopg.query",
472-
TdSpanAttributes.PACKAGE_NAME: "psycopg",
473-
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
474-
TdSpanAttributes.SUBMODULE_NAME: "query",
475-
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
476-
TdSpanAttributes.IS_PRE_APP_START: not sdk.app_ready,
477-
},
478-
is_pre_app_start=not sdk.app_ready,
479-
))
470+
span_info = SpanUtils.create_span(
471+
CreateSpanOptions(
472+
name="psycopg.query",
473+
kind=OTelSpanKind.CLIENT,
474+
attributes={
475+
TdSpanAttributes.NAME: "psycopg.query",
476+
TdSpanAttributes.PACKAGE_NAME: "psycopg",
477+
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
478+
TdSpanAttributes.SUBMODULE_NAME: "query",
479+
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
480+
TdSpanAttributes.IS_PRE_APP_START: not sdk.app_ready,
481+
},
482+
is_pre_app_start=not sdk.app_ready,
483+
)
484+
)
480485

481486
if not span_info:
482487
raise RuntimeError("Error creating span in replay mode")
483488

484489
with SpanUtils.with_span(span_info):
485490
mock_result = self._try_get_mock(
486-
sdk, query_str, {"_batch": params_list},
487-
span_info.trace_id, span_info.span_id, span_info.parent_span_id
491+
sdk, query_str, {"_batch": params_list}, span_info.trace_id, span_info.span_id, span_info.parent_span_id
488492
)
489493

490494
if mock_result is None:
@@ -514,19 +518,21 @@ def _record_executemany(
514518
kwargs: dict,
515519
) -> Any:
516520
"""Handle RECORD mode for executemany - create span and execute query."""
517-
span_info = SpanUtils.create_span(CreateSpanOptions(
518-
name="psycopg.query",
519-
kind=OTelSpanKind.CLIENT,
520-
attributes={
521-
TdSpanAttributes.NAME: "psycopg.query",
522-
TdSpanAttributes.PACKAGE_NAME: "psycopg",
523-
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
524-
TdSpanAttributes.SUBMODULE_NAME: "query",
525-
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
526-
TdSpanAttributes.IS_PRE_APP_START: is_pre_app_start,
527-
},
528-
is_pre_app_start=is_pre_app_start,
529-
))
521+
span_info = SpanUtils.create_span(
522+
CreateSpanOptions(
523+
name="psycopg.query",
524+
kind=OTelSpanKind.CLIENT,
525+
attributes={
526+
TdSpanAttributes.NAME: "psycopg.query",
527+
TdSpanAttributes.PACKAGE_NAME: "psycopg",
528+
TdSpanAttributes.INSTRUMENTATION_NAME: "PsycopgInstrumentation",
529+
TdSpanAttributes.SUBMODULE_NAME: "query",
530+
TdSpanAttributes.PACKAGE_TYPE: PackageType.PG.name,
531+
TdSpanAttributes.IS_PRE_APP_START: is_pre_app_start,
532+
},
533+
is_pre_app_start=is_pre_app_start,
534+
)
535+
)
530536

531537
if not span_info:
532538
# Fallback to original call if span creation fails

0 commit comments

Comments
 (0)