question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FastAPI/Starlette: Unexpected argument app

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
arocketmancommented, Mar 25, 2020

Hi, sure I can do a PR. Thanks for the quick response.

0reactions
basepicommented, Mar 25, 2020

No problem! I’m glad it was easy!

Read more comments on GitHub >

github_iconTop Results From Across the Web

tiangolo/fastapi - Gitter
... Depends from starlette.testclient import TestClient app = FastAPI() ... I get an error TypeError: build() got an unexpected keyword argument 'stream'.
Read more >
Handling Errors - FastAPI
When raising an HTTPException , you can pass any value that can be converted to JSON as the parameter detail , not only...
Read more >
FastAPI/Starlette websocket.close() not accepting "reason ...
I was running an old version of FastAPI whose close() did not support "reason" argument. Updgrading FastAPI to the latest version solved the ......
Read more >
FastAPI/Starlette debug vs prod - William Hayes, PhD - Medium
app --host 0.0.0.0 --reload --port 80 is the part that runs the following dockerfile using uvicorn directly in debug mode so that it...
Read more >
Build And Host Fast Data Science Applications Using FastAPI
Uvicorn is a lightning-fast ASGI server that supports asyncio frameworks. Starlette is a lightweight ASGI framework that enables us to build ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found