Skip to content

Commit c7aa036

Browse files
author
Peter Giacomo Lombardo
authored
Augment aiohttp tests; linter (#257)
1 parent 97965e0 commit c7aa036

File tree

6 files changed

+263
-151
lines changed

6 files changed

+263
-151
lines changed

instana/instrumentation/aiohttp/server.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ async def stan_middleware(request, handler):
2525
url = str(request.url)
2626
parts = url.split('?')
2727
if len(parts) > 1:
28-
cleaned_qp = strip_secrets_from_query(parts[1], agent.options.secrets_matcher, agent.options.secrets_list)
28+
cleaned_qp = strip_secrets_from_query(parts[1],
29+
agent.options.secrets_matcher,
30+
agent.options.secrets_list)
2931
scope.span.set_tag("http.params", cleaned_qp)
3032

3133
scope.span.set_tag("http.url", parts[0])
@@ -40,10 +42,10 @@ async def stan_middleware(request, handler):
4042
response = None
4143
try:
4244
response = await handler(request)
43-
except aiohttp.web.HTTPException as e:
45+
except aiohttp.web.HTTPException as exc:
4446
# AIOHTTP uses exceptions for specific responses
4547
# see https://docs.aiohttp.org/en/latest/web_exceptions.html#web-server-exceptions
46-
response = e
48+
response = exc
4749

4850
if response is not None:
4951
# Mark 500 responses as errored
@@ -55,18 +57,18 @@ async def stan_middleware(request, handler):
5557
response.headers['Server-Timing'] = "intid;desc=%s" % scope.span.context.trace_id
5658

5759
return response
58-
except Exception as e:
60+
except Exception as exc:
5961
logger.debug("aiohttp stan_middleware", exc_info=True)
6062
if scope is not None:
6163
scope.span.set_tag("http.status_code", 500)
62-
scope.span.log_exception(e)
64+
scope.span.log_exception(exc)
6365
raise
6466
finally:
6567
if scope is not None:
6668
scope.close()
6769

6870

69-
@wrapt.patch_function_wrapper('aiohttp.web','Application.__init__')
71+
@wrapt.patch_function_wrapper('aiohttp.web', 'Application.__init__')
7072
def init_with_instana(wrapped, instance, argv, kwargs):
7173
if "middlewares" in kwargs:
7274
kwargs["middlewares"].insert(0, stan_middleware)
@@ -78,4 +80,3 @@ def init_with_instana(wrapped, instance, argv, kwargs):
7880
logger.debug("Instrumenting aiohttp server")
7981
except ImportError:
8082
pass
81-

tests/apps/__init__.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,4 @@
1818
print("Starting background RPC app...")
1919
rpc_server_thread.start()
2020

21-
if sys.version_info >= (3, 5, 3):
22-
# Background aiohttp application
23-
from .app_aiohttp import run_server
24-
25-
# Spawn our background aiohttp app that the tests will throw
26-
# requests at.
27-
aio_server = threading.Thread(target=run_server)
28-
aio_server.daemon = True
29-
aio_server.name = "Background aiohttp server"
30-
print("Starting background aiohttp server...")
31-
aio_server.start()
32-
3321
time.sleep(1)

tests/apps/aiohttp_app/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
import sys
3+
from .app import aiohttp_server as server
4+
from ..utils import launch_background_thread
5+
6+
APP_THREAD = None
7+
8+
if 'GEVENT_TEST' not in os.environ and 'CASSANDRA_TEST' not in os.environ and sys.version_info >= (3, 5, 3):
9+
APP_THREAD = launch_background_thread(server, "AIOHTTP")

tests/apps/app_aiohttp.py renamed to tests/apps/aiohttp_app/app.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from aiohttp import web
55

6-
from ..helpers import testenv
6+
from ...helpers import testenv
77

88

99
testenv["aiohttp_port"] = 10810
@@ -14,8 +14,12 @@ def say_hello(request):
1414
return web.Response(text='Hello, world')
1515

1616

17+
def two_hundred_four(request):
18+
raise web.HTTPNoContent()
19+
20+
1721
def four_hundred_one(request):
18-
return web.HTTPUnauthorized(reason="I must simulate errors.", text="Simulated server error.")
22+
raise web.HTTPUnauthorized(reason="I must simulate errors.", text="Simulated server error.")
1923

2024

2125
def five_hundred(request):
@@ -26,12 +30,13 @@ def raise_exception(request):
2630
raise Exception("Simulated exception")
2731

2832

29-
def run_server():
33+
def aiohttp_server():
3034
loop = asyncio.new_event_loop()
3135
asyncio.set_event_loop(loop)
3236

3337
app = web.Application(debug=False)
3438
app.add_routes([web.get('/', say_hello)])
39+
app.add_routes([web.get('/204', two_hundred_four)])
3540
app.add_routes([web.get('/401', four_hundred_one)])
3641
app.add_routes([web.get('/500', five_hundred)])
3742
app.add_routes([web.get('/exception', raise_exception)])

0 commit comments

Comments
 (0)