Skip to content

Commit d1c2327

Browse files
committed
Merge branch 'master' into fastapi-header-capture
Signed-off-by: Varsha GS <[email protected]>
2 parents 82d1861 + 884edc3 commit d1c2327

File tree

3 files changed

+160
-91
lines changed

3 files changed

+160
-91
lines changed

instana/instrumentation/django/middleware.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,29 @@ 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+
if agent.options.extra_http_headers is None:
34+
return
35+
36+
try:
37+
for custom_header in agent.options.extra_http_headers:
38+
# Headers are available in this format: HTTP_X_CAPTURE_THIS
39+
django_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header
40+
41+
if django_header in headers:
42+
span.set_tag("http.header.%s" % custom_header, headers[django_header])
43+
44+
except Exception:
45+
logger.debug("extract_custom_headers: ", exc_info=True)
46+
3247
def process_request(self, request):
3348
try:
3449
env = request.environ
3550

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

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])
54+
self._extract_custom_headers(request.iscope.span, env, format=True)
4555

4656
request.iscope.span.set_tag(ext.HTTP_METHOD, request.method)
4757
if 'PATH_INFO' in env:
@@ -75,7 +85,9 @@ def process_response(self, request, response):
7585
path_tpl = None
7686
if path_tpl:
7787
request.iscope.span.set_tag("http.path_tpl", path_tpl)
88+
7889
request.iscope.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
90+
self._extract_custom_headers(request.iscope.span, response.headers, format=False)
7991
tracer.inject(request.iscope.span.context, ot.Format.HTTP_HEADERS, response)
8092
response['Server-Timing'] = "intid;desc=%s" % request.iscope.span.context.trace_id
8193
except Exception:

tests/apps/app_django.py

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

127127

128+
def response_with_headers(request):
129+
headers = {
130+
'X-Capture-This-Too': 'this too',
131+
'X-Capture-That-Too': 'that too'
132+
}
133+
return HttpResponse('Stan wuz here with headers!', headers=headers)
134+
135+
128136
urlpatterns = [
129137
re_path(r'^$', index, name='index'),
130138
re_path(r'^cause_error$', cause_error, name='cause_error'),
131139
re_path(r'^another$', another),
132140
re_path(r'^not_found$', not_found, name='not_found'),
133-
re_path(r'^complex$', complex, name='complex')
141+
re_path(r'^complex$', complex, name='complex'),
142+
re_path(r'^response_with_headers$', response_with_headers, name='response_with_headers')
134143
]

0 commit comments

Comments
 (0)