-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
521 lines (435 loc) · 17 KB
/
app.py
File metadata and controls
521 lines (435 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
"""Flask test app for e2e tests - urllib3 instrumentation testing."""
import json
import zlib
import urllib3
from flask import Flask, jsonify, request
from drift import TuskDrift
from drift.instrumentation.e2e_common.external_http import (
external_http_timeout_seconds,
upstream_url,
upstream_url_parts,
)
# Initialize SDK
sdk = TuskDrift.initialize(
api_key="tusk-test-key",
log_level="debug",
)
app = Flask(__name__)
EXTERNAL_HTTP_TIMEOUT_SECONDS = external_http_timeout_seconds()
def _configure_urllib3_for_mock_and_timeouts():
original_poolmanager_request = urllib3.PoolManager.request
def patched_poolmanager_request(self, method, url, *args, **kwargs):
kwargs.setdefault("timeout", urllib3.Timeout(total=EXTERNAL_HTTP_TIMEOUT_SECONDS))
return original_poolmanager_request(self, method, upstream_url(str(url)), *args, **kwargs)
urllib3.PoolManager.request = patched_poolmanager_request
_configure_urllib3_for_mock_and_timeouts()
# Create a shared PoolManager for connection reuse
http = urllib3.PoolManager()
# =============================================================================
# Health Check
# =============================================================================
@app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "healthy"})
# =============================================================================
# PoolManager Tests (high-level API)
# =============================================================================
@app.route("/api/poolmanager/get-json", methods=["GET"])
def poolmanager_get_json():
"""Test GET request returning JSON using PoolManager."""
try:
response = http.request("GET", "https://jsonplaceholder.typicode.com/posts/1")
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/get-with-params", methods=["GET"])
def poolmanager_get_with_params():
"""Test GET request with query parameters using PoolManager."""
try:
response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/comments",
fields={"postId": "1"},
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/get-with-headers", methods=["GET"])
def poolmanager_get_with_headers():
"""Test GET request with custom headers using PoolManager."""
try:
response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/posts/1",
headers={
"X-Custom-Header": "test-value",
"Accept": "application/json",
},
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/post-json", methods=["POST"])
def poolmanager_post_json():
"""Test POST request with JSON body using PoolManager."""
try:
req_data = request.get_json() or {}
body = json.dumps(
{
"title": req_data.get("title", "Test Title"),
"body": req_data.get("body", "Test Body"),
"userId": req_data.get("userId", 1),
}
)
response = http.request(
"POST",
"https://jsonplaceholder.typicode.com/posts",
body=body.encode("utf-8"),
headers={"Content-Type": "application/json"},
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/post-form", methods=["POST"])
def poolmanager_post_form():
"""Test POST request with form-encoded data using PoolManager."""
try:
response = http.request(
"POST",
"https://jsonplaceholder.typicode.com/posts",
fields={
"title": "Form Title",
"body": "Form Body",
"userId": "1",
},
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/put-json", methods=["PUT"])
def poolmanager_put_json():
"""Test PUT request with JSON body using PoolManager."""
try:
req_data = request.get_json() or {}
body = json.dumps(
{
"id": 1,
"title": req_data.get("title", "Updated Title"),
"body": req_data.get("body", "Updated Body"),
"userId": req_data.get("userId", 1),
}
)
response = http.request(
"PUT",
"https://jsonplaceholder.typicode.com/posts/1",
body=body.encode("utf-8"),
headers={"Content-Type": "application/json"},
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/patch-json", methods=["PATCH"])
def poolmanager_patch_json():
"""Test PATCH request with partial JSON body using PoolManager."""
try:
req_data = request.get_json() or {}
body = json.dumps(
{
"title": req_data.get("title", "Patched Title"),
}
)
response = http.request(
"PATCH",
"https://jsonplaceholder.typicode.com/posts/1",
body=body.encode("utf-8"),
headers={"Content-Type": "application/json"},
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/delete", methods=["DELETE"])
def poolmanager_delete():
"""Test DELETE request using PoolManager."""
try:
response = http.request("DELETE", "https://jsonplaceholder.typicode.com/posts/1")
return jsonify({"status": "deleted", "status_code": response.status})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/poolmanager/chain", methods=["GET"])
def poolmanager_chain():
"""Test sequential chained requests using PoolManager."""
try:
# First request: get a user
user_response = http.request("GET", "https://jsonplaceholder.typicode.com/users/1")
user = json.loads(user_response.data.decode("utf-8"))
# Second request: get posts by that user
posts_response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/posts",
fields={"userId": str(user["id"])},
)
posts = json.loads(posts_response.data.decode("utf-8"))
# Third request: get comments on the first post
if posts:
comments_response = http.request(
"GET",
f"https://jsonplaceholder.typicode.com/posts/{posts[0]['id']}/comments",
)
comments = json.loads(comments_response.data.decode("utf-8"))
else:
comments = []
return jsonify(
{
"user": user,
"post_count": len(posts),
"first_post_comments": len(comments),
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
# =============================================================================
# HTTPConnectionPool Tests (low-level API)
# =============================================================================
@app.route("/api/connectionpool/get-json", methods=["GET"])
def connectionpool_get_json():
"""Test GET request using HTTPConnectionPool directly."""
pool = None
try:
scheme, host, port, path = upstream_url_parts("https://jsonplaceholder.typicode.com/posts/2")
pool_cls = urllib3.HTTPSConnectionPool if scheme == "https" else urllib3.HTTPConnectionPool
pool = pool_cls(host, port=port)
response = pool.request("GET", path)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
if pool is not None:
pool.close()
@app.route("/api/connectionpool/post-json", methods=["POST"])
def connectionpool_post_json():
"""Test POST request using HTTPConnectionPool directly."""
pool = None
try:
req_data = request.get_json() or {}
body = json.dumps(
{
"title": req_data.get("title", "Pool Test Title"),
"body": req_data.get("body", "Pool Test Body"),
"userId": req_data.get("userId", 2),
}
)
scheme, host, port, path = upstream_url_parts("https://jsonplaceholder.typicode.com/posts")
pool_cls = urllib3.HTTPSConnectionPool if scheme == "https" else urllib3.HTTPConnectionPool
pool = pool_cls(host, port=port)
response = pool.request(
"POST",
path,
body=body.encode("utf-8"),
headers={"Content-Type": "application/json"},
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
if pool is not None:
pool.close()
# =============================================================================
# Additional Test Cases
# =============================================================================
@app.route("/test/timeout", methods=["GET"])
def test_timeout():
"""Test request with explicit timeout."""
try:
response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/posts/3",
timeout=urllib3.Timeout(connect=5.0, read=10.0),
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/retries", methods=["GET"])
def test_retries():
"""Test request with retry configuration."""
try:
response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/posts/4",
retries=urllib3.Retry(total=3, backoff_factor=0.1),
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/binary-response", methods=["GET"])
def test_binary_response():
"""Test handling of binary response (should be handled gracefully)."""
try:
# Fetch a small image
response = http.request(
"GET",
"https://httpbin.org/image/png",
headers={"Accept": "image/png"},
)
return jsonify(
{
"status": response.status,
"content_type": response.headers.get("Content-Type", ""),
"content_length": len(response.data),
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/redirect", methods=["GET"])
def test_redirect():
"""Test following redirects."""
try:
response = http.request(
"GET",
"https://httpbin.org/redirect/2",
redirect=True,
)
return jsonify(
{
"status": response.status,
"final_url": response.geturl() if hasattr(response, "geturl") else "unknown",
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/new-poolmanager", methods=["GET"])
def test_new_poolmanager():
"""Test with a fresh PoolManager instance per request."""
try:
local_http = urllib3.PoolManager()
response = local_http.request("GET", "https://jsonplaceholder.typicode.com/posts/5")
data = json.loads(response.data.decode("utf-8"))
local_http.clear()
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/basic-auth", methods=["GET"])
def test_basic_auth():
"""Test request with basic authentication."""
try:
# Create authorization header for basic auth
import base64
credentials = base64.b64encode(b"testuser:testpass").decode("ascii")
headers = urllib3.make_headers(basic_auth="testuser:testpass")
response = http.request(
"GET",
"https://httpbin.org/basic-auth/testuser/testpass",
headers=headers,
)
data = json.loads(response.data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/multiple-requests", methods=["GET"])
def test_multiple_requests():
"""Test multiple requests in a single endpoint."""
try:
results = []
# Make three sequential requests
for i in range(1, 4):
response = http.request("GET", f"https://jsonplaceholder.typicode.com/posts/{i}")
data = json.loads(response.data.decode("utf-8"))
results.append({"id": data["id"], "title": data["title"]})
return jsonify({"posts": results})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/requests-lib", methods=["GET"])
def test_requests_lib():
"""Test using requests library (which uses urllib3 internally).
This test verifies that we don't create double spans when requests
library is used, since requests uses urllib3 under the hood.
"""
import requests as requests_lib
try:
response = requests_lib.get(
upstream_url("https://jsonplaceholder.typicode.com/posts/10"),
timeout=EXTERNAL_HTTP_TIMEOUT_SECONDS,
)
return jsonify(response.json())
except Exception as e:
return jsonify({"error": str(e)}), 500
# =============================================================================
# Bug Detection Tests - Confirmed bugs that expose instrumentation issues
# =============================================================================
@app.route("/test/preload-content-false-read", methods=["GET"])
def test_preload_content_false_read():
"""Test preload_content=False with manual read().
This is the pattern botocore/boto3 uses: request with preload_content=False,
then call response.read() to get the body. The instrumentation must buffer
the body in _fp (BytesIO) during recording so both the span capture and the
caller's read() work correctly.
"""
try:
response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/posts/21",
preload_content=False,
)
data_bytes = response.read()
response.release_conn()
data = json.loads(data_bytes.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/preload-content-false-crc32", methods=["GET"])
def test_preload_content_false_crc32():
"""Test preload_content=False with CRC32 checksum validation.
Mimics botocore's DynamoDB flow: read the body via read(), then validate
the CRC32 checksum against a header value. This failed before the fix
because the mock response's BytesIO was exhausted by preload_content=True,
causing read() to return b"" and CRC32 to be 0.
"""
try:
response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/posts/22",
preload_content=False,
)
body = response.read()
response.release_conn()
if not body:
return jsonify({"error": "Empty body from read()"}), 500
actual_crc32 = zlib.crc32(body) & 0xFFFFFFFF
data = json.loads(body.decode("utf-8"))
return jsonify({**data, "crc32": actual_crc32, "body_length": len(body)})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/test/preload-content-false-stream", methods=["GET"])
def test_preload_content_false_stream():
"""Test preload_content=False with chunked stream() reading.
The instrumentation buffers the body into a BytesIO, so subsequent
stream() calls read from that BytesIO in chunks as normal.
"""
try:
response = http.request(
"GET",
"https://jsonplaceholder.typicode.com/posts/27",
preload_content=False,
)
chunks = []
for chunk in response.stream(32):
chunks.append(chunk)
response.release_conn()
full_data = b"".join(chunks)
data = json.loads(full_data.decode("utf-8"))
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
sdk.mark_app_as_ready()
app.run(host="0.0.0.0", port=8000, debug=False)