Skip to content

Commit 32e55a1

Browse files
committed
chore(spyne): Handle repetitive code
Signed-off-by: Varsha GS <[email protected]>
1 parent 2a3a0d2 commit 32e55a1

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed

src/instana/instrumentation/spyne.py

+32-43
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,37 @@
1212
from instana.util.secrets import strip_secrets_from_query
1313
from instana.util.traceutils import extract_custom_headers
1414

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+
)
1539

1640
@wrapt.patch_function_wrapper("spyne.server.wsgi", "WsgiApplication.handle_error")
1741
def handle_error_with_instana(wrapped, instance, args, kwargs):
1842
ctx = args[0]
1943
span = ctx.udc
44+
45+
# span created inside process_request() will be handled by finalize() method
2046
if span:
2147
return wrapped(*args, **kwargs)
2248

@@ -28,19 +54,7 @@ def handle_error_with_instana(wrapped, instance, args, kwargs):
2854
) as span:
2955
extract_custom_headers(span, headers, format=True)
3056

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)
4458

4559
response_headers = ctx.transport.resp_headers
4660

@@ -49,31 +63,18 @@ def handle_error_with_instana(wrapped, instance, args, kwargs):
4963

5064
response = wrapped(*args, **kwargs)
5165

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)
6067
return response
6168

6269

6370
@wrapt.patch_function_wrapper("spyne.server.wsgi", "WsgiApplication._WsgiApplication__finalize")
6471
def finalize_with_instana(wrapped, instance, args, kwargs):
6572
ctx = args[0]
6673
span = ctx.udc
74+
response_string = ctx.transport.resp_code
6775

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)
7778
if span.is_recording():
7879
span.end()
7980

@@ -92,19 +93,7 @@ def process_request_with_instana(wrapped, instance, args, kwargs):
9293
) as span:
9394
extract_custom_headers(span, headers, format=True)
9495

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)
10897

10998
response = wrapped(*args, **kwargs)
11099
response_headers = ctx.transport.resp_headers

0 commit comments

Comments
 (0)