From 725944d3685c9d50ad14fd88e02b1d67ea780fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isra=C3=ABl=20Hall=C3=A9?= Date: Tue, 15 Nov 2022 20:30:22 -0500 Subject: [PATCH] Urllib3 instrumentation can now retrieve urlopen body parameter when used as positional (#1398) --- CHANGELOG.md | 9 +++++--- .../instrumentation/urllib3/__init__.py | 1 + .../tests/test_urllib3_integration.py | 21 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 626c29bb2..cfe3bc8d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Add metric instrumentation for tornado + ([#1252](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1252)) + ### Added - `opentelemetry-instrumentation-pymysql` Add tests for commit() and rollback(). @@ -20,9 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fix bug in Urllib instrumentation - add status code to span attributes only if the status code is not None. +- Fix bug in Urllib instrumentation - add status code to span attributes only if the status code is not None. ([#1430](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1430)) -- `opentelemetry-instrumentation-aiohttp-client` Allow overriding of status in response hook. +- `opentelemetry-instrumentation-aiohttp-client` Allow overriding of status in response hook. ([#1394](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1394)) - `opentelemetry-instrumentation-pymysql` Fix dbapi connection instrument wrapper has no _sock member. ([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424)) @@ -88,7 +91,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- `opentelemetry-instrumentation-grpc` add supports to filter requests to instrument. +- `opentelemetry-instrumentation-grpc` add supports to filter requests to instrument. ([#1241](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1241)) - Flask sqlalchemy psycopg2 integration ([#1224](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1224)) 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 59d7cd35b..02f701068 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py @@ -118,6 +118,7 @@ _ResponseHookT = typing.Optional[ _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 2e70a9d2a..ed2f314dc 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 @@ class TestURLLib3Instrumentor(TestBase): ) 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)