Skip to content

Commit adbec50

Browse files
authored
bugfix(exporter): ensure response is closed (#4477)
1 parent 1b1e8d8 commit adbec50

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Fix intermittent `Connection aborted` error when using otlp/http exporters
11+
([#4477](https://github.com/open-telemetry/opentelemetry-python/pull/4477))
1012
- opentelemetry-sdk: use stable code attributes: `code.function` -> `code.function.name`, `code.lineno` -> `code.line.number`, `code.filepath` -> `code.file.path`
1113
([#4508](https://github.com/open-telemetry/opentelemetry-python/pull/4508))
1214
- Fix serialization of extended attributes for logs signal

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import Dict, Optional, Sequence
2222

2323
import requests
24+
from requests.exceptions import ConnectionError
2425

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

136-
return self._session.post(
137-
url=self._endpoint,
138-
data=data,
139-
verify=self._certificate_file,
140-
timeout=self._timeout,
141-
cert=self._client_cert,
142-
)
137+
# By default, keep-alive is enabled in Session's request
138+
# headers. Backends may choose to close the connection
139+
# while a post happens which causes an unhandled
140+
# exception. This try/except will retry the post on such exceptions
141+
try:
142+
resp = self._session.post(
143+
url=self._endpoint,
144+
data=data,
145+
verify=self._certificate_file,
146+
timeout=self._timeout,
147+
cert=self._client_cert,
148+
)
149+
except ConnectionError:
150+
resp = self._session.post(
151+
url=self._endpoint,
152+
data=data,
153+
verify=self._certificate_file,
154+
timeout=self._timeout,
155+
cert=self._client_cert,
156+
)
157+
return resp
143158

144159
@staticmethod
145160
def _retryable(resp: requests.Response) -> bool:

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/metric_exporter/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import requests
3131
from deprecated import deprecated
32+
from requests.exceptions import ConnectionError
3233

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

178-
return self._session.post(
179-
url=self._endpoint,
180-
data=data,
181-
verify=self._certificate_file,
182-
timeout=self._timeout,
183-
cert=self._client_cert,
184-
)
179+
# By default, keep-alive is enabled in Session's request
180+
# headers. Backends may choose to close the connection
181+
# while a post happens which causes an unhandled
182+
# exception. This try/except will retry the post on such exceptions
183+
try:
184+
resp = self._session.post(
185+
url=self._endpoint,
186+
data=data,
187+
verify=self._certificate_file,
188+
timeout=self._timeout,
189+
cert=self._client_cert,
190+
)
191+
except ConnectionError:
192+
resp = self._session.post(
193+
url=self._endpoint,
194+
data=data,
195+
verify=self._certificate_file,
196+
timeout=self._timeout,
197+
cert=self._client_cert,
198+
)
199+
return resp
185200

186201
@staticmethod
187202
def _retryable(resp: requests.Response) -> bool:

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import Dict, Optional
2222

2323
import requests
24+
from requests.exceptions import ConnectionError
2425

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

133-
return self._session.post(
134-
url=self._endpoint,
135-
data=data,
136-
verify=self._certificate_file,
137-
timeout=self._timeout,
138-
cert=self._client_cert,
139-
)
134+
# By default, keep-alive is enabled in Session's request
135+
# headers. Backends may choose to close the connection
136+
# while a post happens which causes an unhandled
137+
# exception. This try/except will retry the post on such exceptions
138+
try:
139+
resp = self._session.post(
140+
url=self._endpoint,
141+
data=data,
142+
verify=self._certificate_file,
143+
timeout=self._timeout,
144+
cert=self._client_cert,
145+
)
146+
except ConnectionError:
147+
resp = self._session.post(
148+
url=self._endpoint,
149+
data=data,
150+
verify=self._certificate_file,
151+
timeout=self._timeout,
152+
cert=self._client_cert,
153+
)
154+
return resp
140155

141156
@staticmethod
142157
def _retryable(resp: requests.Response) -> bool:

0 commit comments

Comments
 (0)