Skip to content

Commit 6347275

Browse files
committed
style(spyne): Add typehints
Signed-off-by: Varsha GS <[email protected]>
1 parent 32e55a1 commit 6347275

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

src/instana/instrumentation/spyne.py

+38-19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
try:
44
import spyne
55
import wrapt
6+
from typing import TYPE_CHECKING, Dict, Any, Callable, Tuple, Iterable
67

78
from opentelemetry.semconv.trace import SpanAttributes
89

@@ -12,7 +13,12 @@
1213
from instana.util.secrets import strip_secrets_from_query
1314
from instana.util.traceutils import extract_custom_headers
1415

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:
1622
if "REQUEST_METHOD" in headers:
1723
span.set_attribute(SpanAttributes.HTTP_METHOD, headers["REQUEST_METHOD"])
1824
if "PATH_INFO" in headers:
@@ -27,48 +33,55 @@ def set_span_attributes(span, headers):
2733
if "HTTP_HOST" in headers:
2834
span.set_attribute("http.host", headers["HTTP_HOST"])
2935

30-
def set_response_status_code(span, response_string):
36+
def set_response_status_code(span: "InstanaSpan", response_string: str) -> None:
3137
resp_code = int(response_string.split()[0])
3238

3339
if 500 <= resp_code:
3440
span.mark_as_errored()
3541

36-
span.set_attribute(
37-
SpanAttributes.HTTP_STATUS_CODE, int(resp_code)
38-
)
42+
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, int(resp_code))
3943

4044
@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]:
4251
ctx = args[0]
4352
span = ctx.udc
4453

4554
# span created inside process_request() will be handled by finalize() method
4655
if span:
4756
return wrapped(*args, **kwargs)
48-
57+
4958
headers = ctx.in_document
5059
span_context = tracer.extract(Format.HTTP_HEADERS, headers)
5160

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:
5562
extract_custom_headers(span, headers, format=True)
5663

5764
set_span_attributes(span, headers)
5865

5966
response_headers = ctx.transport.resp_headers
60-
67+
6168
extract_custom_headers(span, response_headers, format=False)
6269
tracer.inject(span.context, Format.HTTP_HEADERS, response_headers)
6370

6471
response = wrapped(*args, **kwargs)
6572

6673
set_response_status_code(span, ctx.transport.resp_code)
67-
return response
68-
74+
return response
6975

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[()]:
7285
ctx = args[0]
7386
span = ctx.udc
7487
response_string = ctx.transport.resp_code
@@ -81,23 +94,29 @@ def finalize_with_instana(wrapped, instance, args, kwargs):
8194
ctx.udc = None
8295
return wrapped(*args, **kwargs)
8396

84-
8597
@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:
87104
ctx = args[0]
88105
headers = ctx.in_document
89106
span_context = tracer.extract(Format.HTTP_HEADERS, headers)
90107

91108
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,
93112
) as span:
94113
extract_custom_headers(span, headers, format=True)
95114

96115
set_span_attributes(span, headers)
97116

98117
response = wrapped(*args, **kwargs)
99118
response_headers = ctx.transport.resp_headers
100-
119+
101120
extract_custom_headers(span, response_headers, format=False)
102121
tracer.inject(span.context, Format.HTTP_HEADERS, response_headers)
103122

0 commit comments

Comments
 (0)