apm-agent-python
apm-agent-python copied to clipboard
Starlette middleware not respecting "capture_body" flag
Hello :)
While investigating a bug regarding very slow uploads, we discovered two things about the Starlette middleware.
- It does not honor the "capture_body" flag by consuming the stream regardless the flag is turned on or not. It replaces the original stream with a mocked one, making the Starlette request.stream() feature useless because the request is already entirely in memory (using a lot of memory in case of file uploads). I think the following piece of code could check the flag and behave differently.https://github.com/elastic/apm-agent-python/blob/69beac4a5fc72249b40fc811a9ef34fd33313735/elasticapm/contrib/starlette/init.py#L151-L163
- The same block of code above uses byte concatenation instead of a byte array which is fine for very small buffers but prohibitively slow with anything bigger because of the quadratic behavior. Gigabyte-sized streams do not work at all.
I would happily send a pull request for that, but I would like some opinions first, especially after seeing this (https://github.com/elastic/apm-agent-python/pull/1528), which seems promising.
Anyway, Elastic APM is awesome. Thanks for the great work!
@frnkvieira Great catch! We would love to see a PR if you have time. Don't worry about waiting for #1528. :)
Keep us posted if you don't have time and I can submit a quick fix.