sf-veritas 0.10.3__cp311-cp311-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.

Potentially problematic release.


This version of sf-veritas might be problematic. Click here for more details.

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