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.

Crash on starlette middleware in mount endpoints (StaticFiles)

See original GitHub issue

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

github_iconTop GitHub Comments

3reactions
basepicommented, Jun 30, 2021

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/1178

Once that’s merged we should be able to get #1137 passing and merged. Thanks for your patience.

0reactions
bvadercommented, Jun 30, 2021

Hiya, @Sparkycz Any Updates on this? I have a customer eagerly awaiting this as well…the PR seems stalled?

Read more comments on GitHub >

github_iconTop 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 >

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