From face2a3272b2ccda4c1692bbffa238f9b2cd6efb Mon Sep 17 00:00:00 2001 From: alrex Date: Thu, 14 Oct 2021 15:19:02 -0700 Subject: [PATCH] refactor common test code to use utility method (#700) --- .../tests/test_middleware.py | 18 +++--------------- .../instrumentation/falcon/__init__.py | 1 - .../tests/test_falcon.py | 17 +++++------------ .../tests/test_programmatic.py | 12 ++---------- .../tests/test_programmatic.py | 12 ++---------- .../tests/test_instrumentation.py | 15 +++------------ 6 files changed, 15 insertions(+), 60 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index c9142472c..99ee57da8 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -34,12 +34,7 @@ from opentelemetry.sdk.trace import Span from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase from opentelemetry.test.wsgitestutil import WsgiTestBase -from opentelemetry.trace import ( - SpanKind, - StatusCode, - format_span_id, - format_trace_id, -) +from opentelemetry.trace import SpanKind, StatusCode from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs # pylint: disable=import-error @@ -345,15 +340,8 @@ class TestMiddleware(TestBase, WsgiTestBase): set_global_response_propagator(TraceResponsePropagator()) response = Client().get("/span_name/1234/") - span = self.memory_exporter.get_finished_spans()[0] - - self.assertIn("traceresponse", response.headers) - self.assertEqual( - response.headers["Access-Control-Expose-Headers"], "traceresponse", - ) - self.assertEqual( - response.headers["traceresponse"], - f"00-{format_trace_id(span.get_span_context().trace_id)}-{format_span_id(span.get_span_context().span_id)}-01", + self.assertTraceResponseHeaderMatchesSpan( + response.headers, self.memory_exporter.get_finished_spans()[0] ) self.memory_exporter.clear() diff --git a/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py b/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py index 5a576dda4..efe4dd86c 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py @@ -291,7 +291,6 @@ class _TraceMiddleware: reason = "NotFound" else: status = "500" - reason = "{}: {}".format(exc_type.__name__, exc) reason = f"{exc_type.__name__}: {exc}" status = status.split(" ")[0] diff --git a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py index dbab45a52..677ea313b 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py @@ -25,7 +25,8 @@ from opentelemetry.instrumentation.propagators import ( from opentelemetry.sdk.resources import Resource from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase -from opentelemetry.trace import StatusCode, format_span_id, format_trace_id +from opentelemetry.test.wsgitestutil import WsgiTestBase +from opentelemetry.trace import StatusCode from .app import make_app @@ -58,7 +59,7 @@ class TestFalconBase(TestBase): self.env_patch.stop() -class TestFalconInstrumentation(TestFalconBase): +class TestFalconInstrumentation(TestFalconBase, WsgiTestBase): def test_get(self): self._test_method("GET") @@ -198,16 +199,8 @@ class TestFalconInstrumentation(TestFalconBase): set_global_response_propagator(TraceResponsePropagator()) response = self.client().simulate_get(path="/hello?q=abc") - headers = response.headers - span = self.memory_exporter.get_finished_spans()[0] - - self.assertIn("traceresponse", headers) - self.assertEqual( - headers["access-control-expose-headers"], "traceresponse", - ) - self.assertEqual( - headers["traceresponse"], - f"00-{format_trace_id(span.get_span_context().trace_id)}-{format_span_id(span.get_span_context().span_id)}-01", + self.assertTraceResponseHeaderMatchesSpan( + response.headers, self.memory_exporter.get_finished_spans()[0] ) set_global_response_propagator(orig) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index ced2927e6..21fd23982 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py @@ -153,19 +153,11 @@ class TestProgrammatic(InstrumentationTest, TestBase, WsgiTestBase): set_global_response_propagator(TraceResponsePropagator()) response = self.client.get("/hello/123") - headers = response.headers - span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) - span = span_list[0] - self.assertIn("traceresponse", headers) - self.assertEqual( - headers["access-control-expose-headers"], "traceresponse", - ) - self.assertEqual( - headers["traceresponse"], - f"00-{trace.format_trace_id(span.get_span_context().trace_id)}-{trace.format_span_id(span.get_span_context().span_id)}-01", + self.assertTraceResponseHeaderMatchesSpan( + response.headers, span_list[0], ) set_global_response_propagator(orig) diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py index 1e3263fa0..ac6780936 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py @@ -112,16 +112,8 @@ class TestProgrammatic(InstrumentationTest, TestBase, WsgiTestBase): set_global_response_propagator(TraceResponsePropagator()) response = self.client.get("/hello/500") - headers = response.headers - span = self.memory_exporter.get_finished_spans()[0] - - self.assertIn("traceresponse", headers) - self.assertEqual( - headers["access-control-expose-headers"], "traceresponse", - ) - self.assertEqual( - headers["traceresponse"], - f"00-{trace.format_trace_id(span.get_span_context().trace_id)}-{trace.format_span_id(span.get_span_context().span_id)}-01", + self.assertTraceResponseHeaderMatchesSpan( + response.headers, self.memory_exporter.get_finished_spans()[0] ) set_global_response_propagator(orig) diff --git a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py index 13cd7071f..ab53932ce 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py @@ -30,6 +30,7 @@ from opentelemetry.instrumentation.tornado import ( ) from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase +from opentelemetry.test.wsgitestutil import WsgiTestBase from opentelemetry.trace import SpanKind from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs @@ -110,7 +111,7 @@ class TestTornadoInstrumentor(TornadoTest): unpatch_handler_class(AsyncHandler) -class TestTornadoInstrumentation(TornadoTest): +class TestTornadoInstrumentation(TornadoTest, WsgiTestBase): def test_http_calls(self): methods = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"] for method in methods: @@ -446,20 +447,10 @@ class TestTornadoInstrumentation(TornadoTest): set_global_response_propagator(TraceResponsePropagator()) response = self.fetch("/") - headers = response.headers spans = self.sorted_spans(self.memory_exporter.get_finished_spans()) self.assertEqual(len(spans), 3) - server_span = spans[1] - - self.assertIn("traceresponse", headers) - self.assertEqual( - headers["access-control-expose-headers"], "traceresponse", - ) - self.assertEqual( - headers["traceresponse"], - f"00-{trace.format_trace_id(server_span.get_span_context().trace_id)}-{trace.format_span_id(server_span.get_span_context().span_id)}-01", - ) + self.assertTraceResponseHeaderMatchesSpan(response.headers, spans[1]) self.memory_exporter.clear() set_global_response_propagator(orig)