Skip to content

Commit 79946d8

Browse files
committed
Pass remaining timeout to shutdown
1 parent fb7f320 commit 79946d8

File tree

1 file changed

+9
-2
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/_shared_internal

1 file changed

+9
-2
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_shared_internal/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import annotations
1616

1717
import collections
18+
import time
1819
import enum
1920
import inspect
2021
import logging
@@ -185,20 +186,26 @@ def emit(self, data: Telemetry) -> None:
185186
def shutdown(self, timeout_millis: int = 30000):
186187
if self._shutdown:
187188
return
189+
shutdown_should_end = time.time() + timeout_millis / 1000
188190
# Causes emit to reject telemetry and makes force_flush a no-op.
189191
self._shutdown = True
190192
# Interrupts sleep in the worker if it's sleeping.
191193
self._worker_awaken.set()
192194
self._worker_thread.join(timeout_millis / 1000)
193195
# Stops worker thread from calling export again if queue is still not empty.
194196
self._shutdown_timeout_exceeded = True
195-
# We want to shutdown immediately because we already waited `timeout_millis`.
197+
# We want to shutdown immediately only if we already waited `timeout_millis`.
198+
# Otherwise we pass the remaining timeout to the exporter.
196199
# Some exporter's shutdown support a timeout param.
197200
if (
198201
"timeout_millis"
199202
in inspect.getfullargspec(self._exporter.shutdown).args
200203
):
201-
self._exporter.shutdown(timeout_millis=0) # type: ignore
204+
remaining_time = shutdown_should_end - time.time()
205+
if remaining_time < 0:
206+
self._exporter.shutdown(timeout_millis=0) # type: ignore
207+
else:
208+
self._exporter.shutdown(timeout_millis=remaining_time * 1000) # type: ignore
202209
else:
203210
self._exporter.shutdown()
204211
# Worker thread **should** be finished at this point, because we called shutdown on the exporter,

0 commit comments

Comments
 (0)