Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better "capture_body" flag support #1549

Merged
merged 7 commits into from
Aug 29, 2022
Merged

Better "capture_body" flag support #1549

merged 7 commits into from
Aug 29, 2022

Conversation

frnkvieira
Copy link
Contributor

What does this pull request do?

  • Fixes "capture_body" flag not being respected causing the request.stream() of Starlette not being able to properly stream (by loading everything into memory first)
  • Fixes non-UTF-8 data triggering errors in requests that should be ignored as configured.
  • Improves performance of all requests (especially those who send big payloads like files) by not mocking the stream at all in case the capture_body flag is turned off (which is the default). When the flag is turned on requests are also faster because of the faster loop by using a list of bytes instead of bytes concatenation.

I'm still wrapping my head around how this agent works so my apologies in advance if I made any wrong assumptions =)

Related issues

closes #1545

@github-actions github-actions bot added agent-python community Issues opened by the community triage Issues awaiting triage labels May 16, 2022
@apmmachine
Copy link
Contributor

apmmachine commented May 16, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-08-29T16:43:17.764+0000

  • Duration: 27 min 11 sec

Test stats 🧪

Test Results
Failed 0
Passed 4974
Skipped 3338
Total 8312

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /test linters : Run the Python linters only.

  • /test full : Run the full matrix of tests.

  • /test benchmark : Run the APM Agent Python benchmarks tests.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@beniwohli
Copy link
Contributor

Hi @frnkvieira! Thanks for opening the PR, this looks great! From what I see, we have a pretty similar approach to handling the body in the new generic ASGI middleware (#1528).

Can you point me towards the change that ignores encoding errors? I would have suspected that you added errors="ignore" where we decode the byte string here

return body.decode("utf-8")

@apmmachine
Copy link
Contributor

apmmachine commented May 16, 2022

🌐 Coverage report

Name Metrics % (covered/total) Diff
Packages 100.0% (66/66) 💚
Files 100.0% (225/225) 💚
Classes 100.0% (225/225) 💚
Lines 89.841% (17439/19411) 👎 -0.078
Conditionals 77.255% (3186/4124) 👎 -0.026

@frnkvieira
Copy link
Contributor Author

Hi @beniwohli, to be clear, the intent of this pull request is closing #1545, but as a side effect, this fixes non-UTF-8 errors in ignored paths, only ignored ones, thanks to the ignore path checkup being done way earlier (which seems a good choice to me but maybe I'm missing something). (https://github.com/frnkvieira/apm-agent-python/blob/main/elasticapm/contrib/starlette/__init__.py#L134)

Btw, I don't think crashing on non-utf-8 data is a good thing, but I didn't feel confident enough to keep changing stuff =P
Ignore the errors may be a solution, although it would probably create some strange corner cases for people processing the APM data... encode it in base64 or similar may help preserve the original content but would create other problems... which brings me to other questions, maybe add a size limit where the content gets truncated ?... Anyway... thanks for the fast reply =)

@julianogv
Copy link

Hi @beniwohli, any idea on when you might merge this PR and publish it to pypi?
I'm currently using a forked version of elastic-apm due to this issue (and some other fixes that are already fixed/merged and not published yet).

@basepi basepi assigned basepi and unassigned beniwohli Aug 25, 2022
Colton Myers added 2 commits August 25, 2022 11:47
Also update encoding.long_field to handle bytes
@basepi
Copy link
Contributor

basepi commented Aug 25, 2022

@frnkvieira looks like you've turned off "allow maintainer edits" so I opened a PR instead: https://github.com/frnkvieira/apm-agent-python/pull/1

@julianogv apologies for the delay here!

@basepi
Copy link
Contributor

basepi commented Aug 25, 2022

/test

@frnkvieira
Copy link
Contributor Author

I'm probably missing something because the flag seems to be checked.
Anyway, I just merged the PR on the fork, thanks.

@basepi
Copy link
Contributor

basepi commented Aug 29, 2022

/test

@basepi
Copy link
Contributor

basepi commented Aug 29, 2022

/test linters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-python community Issues opened by the community triage Issues awaiting triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Starlette middleware not respecting "capture_body" flag
5 participants