Skip to content

Commit

Permalink
Merge pull request #489 from instana/django-header-capture
Browse files Browse the repository at this point in the history
Django: capture responseHeadersOnEntrySpans
  • Loading branch information
GSVarsha authored Jan 2, 2024
2 parents 6293143 + 8363101 commit 884edc3
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 91 deletions.
24 changes: 18 additions & 6 deletions instana/instrumentation/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,29 @@ def __init__(self, get_response=None):
super(InstanaMiddleware, self).__init__(get_response)
self.get_response = get_response

def _extract_custom_headers(self, span, headers, format):
if agent.options.extra_http_headers is None:
return

try:
for custom_header in agent.options.extra_http_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
django_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header

if django_header in headers:
span.set_tag("http.header.%s" % custom_header, headers[django_header])

except Exception:
logger.debug("extract_custom_headers: ", exc_info=True)

def process_request(self, request):
try:
env = request.environ

ctx = tracer.extract(ot.Format.HTTP_HEADERS, env)
request.iscope = tracer.start_active_span('django', child_of=ctx)

if agent.options.extra_http_headers is not None:
for custom_header in agent.options.extra_http_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
django_header = ('HTTP_' + custom_header.upper()).replace('-', '_')
if django_header in env:
request.iscope.span.set_tag("http.header.%s" % custom_header, env[django_header])
self._extract_custom_headers(request.iscope.span, env, format=True)

request.iscope.span.set_tag(ext.HTTP_METHOD, request.method)
if 'PATH_INFO' in env:
Expand Down Expand Up @@ -75,7 +85,9 @@ def process_response(self, request, response):
path_tpl = None
if path_tpl:
request.iscope.span.set_tag("http.path_tpl", path_tpl)

request.iscope.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
self._extract_custom_headers(request.iscope.span, response.headers, format=False)
tracer.inject(request.iscope.span.context, ot.Format.HTTP_HEADERS, response)
response['Server-Timing'] = "intid;desc=%s" % request.iscope.span.context.trace_id
except Exception:
Expand Down
11 changes: 10 additions & 1 deletion tests/apps/app_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,19 @@ def complex(request):
return HttpResponse('Stan wuz here!')


def response_with_headers(request):
headers = {
'X-Capture-This-Too': 'this too',
'X-Capture-That-Too': 'that too'
}
return HttpResponse('Stan wuz here with headers!', headers=headers)


urlpatterns = [
re_path(r'^$', index, name='index'),
re_path(r'^cause_error$', cause_error, name='cause_error'),
re_path(r'^another$', another),
re_path(r'^not_found$', not_found, name='not_found'),
re_path(r'^complex$', complex, name='complex')
re_path(r'^complex$', complex, name='complex'),
re_path(r'^response_with_headers$', response_with_headers, name='response_with_headers')
]
Loading

0 comments on commit 884edc3

Please sign in to comment.