Skip to content

Close tracing exporter during BatchTraceProcessor.shutdown()#3470

Open
mshsheikh wants to merge 1 commit into
openai:mainfrom
mshsheikh:patch-38
Open

Close tracing exporter during BatchTraceProcessor.shutdown()#3470
mshsheikh wants to merge 1 commit into
openai:mainfrom
mshsheikh:patch-38

Conversation

@mshsheikh
Copy link
Copy Markdown
Contributor

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:

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.

### 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants