Crash on starlette middleware in mount endpoints (StaticFiles)
See original GitHub issueDescribe the bug: Elastic middleware crash on access to static files
Environment (please complete the following information)
- OS: [e.g. Linux]
- Python version: 3.9.4
- Framework and version [e.g. Django 2.1]: FastAPI
- APM Server version: 7.12.1
- Agent version: 7.12.1
from __future__ import annotations
import uvicorn
from elasticapm.contrib.starlette import ElasticAPM, make_apm_client
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI(routes=[])
apm_config = {
"SERVICE_NAME": "testing",
"ENABLED": True,
"SERVER_URL": "http://localhost:8200",
"CAPTURE_HEADERS": True,
"CAPTURE_BODY": "all",
}
apm = make_apm_client(apm_config)
app.add_middleware(ElasticAPM, client=apm)
app.mount("/static",StaticFiles(directory="/"),name="static")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", debug=False)
The issue is happens when elastic running the function _get_route_name. the middleware will try to loop over the routes where starlette will return None in some cases:
File "/home/syniex/.local/lib/python3.9/site-packages/elasticapm/contrib/starlette/__init__.py", line 233, in _get_route_name
for route in routes:
└ None
TypeError: 'NoneType' object is not iterable
Starlette code that makes the issue:
@property
def routes(self) -> typing.List[BaseRoute]:
return getattr(self.app, "routes", None)
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Middleware - Starlette
A middleware class in charge of converting a WSGI application into an ASGI one. Previous Endpoints · Next Static Files.
Read more >How to use the starlette.routing.Route function in starlette - Snyk
To help you get started, we've selected a few starlette.routing. ... endpoint=raise_runtime_error), ] ) app = ExceptionMiddleware(router) client ...
Read more >`StaticFiles` causes Internal Server Error when user accesses ...
Checklist The bug is reproducible against the latest release and/or master. There are no similar issues or pull requests to fix it yet....
Read more >How to replace hyperlinks in StreamingResponse?
Is that possible to replace hyperlinks in StreamingResponse? I'm using below code to stream HTML content. from starlette.requests import Request ...
Read more >tiangolo/fastapi - Gitter
I was using pydantic model, i change it with starlette requests, ... @brsrtc I believe you could add middleware to do the same...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The PR is waiting on me. We have some upstream test failures caused by the new version of
aiopg
, which I’m working to fix here: https://github.com/elastic/apm-agent-python/pull/1178Once that’s merged we should be able to get #1137 passing and merged. Thanks for your patience.
Hiya, @Sparkycz Any Updates on this? I have a customer eagerly awaiting this as well…the PR seems stalled?