@@ -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 :
0 commit comments