Skip to content

Commit

Permalink
Update some test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx committed Oct 18, 2024
1 parent d1594cd commit b548224
Showing 1 changed file with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import httpx
import respx
from wrapt import ObjectProxy

import opentelemetry.instrumentation.httpx
from opentelemetry import trace
Expand Down Expand Up @@ -168,6 +169,10 @@ def setUp(self):

HTTPXClientInstrumentor().instrument()

def print_spans(self, spans):
for span in spans:
print(span.name, span.attributes)

# pylint: disable=invalid-name
def tearDown(self):
super().tearDown()
Expand All @@ -181,7 +186,9 @@ def assert_span(
if exporter is None:
exporter = self.memory_exporter
span_list = exporter.get_finished_spans()
self.assertEqual(num_spans, len(span_list))
self.assertEqual(
num_spans, len(span_list), self.print_spans(span_list)
)
if num_spans == 0:
return None
if num_spans == 1:
Expand Down Expand Up @@ -760,14 +767,25 @@ def create_proxy_mounts(self):
),
}

def assert_proxy_mounts(self, mounts, num_mounts, transport_type):
def assert_proxy_mounts(self, mounts, num_mounts, transport_type=None):
self.assertEqual(len(mounts), num_mounts)
for transport in mounts:
with self.subTest(transport):
self.assertIsInstance(
transport,
transport_type,
)
if transport_type:
self.assertIsInstance(
transport,
transport_type,
)
else:
handler = getattr(transport, "handle_request", None)
if not handler:
handler = getattr(
transport, "handle_async_request"
)
self.assertTrue(
isinstance(handler, ObjectProxy)
and getattr(handler, "__wrapped__")
)

def test_custom_tracer_provider(self):
resource = resources.Resource.create({})
Expand Down Expand Up @@ -989,9 +1007,25 @@ def test_instrument_proxy(self):
self.assert_proxy_mounts(
client._mounts.values(),
2,
(SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport),
)

def print_handler(self, client):
transport = client._transport
handler = getattr(
transport,
"handle_request",
getattr(transport, "handle_async_request", None),
)
print(
handler,
(
getattr(handler, "__wrapped__", "no wrapped")
if handler
else "no handler"
),
)
return handler

def test_instrument_client_with_proxy(self):
HTTPXClientInstrumentor().uninstrument()
proxy_mounts = self.create_proxy_mounts()
Expand All @@ -1008,7 +1042,6 @@ def test_instrument_client_with_proxy(self):
self.assert_proxy_mounts(
client._mounts.values(),
2,
(SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport),
)
HTTPXClientInstrumentor().uninstrument_client(client)

Expand All @@ -1018,13 +1051,13 @@ def test_uninstrument_client_with_proxy(self):
self.assert_proxy_mounts(
client._mounts.values(),
2,
(SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport),
)

HTTPXClientInstrumentor().uninstrument_client(client)
result = self.perform_request(self.URL, client=client)

self.assertEqual(result.text, "Hello!")
# FIXME: this does fail if uninstrument() has been called before and is a change of behaviour from before
self.assert_span(num_spans=0)
self.assert_proxy_mounts(
client._mounts.values(),
Expand Down

0 comments on commit b548224

Please sign in to comment.