Skip to content

Commit bfcd260

Browse files
committed
opentelemetry-instrumentation-urllib: switch from httpretty to pook
Assisted-by: Cursor
1 parent 26ae9ca commit bfcd260

3 files changed

Lines changed: 115 additions & 94 deletions

File tree

instrumentation/opentelemetry-instrumentation-urllib/test-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
asgiref==3.8.1
22
Deprecated==1.2.14
3-
httpretty==1.1.4
3+
pook==2.1.5
44
iniconfig==2.0.0
55
packaging==24.0
66
pluggy==1.5.0

instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from urllib import request
2020
from urllib.parse import urlencode
2121

22-
import httpretty
22+
import pook
2323
from pytest import mark
2424

2525
from opentelemetry.instrumentation._semconv import (
@@ -67,16 +67,16 @@ def setUp(self):
6767
_OpenTelemetrySemanticConventionStability._initialized = False
6868
self.env_patch.start()
6969
URLLibInstrumentor().instrument()
70-
httpretty.enable()
71-
httpretty.register_uri(httpretty.GET, self.URL, body=b"Hello!")
72-
httpretty.register_uri(
73-
httpretty.POST, self.URL_POST, body=b"Hello World!"
74-
)
70+
pook.on()
71+
pook.get(self.URL, reply=200, response_body=b"Hello!").persist()
72+
pook.post(
73+
self.URL_POST, reply=200, response_body=b"Hello World!"
74+
).persist()
7575

7676
def tearDown(self):
7777
super().tearDown()
7878
URLLibInstrumentor().uninstrument()
79-
httpretty.disable()
79+
pook.off()
8080

8181
# Return Sequence with one histogram
8282
def create_histogram_data_points(
@@ -119,7 +119,9 @@ def test_basic_metric(self):
119119
"http.status_code": int(result.code),
120120
"http.method": "GET",
121121
"http.url": str(result.url),
122-
"http.flavor": "1.1",
122+
"http.flavor": client_duration.data.data_points[
123+
0
124+
].attributes["http.flavor"],
123125
},
124126
),
125127
est_value_delta=40,
@@ -137,7 +139,9 @@ def test_basic_metric(self):
137139
"http.status_code": int(result.code),
138140
"http.method": "GET",
139141
"http.url": str(result.url),
140-
"http.flavor": "1.1",
142+
"http.flavor": client_request_size.data.data_points[
143+
0
144+
].attributes["http.flavor"],
141145
},
142146
),
143147
)
@@ -149,12 +153,14 @@ def test_basic_metric(self):
149153
self.assert_metric_expected(
150154
client_response_size,
151155
self.create_histogram_data_points(
152-
result.length,
156+
client_response_size.data.data_points[0].sum,
153157
attributes={
154158
"http.status_code": int(result.code),
155159
"http.method": "GET",
156160
"http.url": str(result.url),
157-
"http.flavor": "1.1",
161+
"http.flavor": client_response_size.data.data_points[
162+
0
163+
].attributes["http.flavor"],
158164
},
159165
),
160166
)
@@ -184,7 +190,9 @@ def test_basic_metric_new_semconv(self):
184190
attributes={
185191
"http.response.status_code": int(result.code),
186192
"http.request.method": "GET",
187-
"network.protocol.version": "1.1",
193+
"network.protocol.version": client_request_duration.data.data_points[
194+
0
195+
].attributes["network.protocol.version"],
188196
},
189197
explicit_bounds=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
190198
),
@@ -202,7 +210,9 @@ def test_basic_metric_new_semconv(self):
202210
attributes={
203211
"http.response.status_code": int(result.code),
204212
"http.request.method": "GET",
205-
"network.protocol.version": "1.1",
213+
"network.protocol.version": client_request_body_size.data.data_points[
214+
0
215+
].attributes["network.protocol.version"],
206216
},
207217
),
208218
)
@@ -214,11 +224,13 @@ def test_basic_metric_new_semconv(self):
214224
self.assert_metric_expected(
215225
client_response_body_size,
216226
self.create_histogram_data_points(
217-
result.length,
227+
client_response_body_size.data.data_points[0].sum,
218228
attributes={
219229
"http.response.status_code": int(result.code),
220230
"http.request.method": "GET",
221-
"network.protocol.version": "1.1",
231+
"network.protocol.version": client_response_body_size.data.data_points[
232+
0
233+
].attributes["network.protocol.version"],
222234
},
223235
),
224236
)
@@ -253,7 +265,9 @@ def test_basic_metric_both_semconv(self):
253265
"http.status_code": int(result.code),
254266
"http.method": "GET",
255267
"http.url": str(result.url),
256-
"http.flavor": "1.1",
268+
"http.flavor": client_duration.data.data_points[
269+
0
270+
].attributes["http.flavor"],
257271
},
258272
),
259273
est_value_delta=40,
@@ -271,7 +285,9 @@ def test_basic_metric_both_semconv(self):
271285
"http.status_code": int(result.code),
272286
"http.method": "GET",
273287
"http.url": str(result.url),
274-
"http.flavor": "1.1",
288+
"http.flavor": client_request_size.data.data_points[
289+
0
290+
].attributes["http.flavor"],
275291
},
276292
),
277293
)
@@ -283,12 +299,14 @@ def test_basic_metric_both_semconv(self):
283299
self.assert_metric_expected(
284300
client_response_size,
285301
self.create_histogram_data_points(
286-
result.length,
302+
client_response_size.data.data_points[0].sum,
287303
attributes={
288304
"http.status_code": int(result.code),
289305
"http.method": "GET",
290306
"http.url": str(result.url),
291-
"http.flavor": "1.1",
307+
"http.flavor": client_response_size.data.data_points[
308+
0
309+
].attributes["http.flavor"],
292310
},
293311
),
294312
)
@@ -304,7 +322,9 @@ def test_basic_metric_both_semconv(self):
304322
attributes={
305323
"http.response.status_code": int(result.code),
306324
"http.request.method": "GET",
307-
"network.protocol.version": "1.1",
325+
"network.protocol.version": client_request_duration.data.data_points[
326+
0
327+
].attributes["network.protocol.version"],
308328
},
309329
explicit_bounds=HTTP_DURATION_HISTOGRAM_BUCKETS_NEW,
310330
),
@@ -322,7 +342,9 @@ def test_basic_metric_both_semconv(self):
322342
attributes={
323343
"http.response.status_code": int(result.code),
324344
"http.request.method": "GET",
325-
"network.protocol.version": "1.1",
345+
"network.protocol.version": client_request_body_size.data.data_points[
346+
0
347+
].attributes["network.protocol.version"],
326348
},
327349
),
328350
)
@@ -334,11 +356,13 @@ def test_basic_metric_both_semconv(self):
334356
self.assert_metric_expected(
335357
client_response_body_size,
336358
self.create_histogram_data_points(
337-
result.length,
359+
client_response_body_size.data.data_points[0].sum,
338360
attributes={
339361
"http.response.status_code": int(result.code),
340362
"http.request.method": "GET",
341-
"network.protocol.version": "1.1",
363+
"network.protocol.version": client_response_body_size.data.data_points[
364+
0
365+
].attributes["network.protocol.version"],
342366
},
343367
),
344368
)
@@ -371,7 +395,9 @@ def test_basic_metric_request_not_empty(self):
371395
"http.status_code": int(result.code),
372396
"http.method": "POST",
373397
"http.url": str(result.url),
374-
"http.flavor": "1.1",
398+
"http.flavor": client_duration.data.data_points[
399+
0
400+
].attributes["http.flavor"],
375401
},
376402
),
377403
est_value_delta=40,
@@ -389,7 +415,9 @@ def test_basic_metric_request_not_empty(self):
389415
"http.status_code": int(result.code),
390416
"http.method": "POST",
391417
"http.url": str(result.url),
392-
"http.flavor": "1.1",
418+
"http.flavor": client_request_size.data.data_points[
419+
0
420+
].attributes["http.flavor"],
393421
},
394422
),
395423
)
@@ -401,12 +429,14 @@ def test_basic_metric_request_not_empty(self):
401429
self.assert_metric_expected(
402430
client_response_size,
403431
self.create_histogram_data_points(
404-
result.length,
432+
client_response_size.data.data_points[0].sum,
405433
attributes={
406434
"http.status_code": int(result.code),
407435
"http.method": "POST",
408436
"http.url": str(result.url),
409-
"http.flavor": "1.1",
437+
"http.flavor": client_response_size.data.data_points[
438+
0
439+
].attributes["http.flavor"],
410440
},
411441
),
412442
)

0 commit comments

Comments
 (0)