Skip to content

Commit 803bb32

Browse files
xrmxlzchen
andauthored
opentelemetry-instrumentation-httpx: make instrument_client a staticmethod again (#3003)
* opentelemetry-instrumentation-httpx: make instrument_client a staticmethod again * ADd changelog * Can be a classmethod --------- Co-authored-by: Leighton Chen <[email protected]>
1 parent c32cc7a commit 803bb32

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020

2121
### Fixed
2222

23+
- `opentelemetry-instrumentation-httpx`: instrument_client is a static method again
24+
([#3003](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3003))
25+
2326
### Breaking changes
2427

2528
- `opentelemetry-instrumentation-sqlalchemy` teach instruments version

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,9 @@ async def _handle_async_request_wrapper( # pylint: disable=too-many-locals
938938

939939
return response
940940

941+
@classmethod
941942
def instrument_client(
942-
self,
943+
cls,
943944
client: typing.Union[httpx.Client, httpx.AsyncClient],
944945
tracer_provider: TracerProvider = None,
945946
request_hook: typing.Union[
@@ -996,7 +997,7 @@ def instrument_client(
996997
client._transport,
997998
"handle_request",
998999
partial(
999-
self._handle_request_wrapper,
1000+
cls._handle_request_wrapper,
10001001
tracer=tracer,
10011002
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
10021003
request_hook=request_hook,
@@ -1008,7 +1009,7 @@ def instrument_client(
10081009
transport,
10091010
"handle_request",
10101011
partial(
1011-
self._handle_request_wrapper,
1012+
cls._handle_request_wrapper,
10121013
tracer=tracer,
10131014
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
10141015
request_hook=request_hook,
@@ -1021,7 +1022,7 @@ def instrument_client(
10211022
client._transport,
10221023
"handle_async_request",
10231024
partial(
1024-
self._handle_async_request_wrapper,
1025+
cls._handle_async_request_wrapper,
10251026
tracer=tracer,
10261027
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
10271028
async_request_hook=async_request_hook,
@@ -1033,7 +1034,7 @@ def instrument_client(
10331034
transport,
10341035
"handle_async_request",
10351036
partial(
1036-
self._handle_async_request_wrapper,
1037+
cls._handle_async_request_wrapper,
10371038
tracer=tracer,
10381039
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
10391040
async_request_hook=async_request_hook,

instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,13 +910,20 @@ def test_suppress_instrumentation_new_client(self):
910910

911911
self.assert_span(num_spans=0)
912912

913-
def test_instrument_client(self):
913+
def test_instrument_client_called_on_the_instance(self):
914914
client = self.create_client()
915915
HTTPXClientInstrumentor().instrument_client(client)
916916
result = self.perform_request(self.URL, client=client)
917917
self.assertEqual(result.text, "Hello!")
918918
self.assert_span(num_spans=1)
919919

920+
def test_instrument_client_called_on_the_class(self):
921+
client = self.create_client()
922+
HTTPXClientInstrumentor.instrument_client(client)
923+
result = self.perform_request(self.URL, client=client)
924+
self.assertEqual(result.text, "Hello!")
925+
self.assert_span(num_spans=1)
926+
920927
def test_instrumentation_without_client(self):
921928
HTTPXClientInstrumentor().instrument()
922929
results = [

0 commit comments

Comments
 (0)