Skip to content

Commit 8c5c0f8

Browse files
committed
update to retry the post once
Signed-off-by: Alex Boten <[email protected]>
1 parent 7664191 commit 8c5c0f8

File tree

3 files changed

+67
-28
lines changed
  • exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http

3 files changed

+67
-28
lines changed

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

Lines changed: 22 additions & 9 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:
@@ -165,8 +180,6 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
165180
return LogExportResult.FAILURE
166181

167182
resp = self._export(serialized_data)
168-
if resp.raw is not None:
169-
resp.close()
170183
# pylint: disable=no-else-return
171184
if resp.ok:
172185
return LogExportResult.SUCCESS

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929

3030
import requests
31+
from requests.exceptions import ConnectionError
3132
from deprecated import deprecated
3233

3334
from opentelemetry.exporter.otlp.proto.common._internal import (
@@ -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+
try:
180+
resp = self._session.post(
181+
url=self._endpoint,
182+
data=data,
183+
verify=self._certificate_file,
184+
timeout=self._timeout,
185+
cert=self._client_cert,
186+
)
187+
except ConnectionError:
188+
# By default, keep-alive is set in Session's request
189+
# headers. Backends may choose to close the connection
190+
# while a post happens which causes an unhandled
191+
# exception. This try/except will retry the post on such exceptions
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:
@@ -205,8 +220,6 @@ def export(
205220
return MetricExportResult.FAILURE
206221

207222
resp = self._export(serialized_data.SerializeToString())
208-
if resp.raw is not None:
209-
resp.close()
210223
# pylint: disable=no-else-return
211224
if resp.ok:
212225
return MetricExportResult.SUCCESS

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

Lines changed: 23 additions & 10 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,
@@ -129,14 +130,28 @@ def _export(self, serialized_data: bytes):
129130
data = gzip_data.getvalue()
130131
elif self._compression == Compression.Deflate:
131132
data = zlib.compress(serialized_data)
132-
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-
)
133+
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:
@@ -157,8 +172,6 @@ def _export_serialized_spans(self, serialized_data):
157172
return SpanExportResult.FAILURE
158173

159174
resp = self._export(serialized_data)
160-
if resp.raw is not None:
161-
resp.close()
162175
# pylint: disable=no-else-return
163176
if resp.ok:
164177
return SpanExportResult.SUCCESS

0 commit comments

Comments
 (0)