Skip to content

Commit 0054033

Browse files
committed
Fix exception in Urllib3 when dealing with filelike body.
1 parent 99f29b4 commit 0054033

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
([#1252](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1252))
1111
- Fix bug in Falcon instrumentation
1212
([#1377](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1377))
13+
- Fix exception in Urllib3 when dealing with filelike body.
14+
([#1399](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1399))
1315

1416

1517
### Added

instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def response_hook(span, request, response):
6464
---
6565
"""
6666

67+
import collections.abc
6768
import contextlib
6869
import typing
6970
from timeit import default_timer
@@ -212,7 +213,11 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
212213
if callable(response_hook):
213214
response_hook(span, instance, response)
214215

215-
request_size = 0 if body is None else len(body)
216+
request_size = None
217+
if body is None:
218+
request_size = 0
219+
elif isinstance(body, collections.abc.Sized):
220+
request_size = len(body)
216221
response_size = int(response.headers.get("Content-Length", 0))
217222
metric_attributes = _create_metric_attributes(
218223
instance, response, method
@@ -221,9 +226,10 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
221226
duration_histogram.record(
222227
elapsed_time, attributes=metric_attributes
223228
)
224-
request_size_histogram.record(
225-
request_size, attributes=metric_attributes
226-
)
229+
if request_size is not None:
230+
request_size_histogram.record(
231+
request_size, attributes=metric_attributes
232+
)
227233
response_size_histogram.record(
228234
response_size, attributes=metric_attributes
229235
)

instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import json
1515
import typing
16+
from io import BytesIO
1617
from unittest import mock
1718

1819
import httpretty
@@ -309,3 +310,12 @@ def request_hook(span, request, headers, body):
309310
)
310311
self.assertIn("request_hook_body", span.attributes)
311312
self.assertEqual(span.attributes["request_hook_body"], body)
313+
314+
def test_request_body_stream(self):
315+
URLLib3Instrumentor().uninstrument()
316+
URLLib3Instrumentor().instrument()
317+
body = BytesIO(b"123")
318+
319+
pool = urllib3.HTTPConnectionPool("httpbin.org")
320+
response = pool.request("POST", "/status/200", body=body)
321+
self.assertEqual(b"Hello!", response.data)

0 commit comments

Comments
 (0)