|
| 1 | +"""Mode utilities for handling RECORD and REPLAY mode logic. |
| 2 | +
|
| 3 | +This module provides utilities that abstract common mode-handling patterns, |
| 4 | +matching the Node SDK's modeUtils.ts. These utilities help instrumentations |
| 5 | +decide how to handle requests based on the SDK mode and app state. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import logging |
| 11 | +from collections.abc import Callable |
| 12 | +from typing import TYPE_CHECKING, TypeVar |
| 13 | + |
| 14 | +from opentelemetry.trace import SpanKind as OTelSpanKind |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + pass |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | +T = TypeVar("T") |
| 22 | + |
| 23 | +# Type aliases for handler functions |
| 24 | +OriginalFunctionCall = Callable[[], T] |
| 25 | +RecordModeHandler = Callable[[bool], T] # (is_pre_app_start: bool) -> T |
| 26 | +ReplayModeHandler = Callable[[], T] |
| 27 | +NoOpRequestHandler = Callable[[], T] |
| 28 | + |
| 29 | + |
| 30 | +def handle_record_mode( |
| 31 | + original_function_call: OriginalFunctionCall[T], |
| 32 | + record_mode_handler: RecordModeHandler[T], |
| 33 | + span_kind: OTelSpanKind, |
| 34 | +) -> T: |
| 35 | + """Handle RECORD mode logic for instrumentations. |
| 36 | +
|
| 37 | + This utility abstracts the common record mode pattern of checking for |
| 38 | + current span context and deciding whether to execute record mode logic |
| 39 | + or just call the original function. |
| 40 | +
|
| 41 | + Decision logic: |
| 42 | + - If app NOT ready -> call record_mode_handler(is_pre_app_start=True) |
| 43 | + - If no span context AND not SERVER span, OR span was pre-app-start -> call original_function_call() (skip) |
| 44 | + - Otherwise -> call record_mode_handler(is_pre_app_start=False) |
| 45 | +
|
| 46 | + Args: |
| 47 | + original_function_call: Function that calls the original function when no span context exists |
| 48 | + record_mode_handler: Function that handles record mode logic; receives is_pre_app_start flag |
| 49 | + span_kind: The kind of span being created (determines if this is a server request) |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Result from either original_function_call or record_mode_handler |
| 53 | + """ |
| 54 | + from .drift_sdk import TuskDrift |
| 55 | + from .tracing.span_utils import SpanUtils |
| 56 | + |
| 57 | + try: |
| 58 | + sdk = TuskDrift.get_instance() |
| 59 | + is_app_ready = sdk.is_app_ready() |
| 60 | + current_span_info = SpanUtils.get_current_span_info() |
| 61 | + except Exception as e: |
| 62 | + logger.error(f"ModeUtils error checking app readiness or getting current span info: {e}") |
| 63 | + return original_function_call() |
| 64 | + |
| 65 | + if not is_app_ready: |
| 66 | + # App not ready - record with is_pre_app_start=True |
| 67 | + return record_mode_handler(True) |
| 68 | + |
| 69 | + # App is ready - check span context |
| 70 | + is_server_span = span_kind == OTelSpanKind.SERVER |
| 71 | + |
| 72 | + if (not current_span_info and not is_server_span) or (current_span_info and current_span_info.is_pre_app_start): |
| 73 | + # No span context and not a server request, OR within a pre-app-start span |
| 74 | + # Skip recording - call original function |
| 75 | + return original_function_call() |
| 76 | + |
| 77 | + # App ready with valid span context - record with is_pre_app_start=False |
| 78 | + return record_mode_handler(False) |
| 79 | + |
| 80 | + |
| 81 | +def handle_replay_mode( |
| 82 | + replay_mode_handler: ReplayModeHandler[T], |
| 83 | + no_op_request_handler: NoOpRequestHandler[T], |
| 84 | + is_server_request: bool, |
| 85 | +) -> T: |
| 86 | + """Handle REPLAY mode logic for instrumentations. |
| 87 | +
|
| 88 | + This utility abstracts the common replay mode pattern of checking if |
| 89 | + the request is a background request. |
| 90 | +
|
| 91 | + Decision logic: |
| 92 | + - If background request (app ready + no parent span + not server request) -> call no_op_request_handler() |
| 93 | + - Otherwise -> call replay_mode_handler() |
| 94 | +
|
| 95 | + Background requests are requests that happen after app startup but outside |
| 96 | + of any trace context (health checks, background jobs, etc.). In REPLAY mode, |
| 97 | + these should return dummy responses instead of querying for mocks. |
| 98 | +
|
| 99 | + Args: |
| 100 | + replay_mode_handler: Function that handles normal replay mode logic (fetches mocks) |
| 101 | + no_op_request_handler: Function that returns a dummy/no-op response for background requests |
| 102 | + is_server_request: True if this is a SERVER span (inbound HTTP request) |
| 103 | +
|
| 104 | + Returns: |
| 105 | + Result from either no_op_request_handler or replay_mode_handler |
| 106 | + """ |
| 107 | + from .drift_sdk import TuskDrift |
| 108 | + from .tracing.span_utils import SpanUtils |
| 109 | + |
| 110 | + sdk = TuskDrift.get_instance() |
| 111 | + is_app_ready = sdk.is_app_ready() |
| 112 | + current_span_info = SpanUtils.get_current_span_info() |
| 113 | + |
| 114 | + # Background request: App is ready + not within a trace (no parent span) + not a server request |
| 115 | + if is_app_ready and not current_span_info and not is_server_request: |
| 116 | + logger.debug("[ModeUtils] Handling no-op request") |
| 117 | + return no_op_request_handler() |
| 118 | + |
| 119 | + return replay_mode_handler() |
| 120 | + |
| 121 | + |
| 122 | +def is_background_request(is_server_request: bool = False) -> bool: |
| 123 | + """Check if the current request is a background request. |
| 124 | +
|
| 125 | + A background request is one that: |
| 126 | + - Happens after app is ready (not pre-app-start) |
| 127 | + - Has no parent span context (not within an existing trace) |
| 128 | + - Is not a server request (not an incoming HTTP request that starts a new trace) |
| 129 | +
|
| 130 | + Background requests should typically be handled with no-op/dummy responses |
| 131 | + in REPLAY mode since they were never recorded. |
| 132 | +
|
| 133 | + Args: |
| 134 | + is_server_request: True if this is a SERVER span type |
| 135 | +
|
| 136 | + Returns: |
| 137 | + True if this is a background request, False otherwise |
| 138 | + """ |
| 139 | + from .drift_sdk import TuskDrift |
| 140 | + from .tracing.span_utils import SpanUtils |
| 141 | + |
| 142 | + sdk = TuskDrift.get_instance() |
| 143 | + is_app_ready = sdk.is_app_ready() |
| 144 | + current_span_info = SpanUtils.get_current_span_info() |
| 145 | + |
| 146 | + return is_app_ready and not current_span_info and not is_server_request |
0 commit comments