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
28 changes: 28 additions & 0 deletions instana/instrumentation/fastapi_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,31 @@
from ..util import running_in_gunicorn
from .asgi import InstanaASGIMiddleware
from starlette.middleware import Middleware
from fastapi import HTTPException
from fastapi.exception_handlers import http_exception_handler

from instana.singletons import async_tracer

if hasattr(fastapi, '__version__') and \
(LooseVersion(fastapi.__version__) >= LooseVersion('0.51.0')):

async def instana_exception_handler(request, exc):
"""
We capture FastAPI HTTPException, log the error and pass it on
to the default exception handler.
"""
try:
span = async_tracer.active_span

if span is not None:
if hasattr(exc, 'detail'):
span.set_tag('http.error', exc.detail)
span.set_tag('http.status_code', exc.status_code)
except Exception:
logger.debug("FastAPI instana_exception_handler: ", exc_info=True)

return await http_exception_handler(request, exc)

@wrapt.patch_function_wrapper('fastapi.applications', 'FastAPI.__init__')
def init_with_instana(wrapped, instance, args, kwargs):
middleware = kwargs.get('middleware')
Expand All @@ -25,6 +46,13 @@ def init_with_instana(wrapped, instance, args, kwargs):
elif isinstance(middleware, list):
middleware.append(Middleware(InstanaASGIMiddleware))

exception_handlers = kwargs.get('exception_handlers')
if exception_handlers is None:
kwargs['exception_handlers'] = dict()

if isinstance(kwargs['exception_handlers'], dict):
kwargs['exception_handlers'][HTTPException] = instana_exception_handler

return wrapped(*args, **kwargs)

logger.debug("Instrumenting FastAPI")
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ def check_setuptools():
'nose>=1.0',
'pyramid>=1.2',
'pytest>=4.6',
'urllib3[secure]>=1.15'
'urllib3[secure]>=1.15,<=1.25.11'
],
'test-cassandra': [
'cassandra-driver==3.20.2',
'mock>=2.0.0',
'nose>=1.0',
'pytest>=4.6',
'urllib3[secure]>=1.15'
'urllib3[secure]>=1.15<=1.25.11'
],
'test-couchbase': [
'couchbase==2.5.9',
Expand Down Expand Up @@ -122,7 +122,7 @@ def check_setuptools():
'suds-jurko>=0.6',
'tornado>=4.5.3,<6.0',
'uvicorn>=0.12.2;python_version>="3.6"',
'urllib3[secure]>=1.15'
'urllib3[secure]>=1.15,<=1.25.11'
],
},
test_suite='nose.collector',
Expand Down