From 22c8b1fefe5ca95db8875817072aab23789bb0c3 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Fri, 22 Oct 2021 21:57:50 -0300 Subject: [PATCH] Add unit tests --- .../tests/test_middleware.py | 31 ++++++++++++++++++- .../tests/test_middleware_asgi.py | 24 ++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 99ee57da84..0b90a9bb7c 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -31,10 +31,16 @@ ) from opentelemetry.sdk import resources from opentelemetry.sdk.trace import Span +from opentelemetry.sdk.trace.id_generator import RandomIdGenerator 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 +from opentelemetry.trace import ( + SpanKind, + StatusCode, + format_span_id, + format_trace_id, +) from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs # pylint: disable=import-error @@ -331,6 +337,29 @@ def response_hook(span, request, response): self.assertIsInstance(response_hook_args[2], HttpResponse) self.assertEqual(response_hook_args[2], response) + async def test_trace_parent(self): + id_generator = RandomIdGenerator() + trace_id = format_trace_id(id_generator.generate_trace_id()) + span_id = format_span_id(id_generator.generate_span_id()) + traceparent_value = f"00-{trace_id}-{span_id}-01" + + Client().get( + "/span_name/1234/", traceparent=traceparent_value, + ) + span = self.memory_exporter.get_finished_spans()[0] + + self.assertEqual( + trace_id, format_trace_id(span.get_span_context().trace_id), + ) + self.assertIsNotNone(span.parent) + self.assertEqual( + trace_id, format_trace_id(span.parent.trace_id), + ) + self.assertEqual( + span_id, format_span_id(span.parent.span_id), + ) + self.memory_exporter.clear() + def test_trace_response_headers(self): response = Client().get("/span_name/1234/") diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py index d1f578ccc8..0bac1c7243 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py @@ -32,6 +32,7 @@ ) from opentelemetry.sdk import resources from opentelemetry.sdk.trace import Span +from opentelemetry.sdk.trace.id_generator import RandomIdGenerator from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase from opentelemetry.trace import ( @@ -317,6 +318,29 @@ def response_hook(span, request, response): self.assertIsInstance(response_hook_args[2], HttpResponse) self.assertEqual(response_hook_args[2], response) + async def test_trace_parent(self): + id_generator = RandomIdGenerator() + trace_id = format_trace_id(id_generator.generate_trace_id()) + span_id = format_span_id(id_generator.generate_span_id()) + traceparent_value = f"00-{trace_id}-{span_id}-01" + + await self.async_client.get( + "/span_name/1234/", traceparent=traceparent_value, + ) + span = self.memory_exporter.get_finished_spans()[0] + + self.assertEqual( + trace_id, format_trace_id(span.get_span_context().trace_id), + ) + self.assertIsNotNone(span.parent) + self.assertEqual( + trace_id, format_trace_id(span.parent.trace_id), + ) + self.assertEqual( + span_id, format_span_id(span.parent.span_id), + ) + self.memory_exporter.clear() + async def test_trace_response_headers(self): response = await self.async_client.get("/span_name/1234/")