12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import re
15
16
from sys import modules
16
17
from unittest .mock import Mock , patch
17
18
18
19
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
19
22
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
22
28
23
29
from opentelemetry .instrumentation .django import (
24
30
DjangoInstrumentor ,
28
34
TraceResponsePropagator ,
29
35
set_global_response_propagator ,
30
36
)
37
+ from opentelemetry .instrumentation .wsgi import OpenTelemetryMiddleware
31
38
from opentelemetry .sdk import resources
32
39
from opentelemetry .sdk .trace import Span
33
40
from opentelemetry .sdk .trace .id_generator import RandomIdGenerator
41
48
format_trace_id ,
42
49
)
43
50
from opentelemetry .util .http import get_excluded_urls , get_traced_request_attrs
51
+ from packaging .markers import Op
44
52
45
53
# pylint: disable=import-error
46
54
from .views import (
@@ -401,10 +409,9 @@ def setUpClass(cls):
401
409
def setUp (self ):
402
410
super ().setUp ()
403
411
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 ()
408
415
tracer_provider , exporter = result
409
416
self .exporter = exporter
410
417
_django_instrumentor .instrument (tracer_provider = tracer_provider )
@@ -430,3 +437,67 @@ def test_tracer_provider_traced(self):
430
437
self .assertEqual (
431
438
span .resource .attributes ["resource-key" ], "resource-value"
432
439
)
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