Skip to content

Commit f82f1d1

Browse files
committed
Code changes and pytests for #448
1 parent a9507b3 commit f82f1d1

File tree

2 files changed

+94
-11
lines changed

2 files changed

+94
-11
lines changed

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@
3232
from opentelemetry.instrumentation.wsgi import wsgi_getter
3333
from opentelemetry.propagate import extract
3434
from opentelemetry.semconv.trace import SpanAttributes
35-
from opentelemetry.trace import Span, SpanKind, use_span
35+
from opentelemetry.trace import (
36+
INVALID_SPAN,
37+
Span,
38+
SpanKind,
39+
get_current_span,
40+
use_span,
41+
)
3642
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
3743

3844
try:
@@ -184,11 +190,16 @@ def process_request(self, request):
184190
carrier_getter = wsgi_getter
185191
collect_request_attributes = wsgi_collect_request_attributes
186192

187-
token = attach(extract(carrier, getter=carrier_getter))
188-
193+
token = context = None
194+
span_kind = SpanKind.INTERNAL
195+
if get_current_span() is INVALID_SPAN:
196+
context = extract(request_meta, getter=wsgi_getter)
197+
token = attach(context)
198+
span_kind = SpanKind.SERVER
189199
span = self._tracer.start_span(
190200
self._get_span_name(request),
191-
kind=SpanKind.SERVER,
201+
context,
202+
kind=span_kind,
192203
start_time=request_meta.get(
193204
"opentelemetry-instrumentor-django.starttime_key"
194205
),
@@ -221,7 +232,8 @@ def process_request(self, request):
221232

222233
request.META[self._environ_activation_key] = activation
223234
request.META[self._environ_span_key] = span
224-
request.META[self._environ_token] = token
235+
if token:
236+
request.META[self._environ_token] = token
225237

226238
if _DjangoMiddleware._otel_request_hook:
227239
_DjangoMiddleware._otel_request_hook( # pylint: disable=not-callable

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

+77-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import re
1516
from sys import modules
1617
from unittest.mock import Mock, patch
1718

1819
from django import VERSION, conf
20+
from django.core.wsgi import get_wsgi_application
21+
from django.core.servers.basehttp import get_internal_wsgi_application
1922
from django.http import HttpRequest, HttpResponse
20-
from django.test.client import Client
21-
from django.test.utils import setup_test_environment, teardown_test_environment
23+
from django.test.client import Client, ClientHandler, RequestFactory
24+
from django.test.testcases import SimpleTestCase
25+
from django.test.utils import override_settings, setup_test_environment, teardown_test_environment
26+
from fastapi import applications
27+
from opentelemetry import trace
2228

2329
from opentelemetry.instrumentation.django import (
2430
DjangoInstrumentor,
@@ -28,6 +34,7 @@
2834
TraceResponsePropagator,
2935
set_global_response_propagator,
3036
)
37+
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
3138
from opentelemetry.sdk import resources
3239
from opentelemetry.sdk.trace import Span
3340
from opentelemetry.sdk.trace.id_generator import RandomIdGenerator
@@ -41,6 +48,7 @@
4148
format_trace_id,
4249
)
4350
from opentelemetry.util.http import get_excluded_urls, get_traced_request_attrs
51+
from packaging.markers import Op
4452

4553
# pylint: disable=import-error
4654
from .views import (
@@ -401,10 +409,9 @@ def setUpClass(cls):
401409
def setUp(self):
402410
super().setUp()
403411
setup_test_environment()
404-
resource = resources.Resource.create(
405-
{"resource-key": "resource-value"}
406-
)
407-
result = self.create_tracer_provider(resource=resource)
412+
_django_instrumentor.instrument()
413+
414+
result = self.create_tracer_provider()
408415
tracer_provider, exporter = result
409416
self.exporter = exporter
410417
_django_instrumentor.instrument(tracer_provider=tracer_provider)
@@ -430,3 +437,67 @@ def test_tracer_provider_traced(self):
430437
self.assertEqual(
431438
span.resource.attributes["resource-key"], "resource-value"
432439
)
440+
441+
class TestDjangoWithOtherFramework(SimpleTestCase, TestBase, WsgiTestBase):
442+
@classmethod
443+
def setUpClass(cls):
444+
conf.settings.configure(ROOT_URLCONF=modules[__name__], WSGI_APPLICATION="tests.wsgi.application")
445+
super().setUpClass()
446+
447+
def setUp(self):
448+
super().setUp()
449+
setup_test_environment()
450+
451+
# conf.settings.WSGI_APPLICATION = "wsgi.application"
452+
# application = get_internal_wsgi_application()
453+
# application = get_wsgi_application()
454+
# _DjangoMiddleware
455+
# self.application = OpenTelemetryMiddleware(application)
456+
# _django_instrumentor.instrument()
457+
# self.application = OpenTelemetryMiddleware(application, tracer_provider=self.tracer_provider)
458+
# conf.settings.configure(ROOT_URLCONF=modules[__name__], WSGI_APPLICATION="application")
459+
# conf.settings.WSGI_APPLICATION="application"
460+
461+
462+
def tearDown(self) -> None:
463+
super().tearDown()
464+
teardown_test_environment()
465+
_django_instrumentor.uninstrument()
466+
467+
@classmethod
468+
def tearDownClass(cls):
469+
super().tearDownClass()
470+
conf.settings = conf.LazySettings()
471+
472+
473+
def test_with_another_framework(self):
474+
environ = RequestFactory()._base_environ(
475+
PATH_INFO="/span_name/1234/",
476+
CONTENT_TYPE="text/html; charset=utf-8",
477+
REQUEST_METHOD="GET"
478+
)
479+
response_data = {}
480+
def start_response(status, headers):
481+
response_data["status"] = status
482+
response_data["headers"] = headers
483+
484+
result = self.create_tracer_provider()
485+
tracer_provider, exporter = result
486+
self.exporter = exporter
487+
488+
489+
_django_instrumentor.instrument(tracer_provider=tracer_provider)
490+
application = get_internal_wsgi_application()
491+
application = OpenTelemetryMiddleware(application, tracer_provider=tracer_provider)
492+
resp = application(environ, start_response)
493+
494+
# resp = Client().get("/span_name/1234/")
495+
# self.assertEqual(200, resp.status_code)
496+
497+
# span_list = self.memory_exporter.get_finished_spans()
498+
span_list = self.exporter.get_finished_spans()
499+
# print(span_list)
500+
self.assertEqual(trace.SpanKind.INTERNAL, span_list[0].kind)
501+
502+
#Below line give me error "index out of the range" as there is only one span created where it should be 2.
503+
self.assertEqual(trace.SpanKind.SERVER, span_list[1].kind)

0 commit comments

Comments
 (0)