Skip to content

bugfix(exporter): ensure response is closed #4477

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

Merged
merged 17 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix intermittent `Connection aborted` error when using otlp/http exporters
([#4477](https://github.com/open-telemetry/opentelemetry-python/pull/4477))

## Version 1.32.0/0.53b0 (2025-04-10)

- Fix user agent in OTLP HTTP metrics exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Dict, Optional, Sequence

import requests
from requests.exceptions import ConnectionError

from opentelemetry.exporter.otlp.proto.common._internal import (
_create_exp_backoff_generator,
Expand Down Expand Up @@ -133,13 +134,27 @@ def _export(self, serialized_data: bytes):
elif self._compression == Compression.Deflate:
data = zlib.compress(serialized_data)

return self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
# By default, keep-alive is enabled in Session's request
# headers. Backends may choose to close the connection
# while a post happens which causes an unhandled
# exception. This try/except will retry the post on such exceptions
try:
resp = self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
except ConnectionError:
resp = self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
return resp

@staticmethod
def _retryable(resp: requests.Response) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import requests
from deprecated import deprecated
from requests.exceptions import ConnectionError

from opentelemetry.exporter.otlp.proto.common._internal import (
_create_exp_backoff_generator,
Expand Down Expand Up @@ -175,13 +176,27 @@ def _export(self, serialized_data: bytes):
elif self._compression == Compression.Deflate:
data = zlib.compress(serialized_data)

return self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
# By default, keep-alive is enabled in Session's request
# headers. Backends may choose to close the connection
# while a post happens which causes an unhandled
# exception. This try/except will retry the post on such exceptions
try:
resp = self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
except ConnectionError:
resp = self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
return resp

@staticmethod
def _retryable(resp: requests.Response) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Dict, Optional

import requests
from requests.exceptions import ConnectionError

from opentelemetry.exporter.otlp.proto.common._internal import (
_create_exp_backoff_generator,
Expand Down Expand Up @@ -130,13 +131,27 @@ def _export(self, serialized_data: bytes):
elif self._compression == Compression.Deflate:
data = zlib.compress(serialized_data)

return self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
# By default, keep-alive is enabled in Session's request
# headers. Backends may choose to close the connection
# while a post happens which causes an unhandled
# exception. This try/except will retry the post on such exceptions
try:
resp = self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
except ConnectionError:
resp = self._session.post(
url=self._endpoint,
data=data,
verify=self._certificate_file,
timeout=self._timeout,
cert=self._client_cert,
)
return resp

@staticmethod
def _retryable(resp: requests.Response) -> bool:
Expand Down