Conversation
There was a problem hiding this comment.
4 issues found across 5 files
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="drift/instrumentation/httpx/instrumentation.py">
<violation number="1" location="drift/instrumentation/httpx/instrumentation.py:175">
P1: `span.end()` is called twice when request is dropped by transform - once here and again in the outer finally block. Remove this call since the finally block already handles span termination.</violation>
<violation number="2" location="drift/instrumentation/httpx/instrumentation.py:199">
P2: Dead code: duration calculation result is discarded. Either remove this line or assign the result to a variable for use (e.g., passing to `_finalize_span`).</violation>
<violation number="3" location="drift/instrumentation/httpx/instrumentation.py:412">
P2: Potential TypeError when headers=None in async mock lookup; guard with `kwargs.get("headers") or {}` before dict().</violation>
<violation number="4" location="drift/instrumentation/httpx/instrumentation.py:656">
P2: Potential TypeError during span finalization when headers=None; guard with `request_kwargs.get("headers") or {}` before dict().</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| json.dumps({"bodyProcessingError": "dropped"}), | ||
| ) | ||
| span.set_status(Status(OTelStatusCode.ERROR, "Dropped by transform")) | ||
| span.end() |
There was a problem hiding this comment.
P1: span.end() is called twice when request is dropped by transform - once here and again in the outer finally block. Remove this call since the finally block already handles span termination.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At drift/instrumentation/httpx/instrumentation.py, line 175:
<comment>`span.end()` is called twice when request is dropped by transform - once here and again in the outer finally block. Remove this call since the finally block already handles span termination.</comment>
<file context>
@@ -0,0 +1,992 @@
+ json.dumps({"bodyProcessingError": "dropped"}),
+ )
+ span.set_status(Status(OTelStatusCode.ERROR, "Dropped by transform"))
+ span.end()
+
+ raise RequestDroppedByTransform(
</file context>
| finally: | ||
| calling_library_context.reset(calling_lib_token) | ||
| # Finalize span with request/response data | ||
| (time.time_ns() - start_time_ns) / 1_000_000 |
There was a problem hiding this comment.
P2: Dead code: duration calculation result is discarded. Either remove this line or assign the result to a variable for use (e.g., passing to _finalize_span).
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At drift/instrumentation/httpx/instrumentation.py, line 199:
<comment>Dead code: duration calculation result is discarded. Either remove this line or assign the result to a variable for use (e.g., passing to `_finalize_span`).</comment>
<file context>
@@ -0,0 +1,992 @@
+ finally:
+ calling_library_context.reset(calling_lib_token)
+ # Finalize span with request/response data
+ (time.time_ns() - start_time_ns) / 1_000_000
+ instrumentation._finalize_span(
+ span,
</file context>
| (time.time_ns() - start_time_ns) / 1_000_000 | |
| duration_ms = (time.time_ns() - start_time_ns) / 1_000_000 |
| parsed_url = urlparse(url) | ||
|
|
||
| # ===== BUILD INPUT VALUE ===== | ||
| headers = dict(request_kwargs.get("headers", {})) |
There was a problem hiding this comment.
P2: Potential TypeError during span finalization when headers=None; guard with request_kwargs.get("headers") or {} before dict().
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At drift/instrumentation/httpx/instrumentation.py, line 656:
<comment>Potential TypeError during span finalization when headers=None; guard with `request_kwargs.get("headers") or {}` before dict().</comment>
<file context>
@@ -0,0 +1,992 @@
+ parsed_url = urlparse(url)
+
+ # ===== BUILD INPUT VALUE =====
+ headers = dict(request_kwargs.get("headers", {}))
+ params = dict(request_kwargs.get("params", {})) if request_kwargs.get("params") else {}
+
</file context>
| headers = dict(request_kwargs.get("headers", {})) | |
| headers = dict(request_kwargs.get("headers") or {}) |
| parsed_url = urlparse(url) | ||
|
|
||
| # Extract request data | ||
| headers = dict(kwargs.get("headers", {})) |
There was a problem hiding this comment.
P2: Potential TypeError when headers=None in async mock lookup; guard with kwargs.get("headers") or {} before dict().
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At drift/instrumentation/httpx/instrumentation.py, line 412:
<comment>Potential TypeError when headers=None in async mock lookup; guard with `kwargs.get("headers") or {}` before dict().</comment>
<file context>
@@ -0,0 +1,992 @@
+ parsed_url = urlparse(url)
+
+ # Extract request data
+ headers = dict(kwargs.get("headers", {}))
+ params = dict(kwargs.get("params", {})) if kwargs.get("params") else {}
+
</file context>
| headers = dict(kwargs.get("headers", {})) | |
| headers = dict(kwargs.get("headers") or {}) |
Summary
Adds instrumentation for the httpx HTTP client library, supporting both synchronous (
httpx.Client) and asynchronous (httpx.AsyncClient) clients.Changes
New files:
drift/instrumentation/httpx/__init__.py- Package exportsdrift/instrumentation/httpx/instrumentation.py- Main instrumentationModified files:
drift/core/drift_sdk.py- Auto-initialize httpx instrumentation when the library is availabledrift/instrumentation/requests/instrumentation.py- Setcalling_library_contextduring network callsdrift/instrumentation/socket/instrumentation.py- Fix unpatched dependency detection logicFeatures
find_mock_response_async/find_mock_response_syncHttpTransformEnginefor drop/redact/mask transformshttpx.AsyncClientSocket instrumentation fix
The socket instrumentation detects unpatched dependencies by monitoring raw TCP calls during REPLAY mode. Previously, it only allowed
ProtobufCommunicator(SDK's internal communication) and would incorrectly flag instrumented HTTP libraries as "unpatched".The fix changes the check from:
to:
Both
httpxandrequestsinstrumentations now setcalling_library_contextwhen making network calls, preventing false positives.Testing
HttpxInstrumentationCLIENT spans)