Close tracing exporter during BatchTraceProcessor.shutdown()#3470
Open
mshsheikh wants to merge 1 commit into
Open
Close tracing exporter during BatchTraceProcessor.shutdown()#3470mshsheikh wants to merge 1 commit into
BatchTraceProcessor.shutdown()#3470mshsheikh wants to merge 1 commit into
Conversation
### Summary
This PR ensures that `BatchTraceProcessor.shutdown()` closes the tracing exporter after the final batch has been flushed.
### Problem
`BackendSpanExporter` creates a persistent `httpx.Client` for trace ingestion. Before this change, shutdown drained the queue but did not explicitly close the exporter, which left the HTTP client and connection pool open.
That can leave resources allocated longer than necessary, especially in:
* long-running services
* tests that repeatedly create and tear down tracing components
* worker processes or environments with frequent startup and shutdown cycles
### Change
After the final synchronous flush, the shutdown path now looks up `close()` on the exporter and calls it when available:
```python
close_fn = getattr(self._exporter, "close", None)
if callable(close_fn):
try:
close_fn()
except Exception as exc:
logger.warning(
"[non-fatal] Tracing: exporter close failed: %s",
exc,
)
```
### Behavior
* preserves the existing final flush behavior
* closes exporter resources after export completes
* keeps cleanup non-fatal
* remains compatible with custom exporters that do not implement `close()`
### Notes
This change assumes the exporter is safe to close at processor shutdown time. If exporter ownership is later shared more broadly, that lifecycle may need to be made explicit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR ensures that
BatchTraceProcessor.shutdown()closes the tracing exporter after the final batch has been flushed.Problem
BackendSpanExportercreates a persistenthttpx.Clientfor trace ingestion. Before this change, shutdown drained the queue but did not explicitly close the exporter, which left the HTTP client and connection pool open.That can leave resources allocated longer than necessary, especially in:
Change
After the final synchronous flush, the shutdown path now looks up
close()on the exporter and calls it when available:Behavior
close()Notes
This change assumes the exporter is safe to close at processor shutdown time. If exporter ownership is later shared more broadly, that lifecycle may need to be made explicit.