Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions instana/instrumentation/aiohttp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ async def stan_middleware(request, handler):
url = str(request.url)
parts = url.split('?')
if len(parts) > 1:
cleaned_qp = strip_secrets_from_query(parts[1], agent.options.secrets_matcher, agent.options.secrets_list)
cleaned_qp = strip_secrets_from_query(parts[1],
agent.options.secrets_matcher,
agent.options.secrets_list)
scope.span.set_tag("http.params", cleaned_qp)

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

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

return response
except Exception as e:
except Exception as exc:
logger.debug("aiohttp stan_middleware", exc_info=True)
if scope is not None:
scope.span.set_tag("http.status_code", 500)
scope.span.log_exception(e)
scope.span.log_exception(exc)
raise
finally:
if scope is not None:
scope.close()


@wrapt.patch_function_wrapper('aiohttp.web','Application.__init__')
@wrapt.patch_function_wrapper('aiohttp.web', 'Application.__init__')
def init_with_instana(wrapped, instance, argv, kwargs):
if "middlewares" in kwargs:
kwargs["middlewares"].insert(0, stan_middleware)
Expand All @@ -78,4 +80,3 @@ def init_with_instana(wrapped, instance, argv, kwargs):
logger.debug("Instrumenting aiohttp server")
except ImportError:
pass

12 changes: 0 additions & 12 deletions tests/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,4 @@
print("Starting background RPC app...")
rpc_server_thread.start()

if sys.version_info >= (3, 5, 3):
# Background aiohttp application
from .app_aiohttp import run_server

# Spawn our background aiohttp app that the tests will throw
# requests at.
aio_server = threading.Thread(target=run_server)
aio_server.daemon = True
aio_server.name = "Background aiohttp server"
print("Starting background aiohttp server...")
aio_server.start()

time.sleep(1)
9 changes: 9 additions & 0 deletions tests/apps/aiohttp_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os
import sys
from .app import aiohttp_server as server
from ..utils import launch_background_thread

APP_THREAD = None

if 'GEVENT_TEST' not in os.environ and 'CASSANDRA_TEST' not in os.environ and sys.version_info >= (3, 5, 3):
APP_THREAD = launch_background_thread(server, "AIOHTTP")
11 changes: 8 additions & 3 deletions tests/apps/app_aiohttp.py → tests/apps/aiohttp_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio
from aiohttp import web

from ..helpers import testenv
from ...helpers import testenv


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


def two_hundred_four(request):
raise web.HTTPNoContent()


def four_hundred_one(request):
return web.HTTPUnauthorized(reason="I must simulate errors.", text="Simulated server error.")
raise web.HTTPUnauthorized(reason="I must simulate errors.", text="Simulated server error.")


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


def run_server():
def aiohttp_server():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

app = web.Application(debug=False)
app.add_routes([web.get('/', say_hello)])
app.add_routes([web.get('/204', two_hundred_four)])
app.add_routes([web.get('/401', four_hundred_one)])
app.add_routes([web.get('/500', five_hundred)])
app.add_routes([web.get('/exception', raise_exception)])
Expand Down
Loading