From 1b47344f1c1d9505eebd0dd1cef9695fb507fa16 Mon Sep 17 00:00:00 2001 From: isra17 Date: Fri, 21 Oct 2022 17:17:30 -0400 Subject: [PATCH] Urllib3 instrumentation can now retrieve urlopen body parameter when used as positional --- CHANGELOG.md | 2 ++ .../instrumentation/urllib3/__init__.py | 1 + .../tests/test_urllib3_integration.py | 21 +++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e26036875..df6c9167c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.13.0-0.34b0...HEAD) +- Urllib3 instrumentation can now retrieve urlopen body parameter when used as positional + ([#1398](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1398)) - Add metric instrumentation for tornado ([#1252](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1252)) diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py index f32e208ac6..8a9c5e8d1e 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py @@ -117,6 +117,7 @@ def response_hook(span, request, response): _URL_OPEN_ARG_TO_INDEX_MAPPING = { "method": 0, "url": 1, + "body": 2, } diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py index 2e70a9d2ab..ed2f314dcb 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py @@ -309,3 +309,24 @@ def request_hook(span, request, headers, body): ) self.assertIn("request_hook_body", span.attributes) self.assertEqual(span.attributes["request_hook_body"], body) + + def test_request_positional_body(self): + def request_hook(span, request, headers, body): + span.set_attribute("request_hook_body", body) + + URLLib3Instrumentor().uninstrument() + URLLib3Instrumentor().instrument( + request_hook=request_hook, + ) + + body = "param1=1¶m2=2" + + pool = urllib3.HTTPConnectionPool("httpbin.org") + response = pool.urlopen("POST", "/status/200", body) + + self.assertEqual(b"Hello!", response.data) + + span = self.assert_span() + + self.assertIn("request_hook_body", span.attributes) + self.assertEqual(span.attributes["request_hook_body"], body)