Skip to content

Commit

Permalink
fix: provide ASGI support with Django
Browse files Browse the repository at this point in the history
Signed-off-by: Varsha GS <varsha.gs@ibm.com>
  • Loading branch information
GSVarsha committed Oct 24, 2024
1 parent aaa03d4 commit 1364f81
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/instana/instrumentation/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# (c) Copyright Instana Inc. 2018


import os
import sys

import opentracing as ot
import opentracing.ext.tags as ext
import wrapt
from django.core.handlers.asgi import ASGIRequest

from ...log import logger
from ...singletons import agent, tracer
Expand Down Expand Up @@ -45,7 +45,10 @@ def _extract_custom_headers(self, span, headers, format):

def process_request(self, request):
try:
env = request.environ
if isinstance(request, ASGIRequest):
env = request.headers
else:
env = request.environ

ctx = tracer.extract(ot.Format.HTTP_HEADERS, env)
request.iscope = tracer.start_active_span('django', child_of=ctx)
Expand Down Expand Up @@ -92,7 +95,7 @@ def process_response(self, request, response):
except Exception:
logger.debug("Instana middleware @ process_response", exc_info=True)
finally:
if request.iscope is not None:
if hasattr(request, "iscope") and request.iscope:
request.iscope.close()
request.iscope = None
return response
Expand Down

0 comments on commit 1364f81

Please sign in to comment.