FastAPI/Starlette: Unexpected argument app
See original GitHub issueDescribe the bug: …
Hi, I cannot seem to add ElasticAPM as middleware for the following versions:
starlette==0.13.2 fastapi==0.52.0 elastic-apm==5.5.2
The error is:
Process SpawnProcess-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/Users/xxx/.../venv/lib/python3.8/site-packages/uvicorn/subprocess.py", line 61, in subprocess_started
target(sockets=sockets)
File "/Users/xxx/.../venv/lib/python3.8/site-packages/uvicorn/main.py", line 382, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
File "/Users/xxx/.../venv/lib/python3.8/site-packages/uvicorn/main.py", line 389, in serve
config.load()
File "/Users/xxx/.../venv/lib/python3.8/site-packages/uvicorn/config.py", line 288, in load
self.loaded_app = import_from_string(self.app)
File "/Users/xxx/.../venv/lib/python3.8/site-packages/uvicorn/importer.py", line 20, in import_from_string
module = importlib.import_module(module_str)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./app.py", line 20, in <module>
app.add_middleware(ElasticAPM, client=elasticapm)
File "/Users/xxx/.../venv/lib/python3.8/site-packages/starlette/applications.py", line 117, in add_middleware
self.middleware_stack = self.build_middleware_stack()
File "/Users/xxx/.../venv/lib/python3.8/site-packages/starlette/applications.py", line 81, in build_middleware_stack
app = cls(app=app, **options)
TypeError: __init__() got an unexpected keyword argument 'app'
If you deep dive into the code for starlette you will notice that at some point it does the following as clearly stated by the applications.py code in starlette:
app = cls(app=app, **options)
however in ElasticApm we have:
def __init__(self, application, client):
self.application = application
self.client = client
so in the init it would be required to call application -> app
To Reproduce
Install python 3.8 and versions as mentioned above. Then:
app = FastAPI()
elasticapm = make_apm_client(config={
'SERVICE_NAME': 'foo',
'SECRET_TOKEN': ''
})
app.add_middleware(ElasticAPM, client=elasticapm)
Environment (please complete the following information)
-
OS: MAC OS Catalina 10.15.3
-
Python version: Python 3.8
-
Framework and version [e.g. Django 2.1]: Starlette/FastAPI
starlette==0.13.2 fastapi==0.52.0 elastic-apm==5.5.2
-
APM Server version: apm-server-7.6.1
-
Agent version:
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Hi, sure I can do a PR. Thanks for the quick response.
No problem! I’m glad it was easy!