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