Skip to content

Commit aa1f2ba

Browse files
GSVarshapvital
authored andcommitted
sanic: refactor tests
Signed-off-by: Varsha GS <[email protected]> (cherry picked from commit b4968d031793fbf76b15b6d1456f45c54f0c744c)
1 parent 49ea512 commit aa1f2ba

File tree

12 files changed

+429
-409
lines changed

12 files changed

+429
-409
lines changed

src/instana/instrumentation/sanic_inst.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Instrumentation for Sanic
55
https://sanicframework.org/en/
66
"""
7+
78
try:
89
import sanic
910
import wrapt
@@ -27,7 +28,6 @@
2728
from sanic.request import Request
2829
from sanic.response import HTTPResponse
2930

30-
3131
@wrapt.patch_function_wrapper("sanic.app", "Sanic.__init__")
3232
def init_with_instana(
3333
wrapped: Callable[..., sanic.app.Sanic.__init__],
@@ -43,7 +43,7 @@ def request_with_instana(request: Request) -> None:
4343
try:
4444
if "http" not in request.scheme:
4545
return
46-
46+
4747
headers = request.headers.copy()
4848
parent_context = tracer.extract(Format.HTTP_HEADERS, headers)
4949

@@ -54,8 +54,8 @@ def request_with_instana(request: Request) -> None:
5454
token = context.attach(ctx)
5555
request.ctx.token = token
5656

57-
span.set_attribute('span.kind', SpanKind.CLIENT)
58-
span.set_attribute('http.path', request.path)
57+
span.set_attribute("span.kind", SpanKind.CLIENT)
58+
span.set_attribute("http.path", request.path)
5959
span.set_attribute(SpanAttributes.HTTP_METHOD, request.method)
6060
span.set_attribute(SpanAttributes.HTTP_HOST, request.host)
6161
if hasattr(request, "url"):
@@ -65,9 +65,10 @@ def request_with_instana(request: Request) -> None:
6565

6666
if isinstance(query, (str, bytes)) and len(query):
6767
if isinstance(query, bytes):
68-
query = query.decode('utf-8')
69-
scrubbed_params = strip_secrets_from_query(query, agent.options.secrets_matcher,
70-
agent.options.secrets_list)
68+
query = query.decode("utf-8")
69+
scrubbed_params = strip_secrets_from_query(
70+
query, agent.options.secrets_matcher, agent.options.secrets_list
71+
)
7172
span.set_attribute("http.params", scrubbed_params)
7273

7374
if agent.options.extra_http_headers:
@@ -77,7 +78,6 @@ def request_with_instana(request: Request) -> None:
7778
except Exception:
7879
logger.debug("request_with_instana: ", exc_info=True)
7980

80-
8181
@app.exception(Exception)
8282
def exception_with_instana(request: Request, exception: Exception) -> None:
8383
try:
@@ -95,7 +95,6 @@ def exception_with_instana(request: Request, exception: Exception) -> None:
9595
except Exception:
9696
logger.debug("exception_with_instana: ", exc_info=True)
9797

98-
9998
@app.middleware("response")
10099
def response_with_instana(request: Request, response: HTTPResponse) -> None:
101100
try:
@@ -107,12 +106,14 @@ def response_with_instana(request: Request, response: HTTPResponse) -> None:
107106
if status_code:
108107
if int(status_code) >= 500:
109108
span.mark_as_errored()
110-
span.set_attribute('http.status_code', status_code)
109+
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
111110

112111
if hasattr(response, "headers"):
113112
extract_custom_headers(span, response.headers)
114113
tracer.inject(span.context, Format.HTTP_HEADERS, response.headers)
115-
response.headers['Server-Timing'] = "intid;desc=%s" % span.context.trace_id
114+
response.headers["Server-Timing"] = (
115+
"intid;desc=%s" % span.context.trace_id
116+
)
116117

117118
if span.is_recording():
118119
span.end()
@@ -125,4 +126,4 @@ def response_with_instana(request: Request, response: HTTPResponse) -> None:
125126
logger.debug("response_with_instana: ", exc_info=True)
126127

127128
except ImportError:
128-
pass
129+
pass

tests/apps/sanic_app/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
import uvicorn
66

7-
from ...helpers import testenv
8-
from instana.log import logger
7+
from tests.helpers import testenv
98

109
testenv["sanic_port"] = 1337
11-
testenv["sanic_server"] = ("http://127.0.0.1:" + str(testenv["sanic_port"]))
10+
testenv["sanic_server"] = f"http://127.0.0.1:{testenv['sanic_port']}"
1211

1312

1413
def launch_sanic():

tests/apps/sanic_app/name.py

-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@
77

88

99
class NameView(HTTPMethodView):
10-
1110
def get(self, request, name):
1211
return text("Hello {}".format(name))
13-
14-

tests/apps/sanic_app/server.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,35 @@
1010
from tests.apps.sanic_app.simpleview import SimpleView
1111
from tests.apps.sanic_app.name import NameView
1212

13-
app = Sanic('test')
13+
app = Sanic("test")
14+
1415

1516
@app.get("/foo/<foo_id:int>")
1617
async def uuid_handler(request, foo_id: int):
1718
return text("INT - {}".format(foo_id))
1819

20+
1921
@app.route("/response_headers")
2022
async def response_headers(request):
21-
headers = {
22-
'X-Capture-This-Too': 'this too',
23-
'X-Capture-That-Too': 'that too'
24-
}
23+
headers = {"X-Capture-This-Too": "this too", "X-Capture-That-Too": "that too"}
2524
return text("Stan wuz here with headers!", headers=headers)
2625

26+
2727
@app.route("/test_request_args")
28-
async def test_request_args(request):
28+
async def test_request_args_500(request):
2929
raise SanicException("Something went wrong.", status_code=500)
3030

31+
3132
@app.route("/instana_exception")
32-
async def test_request_args(request):
33+
async def test_instana_exception(request):
3334
raise SanicException(description="Something went wrong.", status_code=500)
3435

36+
3537
@app.route("/wrong")
36-
async def test_request_args(request):
38+
async def test_request_args_400(request):
3739
raise SanicException(message="Something went wrong.", status_code=400)
3840

41+
3942
@app.get("/tag/<tag>")
4043
async def tag_handler(request, tag):
4144
return text("Tag - {}".format(tag))
@@ -45,8 +48,5 @@ async def tag_handler(request, tag):
4548
app.add_route(NameView.as_view(), "/<name>")
4649

4750

48-
if __name__ == '__main__':
51+
if __name__ == "__main__":
4952
app.run(host="0.0.0.0", port=8000, debug=True, access_log=True)
50-
51-
52-

tests/apps/sanic_app/simpleview.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
from sanic.views import HTTPMethodView
66
from sanic.response import text
77

8-
class SimpleView(HTTPMethodView):
98

10-
def get(self, request):
11-
return text("I am get method")
9+
class SimpleView(HTTPMethodView):
10+
def get(self, request):
11+
return text("I am get method")
1212

13-
# You can also use async syntax
14-
async def post(self, request):
15-
return text("I am post method")
13+
# You can also use async syntax
14+
async def post(self, request):
15+
return text("I am post method")
1616

17-
def put(self, request):
18-
return text("I am put method")
17+
def put(self, request):
18+
return text("I am put method")
1919

20-
def patch(self, request):
21-
return text("I am patch method")
20+
def patch(self, request):
21+
return text("I am patch method")
2222

23-
def delete(self, request):
24-
return text("I am delete method")
23+
def delete(self, request):
24+
return text("I am delete method")

tests/conftest.py

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
collect_ignore_glob.append("*frameworks/test_gevent*")
4949
collect_ignore_glob.append("*frameworks/test_grpcio*")
5050
collect_ignore_glob.append("*frameworks/test_pyramid*")
51-
collect_ignore_glob.append("*frameworks/test_sanic*")
5251
collect_ignore_glob.append("*frameworks/test_tornado*")
5352

5453
# # Cassandra and gevent tests are run in dedicated jobs on CircleCI and will

0 commit comments

Comments
 (0)