sf-veritas 0.11.10__cp314-cp314-manylinux_2_28_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. sf_veritas/__init__.py +46 -0
  2. sf_veritas/_auto_preload.py +73 -0
  3. sf_veritas/_sfconfig.c +162 -0
  4. sf_veritas/_sfconfig.cpython-314-x86_64-linux-gnu.so +0 -0
  5. sf_veritas/_sfcrashhandler.c +267 -0
  6. sf_veritas/_sfcrashhandler.cpython-314-x86_64-linux-gnu.so +0 -0
  7. sf_veritas/_sffastlog.c +953 -0
  8. sf_veritas/_sffastlog.cpython-314-x86_64-linux-gnu.so +0 -0
  9. sf_veritas/_sffastnet.c +994 -0
  10. sf_veritas/_sffastnet.cpython-314-x86_64-linux-gnu.so +0 -0
  11. sf_veritas/_sffastnetworkrequest.c +727 -0
  12. sf_veritas/_sffastnetworkrequest.cpython-314-x86_64-linux-gnu.so +0 -0
  13. sf_veritas/_sffuncspan.c +2791 -0
  14. sf_veritas/_sffuncspan.cpython-314-x86_64-linux-gnu.so +0 -0
  15. sf_veritas/_sffuncspan_config.c +730 -0
  16. sf_veritas/_sffuncspan_config.cpython-314-x86_64-linux-gnu.so +0 -0
  17. sf_veritas/_sfheadercheck.c +341 -0
  18. sf_veritas/_sfheadercheck.cpython-314-x86_64-linux-gnu.so +0 -0
  19. sf_veritas/_sfnetworkhop.c +1454 -0
  20. sf_veritas/_sfnetworkhop.cpython-314-x86_64-linux-gnu.so +0 -0
  21. sf_veritas/_sfservice.c +1223 -0
  22. sf_veritas/_sfservice.cpython-314-x86_64-linux-gnu.so +0 -0
  23. sf_veritas/_sfteepreload.c +6227 -0
  24. sf_veritas/app_config.py +57 -0
  25. sf_veritas/cli.py +336 -0
  26. sf_veritas/constants.py +10 -0
  27. sf_veritas/custom_excepthook.py +304 -0
  28. sf_veritas/custom_log_handler.py +146 -0
  29. sf_veritas/custom_output_wrapper.py +153 -0
  30. sf_veritas/custom_print.py +153 -0
  31. sf_veritas/django_app.py +5 -0
  32. sf_veritas/env_vars.py +186 -0
  33. sf_veritas/exception_handling_middleware.py +18 -0
  34. sf_veritas/exception_metaclass.py +69 -0
  35. sf_veritas/fast_frame_info.py +116 -0
  36. sf_veritas/fast_network_hop.py +293 -0
  37. sf_veritas/frame_tools.py +112 -0
  38. sf_veritas/funcspan_config_loader.py +693 -0
  39. sf_veritas/function_span_profiler.py +1313 -0
  40. sf_veritas/get_preload_path.py +34 -0
  41. sf_veritas/import_hook.py +62 -0
  42. sf_veritas/infra_details/__init__.py +3 -0
  43. sf_veritas/infra_details/get_infra_details.py +24 -0
  44. sf_veritas/infra_details/kubernetes/__init__.py +3 -0
  45. sf_veritas/infra_details/kubernetes/get_cluster_name.py +147 -0
  46. sf_veritas/infra_details/kubernetes/get_details.py +7 -0
  47. sf_veritas/infra_details/running_on/__init__.py +17 -0
  48. sf_veritas/infra_details/running_on/kubernetes.py +11 -0
  49. sf_veritas/interceptors.py +543 -0
  50. sf_veritas/libsfnettee.so +0 -0
  51. sf_veritas/local_env_detect.py +118 -0
  52. sf_veritas/package_metadata.py +6 -0
  53. sf_veritas/patches/__init__.py +0 -0
  54. sf_veritas/patches/_patch_tracker.py +74 -0
  55. sf_veritas/patches/concurrent_futures.py +19 -0
  56. sf_veritas/patches/constants.py +1 -0
  57. sf_veritas/patches/exceptions.py +82 -0
  58. sf_veritas/patches/multiprocessing.py +32 -0
  59. sf_veritas/patches/network_libraries/__init__.py +99 -0
  60. sf_veritas/patches/network_libraries/aiohttp.py +294 -0
  61. sf_veritas/patches/network_libraries/curl_cffi.py +363 -0
  62. sf_veritas/patches/network_libraries/http_client.py +670 -0
  63. sf_veritas/patches/network_libraries/httpcore.py +580 -0
  64. sf_veritas/patches/network_libraries/httplib2.py +315 -0
  65. sf_veritas/patches/network_libraries/httpx.py +557 -0
  66. sf_veritas/patches/network_libraries/niquests.py +218 -0
  67. sf_veritas/patches/network_libraries/pycurl.py +399 -0
  68. sf_veritas/patches/network_libraries/requests.py +595 -0
  69. sf_veritas/patches/network_libraries/ssl_socket.py +822 -0
  70. sf_veritas/patches/network_libraries/tornado.py +360 -0
  71. sf_veritas/patches/network_libraries/treq.py +270 -0
  72. sf_veritas/patches/network_libraries/urllib_request.py +483 -0
  73. sf_veritas/patches/network_libraries/utils.py +598 -0
  74. sf_veritas/patches/os.py +17 -0
  75. sf_veritas/patches/threading.py +231 -0
  76. sf_veritas/patches/web_frameworks/__init__.py +54 -0
  77. sf_veritas/patches/web_frameworks/aiohttp.py +798 -0
  78. sf_veritas/patches/web_frameworks/async_websocket_consumer.py +337 -0
  79. sf_veritas/patches/web_frameworks/blacksheep.py +532 -0
  80. sf_veritas/patches/web_frameworks/bottle.py +513 -0
  81. sf_veritas/patches/web_frameworks/cherrypy.py +683 -0
  82. sf_veritas/patches/web_frameworks/cors_utils.py +122 -0
  83. sf_veritas/patches/web_frameworks/django.py +963 -0
  84. sf_veritas/patches/web_frameworks/eve.py +401 -0
  85. sf_veritas/patches/web_frameworks/falcon.py +931 -0
  86. sf_veritas/patches/web_frameworks/fastapi.py +738 -0
  87. sf_veritas/patches/web_frameworks/flask.py +526 -0
  88. sf_veritas/patches/web_frameworks/klein.py +501 -0
  89. sf_veritas/patches/web_frameworks/litestar.py +616 -0
  90. sf_veritas/patches/web_frameworks/pyramid.py +440 -0
  91. sf_veritas/patches/web_frameworks/quart.py +841 -0
  92. sf_veritas/patches/web_frameworks/robyn.py +708 -0
  93. sf_veritas/patches/web_frameworks/sanic.py +874 -0
  94. sf_veritas/patches/web_frameworks/starlette.py +742 -0
  95. sf_veritas/patches/web_frameworks/strawberry.py +1446 -0
  96. sf_veritas/patches/web_frameworks/tornado.py +485 -0
  97. sf_veritas/patches/web_frameworks/utils.py +170 -0
  98. sf_veritas/print_override.py +13 -0
  99. sf_veritas/regular_data_transmitter.py +444 -0
  100. sf_veritas/request_interceptor.py +401 -0
  101. sf_veritas/request_utils.py +550 -0
  102. sf_veritas/segfault_handler.py +116 -0
  103. sf_veritas/server_status.py +1 -0
  104. sf_veritas/shutdown_flag.py +11 -0
  105. sf_veritas/subprocess_startup.py +3 -0
  106. sf_veritas/test_cli.py +145 -0
  107. sf_veritas/thread_local.py +1319 -0
  108. sf_veritas/timeutil.py +114 -0
  109. sf_veritas/transmit_exception_to_sailfish.py +28 -0
  110. sf_veritas/transmitter.py +132 -0
  111. sf_veritas/types.py +47 -0
  112. sf_veritas/unified_interceptor.py +1678 -0
  113. sf_veritas/utils.py +39 -0
  114. sf_veritas-0.11.10.dist-info/METADATA +97 -0
  115. sf_veritas-0.11.10.dist-info/RECORD +141 -0
  116. sf_veritas-0.11.10.dist-info/WHEEL +5 -0
  117. sf_veritas-0.11.10.dist-info/entry_points.txt +2 -0
  118. sf_veritas-0.11.10.dist-info/top_level.txt +1 -0
  119. sf_veritas.libs/libbrotlicommon-6ce2a53c.so.1.0.6 +0 -0
  120. sf_veritas.libs/libbrotlidec-811d1be3.so.1.0.6 +0 -0
  121. sf_veritas.libs/libcom_err-730ca923.so.2.1 +0 -0
  122. sf_veritas.libs/libcrypt-52aca757.so.1.1.0 +0 -0
  123. sf_veritas.libs/libcrypto-bdaed0ea.so.1.1.1k +0 -0
  124. sf_veritas.libs/libcurl-eaa3cf66.so.4.5.0 +0 -0
  125. sf_veritas.libs/libgssapi_krb5-323bbd21.so.2.2 +0 -0
  126. sf_veritas.libs/libidn2-2f4a5893.so.0.3.6 +0 -0
  127. sf_veritas.libs/libk5crypto-9a74ff38.so.3.1 +0 -0
  128. sf_veritas.libs/libkeyutils-2777d33d.so.1.6 +0 -0
  129. sf_veritas.libs/libkrb5-a55300e8.so.3.3 +0 -0
  130. sf_veritas.libs/libkrb5support-e6594cfc.so.0.1 +0 -0
  131. sf_veritas.libs/liblber-2-d20824ef.4.so.2.10.9 +0 -0
  132. sf_veritas.libs/libldap-2-cea2a960.4.so.2.10.9 +0 -0
  133. sf_veritas.libs/libnghttp2-39367a22.so.14.17.0 +0 -0
  134. sf_veritas.libs/libpcre2-8-516f4c9d.so.0.7.1 +0 -0
  135. sf_veritas.libs/libpsl-99becdd3.so.5.3.1 +0 -0
  136. sf_veritas.libs/libsasl2-7de4d792.so.3.0.0 +0 -0
  137. sf_veritas.libs/libselinux-d0805dcb.so.1 +0 -0
  138. sf_veritas.libs/libssh-c11d285b.so.4.8.7 +0 -0
  139. sf_veritas.libs/libssl-60250281.so.1.1.1k +0 -0
  140. sf_veritas.libs/libunistring-05abdd40.so.2.1.0 +0 -0
  141. sf_veritas.libs/libuuid-95b83d40.so.1.3.0 +0 -0
@@ -0,0 +1,738 @@
1
+ """
2
+ OTEL-STYLE PURE ASYNC PATTERN:
3
+ • Call C extension directly AFTER response sent
4
+ • C queues to lock-free ring buffer and returns in ~1µs
5
+ • ASGI event loop returns instantly (doesn't wait)
6
+ • C background thread does ALL work with GIL released
7
+ • This should MATCH OTEL performance (identical pattern)
8
+
9
+ KEY INSIGHT: No Python threads! C extension handles everything.
10
+ """
11
+
12
+ import asyncio
13
+ import functools
14
+ import gc
15
+ import json
16
+ import os
17
+ import threading
18
+ from typing import Callable, List, Optional
19
+
20
+ from ... import _sffuncspan, app_config
21
+ from ...constants import (
22
+ FUNCSPAN_OVERRIDE_HEADER_BYTES,
23
+ SAILFISH_TRACING_HEADER,
24
+ SAILFISH_TRACING_HEADER_BYTES,
25
+ )
26
+ from ...custom_excepthook import custom_excepthook
27
+ from ...env_vars import (
28
+ SF_DEBUG,
29
+ SF_NETWORKHOP_CAPTURE_ENABLED,
30
+ SF_NETWORKHOP_CAPTURE_REQUEST_BODY,
31
+ SF_NETWORKHOP_CAPTURE_REQUEST_HEADERS,
32
+ SF_NETWORKHOP_CAPTURE_RESPONSE_BODY,
33
+ SF_NETWORKHOP_CAPTURE_RESPONSE_HEADERS,
34
+ SF_NETWORKHOP_REQUEST_LIMIT_MB,
35
+ SF_NETWORKHOP_RESPONSE_LIMIT_MB,
36
+ )
37
+ from ...fast_network_hop import fast_send_network_hop_fast, register_endpoint
38
+ from ...thread_local import (
39
+ clear_c_tls_parent_trace_id,
40
+ clear_current_request_path,
41
+ clear_funcspan_override,
42
+ clear_outbound_header_base,
43
+ clear_trace_id,
44
+ generate_new_trace_id,
45
+ get_or_set_sf_trace_id,
46
+ get_sf_trace_id,
47
+ set_current_request_path,
48
+ set_funcspan_override,
49
+ set_outbound_header_base,
50
+ )
51
+ from .utils import reinitialize_log_print_capture_for_worker
52
+ from .cors_utils import inject_sailfish_headers, should_inject_headers
53
+ from .utils import _is_user_code, _unwrap_user_func, should_skip_route
54
+
55
+ _SKIP_TRACING_ATTR = "_sf_skip_tracing"
56
+
57
+ # Size limits in bytes
58
+ _REQUEST_LIMIT_BYTES = SF_NETWORKHOP_REQUEST_LIMIT_MB * 1024 * 1024
59
+ _RESPONSE_LIMIT_BYTES = SF_NETWORKHOP_RESPONSE_LIMIT_MB * 1024 * 1024
60
+
61
+ # OTEL-STYLE: No thread pool! Pure async, call C extension directly
62
+ # C extension has its own background worker thread
63
+
64
+ try:
65
+ import fastapi
66
+ from starlette.types import ASGIApp, Message, Receive, Scope, Send
67
+ except ImportError:
68
+
69
+ def patch_fastapi(routes_to_skip: Optional[List[str]] = None):
70
+ return
71
+
72
+ else:
73
+
74
+ # Pre-registered endpoint IDs (maps endpoint function id -> endpoint_id from C extension)
75
+ _ENDPOINT_REGISTRY: dict[int, int] = {}
76
+
77
+ # Track which FastAPI app instances have been registered (to support multiple apps)
78
+ _REGISTERED_APPS: set[int] = set()
79
+
80
+ def _truncate_if_needed(data: bytes, limit: int) -> bytes:
81
+ """Truncate data to limit if needed."""
82
+ if len(data) <= limit:
83
+ return data
84
+ return data[:limit] + b"... [TRUNCATED]"
85
+
86
+ def _serialize_headers(headers: list) -> str:
87
+ """Serialize headers to JSON string (fast path)."""
88
+ try:
89
+ return json.dumps(
90
+ {k.decode(): v.decode() for k, v in headers}, separators=(",", ":")
91
+ )
92
+ except Exception: # noqa: BLE001
93
+ return "{}"
94
+
95
+ class SFZeroOverheadMiddleware:
96
+ """ZERO-OVERHEAD network hop capture middleware.
97
+
98
+ OTEL-STYLE PATTERN:
99
+ - Call C extension directly AFTER send completes
100
+ - C queues message and returns in ~1µs (lock-free ring buffer)
101
+ - C background thread does ALL network work with GIL released
102
+ - ASGI event loop never blocks!
103
+
104
+ This matches OTEL's performance because we use the same pattern!
105
+ """
106
+
107
+ def __init__(self, app: ASGIApp):
108
+ self.app = app
109
+
110
+ async def __call__(self, scope: Scope, receive: Receive, send: Send):
111
+ if scope.get("type") != "http":
112
+ await self.app(scope, receive, send)
113
+ return
114
+
115
+ # Set current request path for route-based suppression (SF_DISABLE_INBOUND_NETWORK_TRACING_ON_ROUTES)
116
+ request_path = scope.get("path", "")
117
+ set_current_request_path(request_path)
118
+
119
+ # Always print to verify middleware is being called
120
+ if SF_DEBUG and app_config._interceptors_initialized:
121
+ print(
122
+ f"[[SFZeroOverheadMiddleware.__call__]] HTTP request to {request_path}, type={scope.get('type')}",
123
+ log=False,
124
+ )
125
+
126
+ # PERFORMANCE: Single-pass bytes-level header scan (no dict allocation until needed)
127
+ # Scan headers once on bytes, only decode what we need, use latin-1 (fast 1:1 byte map)
128
+ hdr_tuples = scope.get("headers") or ()
129
+ incoming_trace_raw = None # bytes
130
+ funcspan_raw = None # bytes
131
+ req_headers = None # dict[str,str] only if capture enabled
132
+
133
+ capture_req_headers = SF_NETWORKHOP_CAPTURE_REQUEST_HEADERS # local cache
134
+
135
+ if capture_req_headers:
136
+ # decode once using latin-1 (1:1 bytes, faster than utf-8 and never throws)
137
+ tmp = {}
138
+ for k, v in hdr_tuples:
139
+ kl = k.lower()
140
+ if kl == SAILFISH_TRACING_HEADER_BYTES:
141
+ incoming_trace_raw = v
142
+ elif kl == FUNCSPAN_OVERRIDE_HEADER_BYTES:
143
+ funcspan_raw = v
144
+ # build the dict while we're here
145
+ tmp[k.decode("latin-1")] = v.decode("latin-1")
146
+ req_headers = tmp
147
+ else:
148
+ for k, v in hdr_tuples:
149
+ kl = k.lower()
150
+ if kl == SAILFISH_TRACING_HEADER_BYTES:
151
+ incoming_trace_raw = v
152
+ elif kl == FUNCSPAN_OVERRIDE_HEADER_BYTES:
153
+ funcspan_raw = v
154
+ # no dict build
155
+
156
+ # CRITICAL: Seed/ensure trace_id immediately (BEFORE any outbound work)
157
+ if incoming_trace_raw:
158
+ # Incoming X-Sf3-Rid header provided - use it
159
+ incoming_trace = incoming_trace_raw.decode("latin-1")
160
+ get_or_set_sf_trace_id(
161
+ incoming_trace, is_associated_with_inbound_request=True
162
+ )
163
+ else:
164
+ # No incoming X-Sf3-Rid header - generate fresh trace_id for this request
165
+ generate_new_trace_id()
166
+
167
+ # Optional funcspan override (decode only if present)
168
+ funcspan_override_header = (
169
+ funcspan_raw.decode("latin-1") if funcspan_raw else None
170
+ )
171
+ if funcspan_override_header:
172
+ try:
173
+ set_funcspan_override(funcspan_override_header)
174
+ if SF_DEBUG and app_config._interceptors_initialized:
175
+ print(
176
+ f"[[SFZeroOverheadMiddleware]] Set function span override from header: {funcspan_override_header}",
177
+ log=False,
178
+ )
179
+ except Exception as e:
180
+ if SF_DEBUG and app_config._interceptors_initialized:
181
+ print(
182
+ f"[[SFZeroOverheadMiddleware]] Failed to set function span override: {e}",
183
+ log=False,
184
+ )
185
+
186
+ # Initialize outbound base without list/allocs from split()
187
+ try:
188
+ trace_id = get_sf_trace_id()
189
+ if trace_id:
190
+ s = str(trace_id)
191
+ i = s.find("/") # session
192
+ j = s.find("/", i + 1) if i != -1 else -1 # page
193
+ if j != -1:
194
+ base_trace = s[:j] # "session/page"
195
+ set_outbound_header_base(
196
+ base_trace=base_trace,
197
+ parent_trace_id=s, # "session/page/uuid"
198
+ funcspan=funcspan_override_header,
199
+ )
200
+ if SF_DEBUG and app_config._interceptors_initialized:
201
+ print(
202
+ f"[[SFZeroOverheadMiddleware]] Initialized outbound header base (base={base_trace[:16]}...)",
203
+ log=False,
204
+ )
205
+ except Exception as e:
206
+ if SF_DEBUG and app_config._interceptors_initialized:
207
+ print(
208
+ f"[[SFZeroOverheadMiddleware]] Failed to initialize outbound header base: {e}",
209
+ log=False,
210
+ )
211
+
212
+ # OPTIMIZATION: Skip ALL capture infrastructure if not capturing network hops
213
+ # We still needed to set up trace_id and outbound header base above (for outbound call tracing)
214
+ # but we can skip all request/response capture overhead
215
+ if not SF_NETWORKHOP_CAPTURE_ENABLED:
216
+ await self.app(scope, receive, send)
217
+ return
218
+
219
+ # NOTE: req_headers already captured in single-pass scan above (if enabled)
220
+
221
+ # Capture request body if enabled (must intercept receive)
222
+ req_body_chunks = []
223
+
224
+ # OPTIMIZATION: Only wrap receive if we need to capture request body
225
+ if SF_NETWORKHOP_CAPTURE_REQUEST_BODY:
226
+
227
+ async def wrapped_receive():
228
+ message = await receive()
229
+ if message["type"] == "http.request":
230
+ body = message.get("body", b"")
231
+ if body:
232
+ req_body_chunks.append(body)
233
+ return message
234
+
235
+ else:
236
+ wrapped_receive = receive
237
+
238
+ # Capture response headers and body if enabled
239
+ resp_headers = None
240
+ resp_body_chunks = []
241
+
242
+ # OPTIMIZATION: Cache debug flag check (avoid repeated lookups)
243
+ _debug_enabled = SF_DEBUG and app_config._interceptors_initialized
244
+
245
+ # OPTIMIZATION: Cache capture flags (avoid repeated global lookups in hot path)
246
+ _capture_resp_headers = SF_NETWORKHOP_CAPTURE_RESPONSE_HEADERS
247
+ _capture_resp_body = SF_NETWORKHOP_CAPTURE_RESPONSE_BODY
248
+
249
+ async def wrapped_send(message: Message):
250
+ nonlocal resp_headers
251
+
252
+ # ULTRA-FAST PATH: Most messages just pass through without any processing
253
+ # Only http.response.body (final) triggers network hop collection
254
+ msg_type = message.get("type")
255
+
256
+ # FAST PATH: Early exit for non-body messages (http.response.start, etc.)
257
+ if msg_type != "http.response.body":
258
+ # Capture response headers if needed (only on http.response.start)
259
+ if _capture_resp_headers and msg_type == "http.response.start":
260
+ try:
261
+ resp_headers = {
262
+ k.decode(): v.decode()
263
+ for k, v in message.get("headers", [])
264
+ }
265
+ except Exception:
266
+ pass
267
+ await send(message)
268
+ return
269
+
270
+ # BODY PATH: Capture body chunks if needed
271
+ if _capture_resp_body:
272
+ body = message.get("body", b"")
273
+ if body:
274
+ resp_body_chunks.append(body)
275
+
276
+ # Send the actual message first
277
+ await send(message)
278
+
279
+ # OPTIMIZATION: Early exit if there's more body chunks coming
280
+ if message.get("more_body", False):
281
+ return
282
+
283
+ # NOW we can get the endpoint (it's been populated by the router)
284
+ endpoint_fn = scope.get("endpoint")
285
+ if not endpoint_fn:
286
+ return # Early exit if no endpoint
287
+
288
+ endpoint_id = _ENDPOINT_REGISTRY.get(id(endpoint_fn))
289
+ if endpoint_id is None or endpoint_id < 0:
290
+ if _debug_enabled:
291
+ print(
292
+ f"[[SFZeroOverheadMiddleware]] Skipping NetworkHop (endpoint_id={endpoint_id}), registry has {len(_ENDPOINT_REGISTRY)} endpoints",
293
+ log=False,
294
+ )
295
+ return # Early exit if invalid endpoint
296
+
297
+ if _debug_enabled:
298
+ print(
299
+ f"[[SFZeroOverheadMiddleware]] Response complete for {scope.get('path')}, endpoint_fn={endpoint_fn.__name__ if hasattr(endpoint_fn, '__name__') else endpoint_fn}, endpoint_id={endpoint_id}",
300
+ log=False,
301
+ )
302
+
303
+ # Only proceed if we have valid endpoint
304
+ try:
305
+ # OPTIMIZATION: Use get_sf_trace_id() directly instead of get_or_set_sf_trace_id()
306
+ # Trace ID is GUARANTEED to be set at request start (lines 111-118)
307
+ # This saves ~11-12μs by avoiding tuple unpacking and conditional logic
308
+ session_id = get_sf_trace_id()
309
+
310
+ # OPTIMIZATION: Consolidate body chunks efficiently
311
+ req_body = None
312
+ if req_body_chunks:
313
+ joined = b"".join(req_body_chunks)
314
+ req_body = (
315
+ joined
316
+ if len(joined) <= _REQUEST_LIMIT_BYTES
317
+ else joined[:_REQUEST_LIMIT_BYTES]
318
+ )
319
+
320
+ resp_body = None
321
+ if resp_body_chunks:
322
+ joined = b"".join(resp_body_chunks)
323
+ resp_body = (
324
+ joined
325
+ if len(joined) <= _RESPONSE_LIMIT_BYTES
326
+ else joined[:_RESPONSE_LIMIT_BYTES]
327
+ )
328
+
329
+ # Direct C call - it queues to background worker, returns instantly
330
+ # No need to check SF_NETWORKHOP_CAPTURE_ENABLED - we return early if it's False (line 170)
331
+ fast_send_network_hop_fast(
332
+ session_id=session_id,
333
+ endpoint_id=endpoint_id,
334
+ raw_path=scope["path"],
335
+ raw_query_string=scope["query_string"],
336
+ request_headers=req_headers,
337
+ request_body=req_body,
338
+ response_headers=resp_headers,
339
+ response_body=resp_body,
340
+ )
341
+ if _debug_enabled:
342
+ print(
343
+ f"[[SFZeroOverheadMiddleware]] Emitted NetworkHop for endpoint_id={endpoint_id}",
344
+ log=False,
345
+ )
346
+ except Exception as e: # noqa: BLE001 S110
347
+ if _debug_enabled:
348
+ print(
349
+ f"[[SFZeroOverheadMiddleware]] Failed to emit NetworkHop: {e}",
350
+ log=False,
351
+ )
352
+
353
+ try:
354
+ await self.app(scope, wrapped_receive, wrapped_send)
355
+ except Exception as exc: # noqa: BLE001
356
+ custom_excepthook(type(exc), exc, exc.__traceback__)
357
+ raise
358
+ finally:
359
+ # CRITICAL: Clear C TLS to prevent stale data in thread pools
360
+ clear_c_tls_parent_trace_id()
361
+
362
+ # CRITICAL: Clear outbound header base to prevent stale cached headers
363
+ # ContextVar does NOT automatically clean up in thread pools - must clear explicitly
364
+ clear_outbound_header_base()
365
+
366
+ # CRITICAL: Clear trace_id to ensure fresh generation for next request
367
+ # Without this, get_or_set_sf_trace_id() reuses trace_id from previous request
368
+ # causing X-Sf4-Prid to stay constant when no incoming X-Sf3-Rid header
369
+ clear_trace_id()
370
+
371
+ # CRITICAL: Clear current request path to prevent stale data in thread pools
372
+ clear_current_request_path()
373
+
374
+ # CRITICAL: Clear function span override for this request (ContextVar cleanup - also syncs C thread-local)
375
+ try:
376
+ clear_funcspan_override()
377
+ except Exception:
378
+ pass
379
+
380
+ def _should_trace_endpoint(endpoint_fn: Callable) -> bool:
381
+ """Check if endpoint should be traced."""
382
+ if getattr(endpoint_fn, _SKIP_TRACING_ATTR, False):
383
+ return False
384
+
385
+ code = getattr(endpoint_fn, "__code__", None)
386
+ if not code:
387
+ return False
388
+
389
+ filename = code.co_filename
390
+ if not _is_user_code(filename):
391
+ return False
392
+
393
+ if getattr(endpoint_fn, "__module__", "").startswith("strawberry"):
394
+ return False
395
+
396
+ return True
397
+
398
+ def _pre_register_endpoints(
399
+ app: "fastapi.FastAPI", routes_to_skip: Optional[List[str]] = None
400
+ ):
401
+ """Pre-register all endpoints at startup."""
402
+ routes_to_skip = routes_to_skip or []
403
+ count = 0
404
+ skipped = 0
405
+
406
+ app_id = id(app)
407
+
408
+ # Check if this app has already been registered
409
+ if app_id in _REGISTERED_APPS:
410
+ if SF_DEBUG and app_config._interceptors_initialized:
411
+ print(
412
+ f"[[_pre_register_endpoints]] App {app_id} already registered, skipping",
413
+ log=False,
414
+ )
415
+ return
416
+
417
+ if SF_DEBUG and app_config._interceptors_initialized:
418
+ print(
419
+ f"[[_pre_register_endpoints]] Starting registration for app {app_id}, app has {len(app.routes)} routes",
420
+ log=False,
421
+ )
422
+
423
+ for route in app.routes:
424
+ if not hasattr(route, "endpoint"):
425
+ skipped += 1
426
+ if SF_DEBUG and app_config._interceptors_initialized:
427
+ print(
428
+ f"[[_pre_register_endpoints]] Skipping route (no endpoint): {route}",
429
+ log=False,
430
+ )
431
+ continue
432
+
433
+ original_endpoint = route.endpoint
434
+ endpoint_fn_id = id(original_endpoint)
435
+
436
+ # Check if this specific endpoint function is already registered
437
+ if endpoint_fn_id in _ENDPOINT_REGISTRY:
438
+ if SF_DEBUG and app_config._interceptors_initialized:
439
+ print(
440
+ f"[[_pre_register_endpoints]] Endpoint function {original_endpoint.__name__ if hasattr(original_endpoint, '__name__') else original_endpoint} already registered (id={_ENDPOINT_REGISTRY[endpoint_fn_id]}), skipping",
441
+ log=False,
442
+ )
443
+ continue
444
+
445
+ # Check for @skip_network_tracing on the wrapped function BEFORE unwrapping
446
+ if getattr(original_endpoint, _SKIP_TRACING_ATTR, False):
447
+ skipped += 1
448
+ if SF_DEBUG and app_config._interceptors_initialized:
449
+ print(
450
+ f"[[_pre_register_endpoints]] Skipping endpoint (marked with @skip_network_tracing): {original_endpoint.__name__ if hasattr(original_endpoint, '__name__') else original_endpoint}",
451
+ log=False,
452
+ )
453
+ continue
454
+
455
+ unwrapped = _unwrap_user_func(original_endpoint)
456
+
457
+ if not _should_trace_endpoint(unwrapped):
458
+ skipped += 1
459
+ if SF_DEBUG and app_config._interceptors_initialized:
460
+ print(
461
+ f"[[_pre_register_endpoints]] Skipping endpoint (not user code): {unwrapped.__name__ if hasattr(unwrapped, '__name__') else unwrapped}",
462
+ log=False,
463
+ )
464
+ continue
465
+
466
+ code = unwrapped.__code__
467
+ line_no_str = str(code.co_firstlineno)
468
+ name = unwrapped.__name__
469
+ filename = code.co_filename
470
+
471
+ # Extract route pattern (e.g., "/log/{n}")
472
+ route_pattern = getattr(route, "path", None)
473
+
474
+ # Check if route should be skipped based on wildcard patterns
475
+ if should_skip_route(route_pattern, routes_to_skip):
476
+ skipped += 1
477
+ if SF_DEBUG and app_config._interceptors_initialized:
478
+ print(
479
+ f"[[_pre_register_endpoints]] Skipping endpoint (route matches skip pattern): {route_pattern}",
480
+ log=False,
481
+ )
482
+ continue
483
+
484
+ endpoint_id = register_endpoint(
485
+ line=line_no_str,
486
+ column="0",
487
+ name=name,
488
+ entrypoint=filename,
489
+ route=route_pattern,
490
+ )
491
+
492
+ if endpoint_id < 0:
493
+ if SF_DEBUG and app_config._interceptors_initialized:
494
+ print(
495
+ f"[[_pre_register_endpoints]] Failed to register {name} (endpoint_id={endpoint_id})",
496
+ log=False,
497
+ )
498
+ continue
499
+
500
+ _ENDPOINT_REGISTRY[endpoint_fn_id] = endpoint_id
501
+ count += 1
502
+
503
+ if SF_DEBUG and app_config._interceptors_initialized:
504
+ print(
505
+ f"[[patch_fastapi]] Registered: {name} @ {filename}:{line_no_str} route={route_pattern} (endpoint_fn_id={endpoint_fn_id}, endpoint_id={endpoint_id})",
506
+ log=False,
507
+ )
508
+
509
+ if SF_DEBUG and app_config._interceptors_initialized:
510
+ print(f"[[patch_fastapi]] Total endpoints registered: {count}", log=False)
511
+
512
+ # Only mark this app as registered if we actually registered user endpoints
513
+ # This allows retry on startup event if routes weren't ready yet
514
+ if count > 0:
515
+ _REGISTERED_APPS.add(app_id)
516
+ if SF_DEBUG and app_config._interceptors_initialized:
517
+ print(f"[[patch_fastapi]] App {app_id} marked as registered", log=False)
518
+ print(
519
+ f"[[patch_fastapi]] OTEL-STYLE: Pure async, direct C call (no Python threads)",
520
+ log=False,
521
+ )
522
+ print(
523
+ f"[[patch_fastapi]] Request headers capture: {'ENABLED' if SF_NETWORKHOP_CAPTURE_REQUEST_HEADERS else 'DISABLED'}",
524
+ log=False,
525
+ )
526
+ print(
527
+ f"[[patch_fastapi]] Request body capture: {'ENABLED' if SF_NETWORKHOP_CAPTURE_REQUEST_BODY else 'DISABLED'}",
528
+ log=False,
529
+ )
530
+ print(
531
+ f"[[patch_fastapi]] Response headers capture: {'ENABLED' if SF_NETWORKHOP_CAPTURE_RESPONSE_HEADERS else 'DISABLED'}",
532
+ log=False,
533
+ )
534
+ print(
535
+ f"[[patch_fastapi]] Response body capture: {'ENABLED' if SF_NETWORKHOP_CAPTURE_RESPONSE_BODY else 'DISABLED'}",
536
+ log=False,
537
+ )
538
+ print(
539
+ f"[[patch_fastapi]] C extension queues to background worker with GIL released",
540
+ log=False,
541
+ )
542
+ else:
543
+ if SF_DEBUG and app_config._interceptors_initialized:
544
+ print(
545
+ f"[[patch_fastapi]] No user endpoints registered yet for app {app_id}, will retry on startup",
546
+ log=False,
547
+ )
548
+
549
+ def patch_fastapi(routes_to_skip: Optional[List[str]] = None):
550
+ """
551
+ OTEL-STYLE PURE ASYNC:
552
+ • Direct C call IMMEDIATELY after send completes
553
+ • ASGI returns instantly (C queues and returns in ~1µs)
554
+ • C background thread does all network work with GIL released
555
+ • This is TRUE zero overhead!
556
+ """
557
+ routes_to_skip = routes_to_skip or []
558
+
559
+ if SF_DEBUG and app_config._interceptors_initialized:
560
+ print("[[patch_fastapi]] Patching FastAPI.__init__", log=False)
561
+
562
+ original_init = fastapi.FastAPI.__init__
563
+
564
+ def patched_init(self, *args, **kwargs):
565
+ original_init(self, *args, **kwargs)
566
+
567
+ if SF_DEBUG and app_config._interceptors_initialized:
568
+ print(
569
+ f"[[patch_fastapi]] FastAPI app created: {self.__class__.__name__}",
570
+ log=False,
571
+ )
572
+
573
+ # Install zero-overhead middleware
574
+ self.add_middleware(SFZeroOverheadMiddleware)
575
+
576
+ # Try to register endpoints immediately if routes are already defined
577
+ # This handles apps where routes are defined before __init__ completes
578
+ if hasattr(self, "routes") and self.routes:
579
+ try:
580
+ if SF_DEBUG and app_config._interceptors_initialized:
581
+ print(
582
+ f"[[patch_fastapi]] Routes already defined ({len(self.routes)} routes), registering immediately",
583
+ log=False,
584
+ )
585
+ _pre_register_endpoints(self, routes_to_skip)
586
+ except Exception as e:
587
+ if SF_DEBUG and app_config._interceptors_initialized:
588
+ print(
589
+ f"[[patch_fastapi]] Immediate registration failed: {e}",
590
+ log=False,
591
+ )
592
+
593
+ # Also register on startup event as a fallback (for routes added after __init__)
594
+ @self.on_event("startup")
595
+ async def _sf_startup():
596
+ # Note: Profiler is already installed by unified_interceptor.py
597
+
598
+ if SF_DEBUG and app_config._interceptors_initialized:
599
+ print(
600
+ "[[patch_fastapi]] Startup event fired, registering endpoints",
601
+ log=False,
602
+ )
603
+ # _pre_register_endpoints now checks if this app was already registered
604
+ _pre_register_endpoints(self, routes_to_skip)
605
+ if SF_DEBUG and app_config._interceptors_initialized:
606
+ print(
607
+ "[[patch_fastapi]] ZERO-OVERHEAD pattern activated (truly async, no blocking)",
608
+ log=False,
609
+ )
610
+
611
+ fastapi.FastAPI.__init__ = patched_init
612
+
613
+ # Also patch any existing FastAPI instances that were created before patching
614
+ # This handles the case where app = FastAPI() happens before setup_interceptors()
615
+ for obj in gc.get_objects():
616
+ try:
617
+ # Wrap in try-except to safely handle lazy objects (e.g., Django settings)
618
+ # that trigger initialization on attribute access
619
+ if isinstance(obj, fastapi.FastAPI):
620
+ # Check if this app already has our middleware
621
+ has_our_middleware = any(
622
+ m.__class__.__name__ == "SFZeroOverheadMiddleware"
623
+ for m in getattr(obj, "user_middleware", [])
624
+ )
625
+ if not has_our_middleware:
626
+ if SF_DEBUG and app_config._interceptors_initialized:
627
+ print(
628
+ f"[[patch_fastapi]] Retroactively patching existing FastAPI app",
629
+ log=False,
630
+ )
631
+ obj.add_middleware(SFZeroOverheadMiddleware)
632
+
633
+ # Try immediate registration if routes exist
634
+ if obj.routes:
635
+ try:
636
+ if SF_DEBUG and app_config._interceptors_initialized:
637
+ print(
638
+ f"[[patch_fastapi]] Retroactive immediate registration ({len(obj.routes)} routes)",
639
+ log=False,
640
+ )
641
+ _pre_register_endpoints(obj, routes_to_skip)
642
+ except Exception as e:
643
+ if SF_DEBUG and app_config._interceptors_initialized:
644
+ print(
645
+ f"[[patch_fastapi]] Retroactive immediate registration failed: {e}",
646
+ log=False,
647
+ )
648
+
649
+ @obj.on_event("startup")
650
+ async def _sf_startup_retro():
651
+ # Note: Profiler is already installed by unified_interceptor.py
652
+
653
+ if SF_DEBUG and app_config._interceptors_initialized:
654
+ print(
655
+ "[[patch_fastapi]] Retroactive startup event fired",
656
+ log=False,
657
+ )
658
+ # _pre_register_endpoints now checks if this app was already registered
659
+ _pre_register_endpoints(obj, routes_to_skip)
660
+ if SF_DEBUG and app_config._interceptors_initialized:
661
+ print(
662
+ "[[patch_fastapi]] Retroactive registration complete",
663
+ log=False,
664
+ )
665
+ except Exception:
666
+ # Silently skip objects that fail isinstance checks (e.g., Django lazy settings)
667
+ pass
668
+
669
+ def patch_fastapi_cors():
670
+ """
671
+ Patch Starlette's CORSMiddleware to automatically inject Sailfish headers.
672
+
673
+ SAFE: Only modifies CORS if CORSMiddleware is used by the application.
674
+ This works for both FastAPI and Starlette apps.
675
+ """
676
+ try:
677
+ from starlette.middleware.cors import CORSMiddleware
678
+ except ImportError:
679
+ # Starlette not installed, skip patching
680
+ if SF_DEBUG and app_config._interceptors_initialized:
681
+ print(
682
+ "[[patch_fastapi_cors]] Starlette CORSMiddleware not found, skipping",
683
+ log=False,
684
+ )
685
+ return
686
+
687
+ # Check if already patched
688
+ if hasattr(CORSMiddleware, "_sf_cors_patched"):
689
+ if SF_DEBUG and app_config._interceptors_initialized:
690
+ print("[[patch_fastapi_cors]] Already patched, skipping", log=False)
691
+ return
692
+
693
+ original_cors_init = CORSMiddleware.__init__
694
+
695
+ def patched_cors_init(
696
+ self,
697
+ app,
698
+ allow_origins=(),
699
+ allow_methods=(),
700
+ allow_headers=(),
701
+ allow_credentials=False,
702
+ allow_origin_regex=None,
703
+ expose_headers=(),
704
+ max_age=600,
705
+ ):
706
+ # Intercept allow_headers parameter
707
+ if should_inject_headers(allow_headers):
708
+ allow_headers = inject_sailfish_headers(allow_headers)
709
+ if SF_DEBUG and app_config._interceptors_initialized:
710
+ print(
711
+ "[[patch_fastapi_cors]] Injected Sailfish headers into CORSMiddleware",
712
+ log=False,
713
+ )
714
+
715
+ # Call original init with potentially modified headers
716
+ original_cors_init(
717
+ self,
718
+ app,
719
+ allow_origins=allow_origins,
720
+ allow_methods=allow_methods,
721
+ allow_headers=allow_headers,
722
+ allow_credentials=allow_credentials,
723
+ allow_origin_regex=allow_origin_regex,
724
+ expose_headers=expose_headers,
725
+ max_age=max_age,
726
+ )
727
+
728
+ CORSMiddleware.__init__ = patched_cors_init
729
+ CORSMiddleware._sf_cors_patched = True
730
+
731
+ if SF_DEBUG and app_config._interceptors_initialized:
732
+ print(
733
+ "[[patch_fastapi_cors]] Successfully patched Starlette CORSMiddleware",
734
+ log=False,
735
+ )
736
+
737
+ # Call CORS patching
738
+ patch_fastapi_cors()