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

bugfix(exporter): ensure response is closed #4477

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

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]>
@@ -167,8 +167,12 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
resp = self._export(serialized_data)
# pylint: disable=no-else-return
if resp.ok:
if resp.raw is not None:
Copy link
Contributor

@xrmx xrmx Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if moving this right after getting resp will make resp.ok and self._retryable(resp) change behavior? If not would be nice to do this just once.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick test it looks like it should be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion, updated the code and tested it locally, seems ok

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connection exception Connection aborted when exporting metrics periodically
3 participants