Skip to content

Commit 12a5027

Browse files
committed
Django: capture responseHeadersOnEntrySpans
Signed-off-by: Varsha GS <[email protected]>
1 parent 6293143 commit 12a5027

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

instana/instrumentation/django/middleware.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,27 @@ def __init__(self, get_response=None):
2929
super(InstanaMiddleware, self).__init__(get_response)
3030
self.get_response = get_response
3131

32+
def _extract_custom_headers(self, span, headers, format):
33+
try:
34+
if agent.options.extra_http_headers is not None:
35+
for custom_header in agent.options.extra_http_headers:
36+
# Headers are available in this format: HTTP_X_CAPTURE_THIS
37+
django_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header
38+
39+
if django_header in headers:
40+
span.set_tag("http.header.%s" % custom_header, headers[django_header])
41+
42+
except Exception:
43+
logger.debug("extract_custom_headers: ", exc_info=True)
44+
3245
def process_request(self, request):
3346
try:
3447
env = request.environ
3548

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

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

4654
request.iscope.span.set_tag(ext.HTTP_METHOD, request.method)
4755
if 'PATH_INFO' in env:
@@ -75,7 +83,9 @@ def process_response(self, request, response):
7583
path_tpl = None
7684
if path_tpl:
7785
request.iscope.span.set_tag("http.path_tpl", path_tpl)
86+
7887
request.iscope.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
88+
self._extract_custom_headers(request.iscope.span, response.headers, format=False)
7989
tracer.inject(request.iscope.span.context, ot.Format.HTTP_HEADERS, response)
8090
response['Server-Timing'] = "intid;desc=%s" % request.iscope.span.context.trace_id
8191
except Exception:

tests/apps/app_django.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,17 @@ def complex(request):
125125
return HttpResponse('Stan wuz here!')
126126

127127

128+
def response_with_headers(request):
129+
response = HttpResponse(content_type='')
130+
response['X-Capture-This-Too'] = 'this too'
131+
return response
132+
133+
128134
urlpatterns = [
129135
re_path(r'^$', index, name='index'),
130136
re_path(r'^cause_error$', cause_error, name='cause_error'),
131137
re_path(r'^another$', another),
132138
re_path(r'^not_found$', not_found, name='not_found'),
133-
re_path(r'^complex$', complex, name='complex')
139+
re_path(r'^complex$', complex, name='complex'),
140+
re_path(r'^response_with_headers$', response_with_headers, name='response_with_headers')
134141
]

0 commit comments

Comments
 (0)