12
12
from instana .util .secrets import strip_secrets_from_query
13
13
from instana .util .traceutils import extract_custom_headers
14
14
15
+ def set_span_attributes (span , headers ):
16
+ if "REQUEST_METHOD" in headers :
17
+ span .set_attribute (SpanAttributes .HTTP_METHOD , headers ["REQUEST_METHOD" ])
18
+ if "PATH_INFO" in headers :
19
+ span .set_attribute (SpanAttributes .HTTP_URL , headers ["PATH_INFO" ])
20
+ if "QUERY_STRING" in headers and len (headers ["QUERY_STRING" ]):
21
+ scrubbed_params = strip_secrets_from_query (
22
+ headers ["QUERY_STRING" ],
23
+ agent .options .secrets_matcher ,
24
+ agent .options .secrets_list ,
25
+ )
26
+ span .set_attribute ("http.params" , scrubbed_params )
27
+ if "HTTP_HOST" in headers :
28
+ span .set_attribute ("http.host" , headers ["HTTP_HOST" ])
29
+
30
+ def set_response_status_code (span , response_string ):
31
+ resp_code = int (response_string .split ()[0 ])
32
+
33
+ if 500 <= resp_code :
34
+ span .mark_as_errored ()
35
+
36
+ span .set_attribute (
37
+ SpanAttributes .HTTP_STATUS_CODE , int (resp_code )
38
+ )
15
39
16
40
@wrapt .patch_function_wrapper ("spyne.server.wsgi" , "WsgiApplication.handle_error" )
17
41
def handle_error_with_instana (wrapped , instance , args , kwargs ):
18
42
ctx = args [0 ]
19
43
span = ctx .udc
44
+
45
+ # span created inside process_request() will be handled by finalize() method
20
46
if span :
21
47
return wrapped (* args , ** kwargs )
22
48
@@ -28,19 +54,7 @@ def handle_error_with_instana(wrapped, instance, args, kwargs):
28
54
) as span :
29
55
extract_custom_headers (span , headers , format = True )
30
56
31
- if "REQUEST_METHOD" in headers :
32
- span .set_attribute (SpanAttributes .HTTP_METHOD , headers ["REQUEST_METHOD" ])
33
- if "PATH_INFO" in headers :
34
- span .set_attribute (SpanAttributes .HTTP_URL , headers ["PATH_INFO" ])
35
- if "QUERY_STRING" in headers and len (headers ["QUERY_STRING" ]):
36
- scrubbed_params = strip_secrets_from_query (
37
- headers ["QUERY_STRING" ],
38
- agent .options .secrets_matcher ,
39
- agent .options .secrets_list ,
40
- )
41
- span .set_attribute ("http.params" , scrubbed_params )
42
- if "HTTP_HOST" in headers :
43
- span .set_attribute ("http.host" , headers ["HTTP_HOST" ])
57
+ set_span_attributes (span , headers )
44
58
45
59
response_headers = ctx .transport .resp_headers
46
60
@@ -49,31 +63,18 @@ def handle_error_with_instana(wrapped, instance, args, kwargs):
49
63
50
64
response = wrapped (* args , ** kwargs )
51
65
52
- resp_code = int (ctx .transport .resp_code .split ()[0 ])
53
-
54
- if 500 <= resp_code :
55
- span .mark_as_errored ()
56
-
57
- span .set_attribute (
58
- SpanAttributes .HTTP_STATUS_CODE , int (resp_code )
59
- )
66
+ set_response_status_code (span , ctx .transport .resp_code )
60
67
return response
61
68
62
69
63
70
@wrapt .patch_function_wrapper ("spyne.server.wsgi" , "WsgiApplication._WsgiApplication__finalize" )
64
71
def finalize_with_instana (wrapped , instance , args , kwargs ):
65
72
ctx = args [0 ]
66
73
span = ctx .udc
74
+ response_string = ctx .transport .resp_code
67
75
68
- if span and ctx .transport .resp_code :
69
- resp_code = int (ctx .transport .resp_code .split ()[0 ])
70
-
71
- if 500 <= resp_code :
72
- span .mark_as_errored ()
73
-
74
- span .set_attribute (
75
- SpanAttributes .HTTP_STATUS_CODE , int (resp_code )
76
- )
76
+ if span and response_string :
77
+ set_response_status_code (span , response_string )
77
78
if span .is_recording ():
78
79
span .end ()
79
80
@@ -92,19 +93,7 @@ def process_request_with_instana(wrapped, instance, args, kwargs):
92
93
) as span :
93
94
extract_custom_headers (span , headers , format = True )
94
95
95
- if "REQUEST_METHOD" in headers :
96
- span .set_attribute (SpanAttributes .HTTP_METHOD , headers ["REQUEST_METHOD" ])
97
- if "PATH_INFO" in headers :
98
- span .set_attribute (SpanAttributes .HTTP_URL , headers ["PATH_INFO" ])
99
- if "QUERY_STRING" in headers and len (headers ["QUERY_STRING" ]):
100
- scrubbed_params = strip_secrets_from_query (
101
- headers ["QUERY_STRING" ],
102
- agent .options .secrets_matcher ,
103
- agent .options .secrets_list ,
104
- )
105
- span .set_attribute ("http.params" , scrubbed_params )
106
- if "HTTP_HOST" in headers :
107
- span .set_attribute ("http.host" , headers ["HTTP_HOST" ])
96
+ set_span_attributes (span , headers )
108
97
109
98
response = wrapped (* args , ** kwargs )
110
99
response_headers = ctx .transport .resp_headers
0 commit comments