Skip to content

Commit 34a539d

Browse files
fix: requests instrumentation fixes (#23)
1 parent 9de29ad commit 34a539d

3 files changed

Lines changed: 242 additions & 82 deletions

File tree

drift/instrumentation/requests/e2e-tests/src/app.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,71 @@ def text_response():
262262
return jsonify({"error": str(e)}), 500
263263

264264

265+
@app.route("/test/session-send-direct", methods=["GET"])
266+
def session_send_direct():
267+
try:
268+
session = requests.Session()
269+
270+
# Create a PreparedRequest manually
271+
req = requests.Request("GET", "https://jsonplaceholder.typicode.com/posts/2")
272+
prepared = session.prepare_request(req)
273+
274+
# Call send() directly - bypasses request() method
275+
response = session.send(prepared, timeout=10)
276+
277+
return jsonify({"status_code": response.status_code, "data": response.json(), "test": "session-send-direct"})
278+
except Exception as e:
279+
return jsonify({"error": str(e)}), 500
280+
281+
282+
@app.route("/test/streaming-iter-lines", methods=["GET"])
283+
def test_streaming_iter_lines():
284+
"""Test streaming response using iter_lines."""
285+
try:
286+
response = requests.get(
287+
"https://httpbin.org/stream/5", # Returns 5 JSON lines
288+
stream=True,
289+
timeout=10,
290+
)
291+
# Consume the stream using iter_lines
292+
lines = []
293+
for line in response.iter_lines():
294+
if line:
295+
lines.append(line.decode("utf-8"))
296+
297+
return jsonify(
298+
{"test": "streaming-iter-lines", "status_code": response.status_code, "lines_received": len(lines)}
299+
)
300+
except Exception as e:
301+
return jsonify({"error": str(e)}), 500
302+
303+
304+
@app.route("/test/response-hooks", methods=["GET"])
305+
def test_response_hooks():
306+
"""Test response hooks that modify response."""
307+
try:
308+
hook_called = {"value": False}
309+
hook_status = {"value": 0}
310+
311+
def response_hook(response, *args, **kwargs):
312+
hook_called["value"] = True
313+
hook_status["value"] = response.status_code
314+
return response
315+
316+
response = requests.get("https://httpbin.org/get", hooks={"response": response_hook}, timeout=10)
317+
318+
return jsonify(
319+
{
320+
"test": "response-hooks",
321+
"status_code": response.status_code,
322+
"hook_called": hook_called["value"],
323+
"hook_status": hook_status["value"],
324+
}
325+
)
326+
except Exception as e:
327+
return jsonify({"error": str(e)}), 500
328+
329+
265330
if __name__ == "__main__":
266331
sdk.mark_app_as_ready()
267332
app.run(host="0.0.0.0", port=8000, debug=False)

drift/instrumentation/requests/e2e-tests/src/test_requests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@ def make_request(method, endpoint, **kwargs):
7070
# Text response handling
7171
make_request("GET", "/api/text-response")
7272

73+
make_request("GET", "/test/session-send-direct")
74+
75+
make_request("GET", "/test/streaming-iter-lines")
76+
77+
make_request("GET", "/test/response-hooks")
78+
7379
print("\nAll requests completed successfully")

0 commit comments

Comments
 (0)