3
3
try :
4
4
import spyne
5
5
import wrapt
6
+ from typing import TYPE_CHECKING , Dict , Any , Callable , Tuple , Iterable
6
7
7
8
from opentelemetry .semconv .trace import SpanAttributes
8
9
12
13
from instana .util .secrets import strip_secrets_from_query
13
14
from instana .util .traceutils import extract_custom_headers
14
15
15
- def set_span_attributes (span , headers ):
16
+ if TYPE_CHECKING :
17
+ from instana .span .span import InstanaSpan
18
+ from spyne .application import Application
19
+ from spyne .server .wsgi import WsgiApplication
20
+
21
+ def set_span_attributes (span : "InstanaSpan" , headers : Dict [str , Any ]) -> None :
16
22
if "REQUEST_METHOD" in headers :
17
23
span .set_attribute (SpanAttributes .HTTP_METHOD , headers ["REQUEST_METHOD" ])
18
24
if "PATH_INFO" in headers :
@@ -27,48 +33,55 @@ def set_span_attributes(span, headers):
27
33
if "HTTP_HOST" in headers :
28
34
span .set_attribute ("http.host" , headers ["HTTP_HOST" ])
29
35
30
- def set_response_status_code (span , response_string ) :
36
+ def set_response_status_code (span : "InstanaSpan" , response_string : str ) -> None :
31
37
resp_code = int (response_string .split ()[0 ])
32
38
33
39
if 500 <= resp_code :
34
40
span .mark_as_errored ()
35
41
36
- span .set_attribute (
37
- SpanAttributes .HTTP_STATUS_CODE , int (resp_code )
38
- )
42
+ span .set_attribute (SpanAttributes .HTTP_STATUS_CODE , int (resp_code ))
39
43
40
44
@wrapt .patch_function_wrapper ("spyne.server.wsgi" , "WsgiApplication.handle_error" )
41
- def handle_error_with_instana (wrapped , instance , args , kwargs ):
45
+ def handle_error_with_instana (
46
+ wrapped : Callable [..., Iterable [object ]],
47
+ instance : "WsgiApplication" ,
48
+ args : Tuple [object ],
49
+ kwargs : Dict [str , Any ],
50
+ ) -> Iterable [object ]:
42
51
ctx = args [0 ]
43
52
span = ctx .udc
44
53
45
54
# span created inside process_request() will be handled by finalize() method
46
55
if span :
47
56
return wrapped (* args , ** kwargs )
48
-
57
+
49
58
headers = ctx .in_document
50
59
span_context = tracer .extract (Format .HTTP_HEADERS , headers )
51
60
52
- with tracer .start_as_current_span (
53
- "spyne" , span_context = span_context
54
- ) as span :
61
+ with tracer .start_as_current_span ("spyne" , span_context = span_context ) as span :
55
62
extract_custom_headers (span , headers , format = True )
56
63
57
64
set_span_attributes (span , headers )
58
65
59
66
response_headers = ctx .transport .resp_headers
60
-
67
+
61
68
extract_custom_headers (span , response_headers , format = False )
62
69
tracer .inject (span .context , Format .HTTP_HEADERS , response_headers )
63
70
64
71
response = wrapped (* args , ** kwargs )
65
72
66
73
set_response_status_code (span , ctx .transport .resp_code )
67
- return response
68
-
74
+ return response
69
75
70
- @wrapt .patch_function_wrapper ("spyne.server.wsgi" , "WsgiApplication._WsgiApplication__finalize" )
71
- def finalize_with_instana (wrapped , instance , args , kwargs ):
76
+ @wrapt .patch_function_wrapper (
77
+ "spyne.server.wsgi" , "WsgiApplication._WsgiApplication__finalize"
78
+ )
79
+ def finalize_with_instana (
80
+ wrapped : Callable [..., Tuple [()]],
81
+ instance : "WsgiApplication" ,
82
+ args : Tuple [object ],
83
+ kwargs : Dict [str , Any ],
84
+ ) -> Tuple [()]:
72
85
ctx = args [0 ]
73
86
span = ctx .udc
74
87
response_string = ctx .transport .resp_code
@@ -81,23 +94,29 @@ def finalize_with_instana(wrapped, instance, args, kwargs):
81
94
ctx .udc = None
82
95
return wrapped (* args , ** kwargs )
83
96
84
-
85
97
@wrapt .patch_function_wrapper ("spyne.application" , "Application.process_request" )
86
- def process_request_with_instana (wrapped , instance , args , kwargs ):
98
+ def process_request_with_instana (
99
+ wrapped : Callable [..., None ],
100
+ instance : "Application" ,
101
+ args : Tuple [object ],
102
+ kwargs : Dict [str , Any ],
103
+ ) -> None :
87
104
ctx = args [0 ]
88
105
headers = ctx .in_document
89
106
span_context = tracer .extract (Format .HTTP_HEADERS , headers )
90
107
91
108
with tracer .start_as_current_span (
92
- "spyne" , span_context = span_context , end_on_exit = False ,
109
+ "spyne" ,
110
+ span_context = span_context ,
111
+ end_on_exit = False ,
93
112
) as span :
94
113
extract_custom_headers (span , headers , format = True )
95
114
96
115
set_span_attributes (span , headers )
97
116
98
117
response = wrapped (* args , ** kwargs )
99
118
response_headers = ctx .transport .resp_headers
100
-
119
+
101
120
extract_custom_headers (span , response_headers , format = False )
102
121
tracer .inject (span .context , Format .HTTP_HEADERS , response_headers )
103
122
0 commit comments