Skip to content

Conversation

codeboten
Copy link
Contributor

Description

I was experiencing connection aborted when using the OTLP exporters to emit metrics. After reading the documentation for the requests library, it appears that if the body is not read, it is not necessarily returned to the connection pool.

Fix #4476

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Manually tested to validate that the connection aborted errors no longer happen.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@codeboten codeboten requested a review from a team as a code owner March 11, 2025 22:43
@xrmx
Copy link
Contributor

xrmx commented Mar 13, 2025

The metrics failure looks like a double close or something in case the response is a 200?

@codeboten
Copy link
Contributor Author

Turns out the Response() constructor doesn't set a raw property on the response object which was causing the failures. I added a check for the property to exist, but in my manual testing that was not an issue.

Signed-off-by: Alex Boten <[email protected]>
@codeboten
Copy link
Contributor Author

After further testing, the problem is caused by the Session object sending a keep-alive to the server and the server eventually deciding to close the connection. It would be possible to avoid this problem by closing the session's connection (via self._session.close()) but that would cause a new connection to be established every time, which seems wasteful.

Instead, I added a try/except around the post, to catch the ConnectionError and try to post another time (which causes the connection to be re-established successfully in this case). PTAL

@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Mar 31, 2025
@lzchen lzchen merged commit adbec50 into open-telemetry:main Apr 23, 2025
386 checks passed
@github-project-automation github-project-automation bot moved this from Ready for review to Done in @xrmx's Python PR digest Apr 23, 2025
@codeboten codeboten deleted the codeboten/fix-close-response branch April 23, 2025 20:19
DylanRussell pushed a commit to DylanRussell/opentelemetry-python that referenced this pull request Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Connection exception Connection aborted when exporting metrics periodically
4 participants