Skip to content

feat(tracing): add flush_traces() for long-running workers#2735

Draft
mvanhorn wants to merge 6 commits intoopenai:mainfrom
mvanhorn:feat/public-flush-traces-api
Draft

feat(tracing): add flush_traces() for long-running workers#2735
mvanhorn wants to merge 6 commits intoopenai:mainfrom
mvanhorn:feat/public-flush-traces-api

Conversation

@mvanhorn
Copy link

Summary

In long-running worker processes (Celery, FastAPI background tasks, RQ, Dramatiq), traces created with agents.trace() are buffered by BatchTraceProcessor but never exported because the process never exits to trigger shutdown flushing. Users currently have to access private internals (get_trace_provider()._multi_processor.force_flush()) to flush traces.

This PR adds a public flush_traces() function that delegates to the trace provider's force_flush():

from agents import flush_traces

@celery_app.task
def my_task():
    with trace("my_task"):
        # ... do work ...
        pass
    flush_traces()  # ensure traces are exported

Test plan

  • Added test_flush_traces_calls_provider_force_flush - verifies the function delegates to the provider
  • Added test_flush_traces_importable_from_agents - verifies the public API export works
  • All existing tests pass (make tests - 2405 passed)

Issue number

Closes #2135

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

This contribution was developed with AI assistance (Claude Code).

In long-running processes (Celery, FastAPI background tasks, RQ),
traces are buffered indefinitely because the process never exits to
trigger shutdown flushing. This adds flush_traces() as a public API
that delegates to the trace provider's force_flush(), allowing users
to explicitly flush traces at the end of each task.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30cb3f99ec

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're willing to take more time, please continue working until Codex auto review gives 👍 to your PR. Then, we'll check the direction and details.

…lity

The flush_traces() function calls get_trace_provider().force_flush(),
but TraceProvider (the abstract base class) did not declare force_flush
as an abstract method. mypy correctly flagged this as attr-defined error
since only DefaultTraceProvider had the concrete implementation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f29ffb7a27

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

…patibility

Addresses Codex review feedback: existing TraceProvider subclasses that
don't override force_flush or shutdown will no longer break at
instantiation. Both methods now default to no-ops on the base class.

Adds a test proving a minimal custom provider works with flush_traces().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mvanhorn
Copy link
Author

Addressed Codex findings in 1ed604d: made force_flush and shutdown non-abstract on TraceProvider with default no-ops for backward compatibility. Added test proving a minimal custom provider works with flush_traces().

@codex review

Add hasattr check so flush_traces() is safe to call even with custom
TraceProvider implementations that predate the force_flush method.
@mvanhorn
Copy link
Author

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 25377a406a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

force_flush() now checks the disabled flag before flushing processors,
matching the behavior of shutdown(). Prevents buffered telemetry from
being exported after set_tracing_disabled(True).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mvanhorn
Copy link
Author

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Can't wait for the next one!

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you fix the lint / typecheck errors?

Add explicit `pass` to TraceProvider.force_flush/shutdown to satisfy
ruff B027 (empty method in abstract class without @AbstractMethod).
Expand MinimalProvider test stub signatures to match the parent class.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mvanhorn
Copy link
Author

Fixed in 21d84de:

  • Lint (B027): added pass to TraceProvider.force_flush/shutdown so ruff doesn't flag them as empty abstract methods
  • Typecheck (mypy): expanded MinimalProvider test stub signatures to match the parent class

@seratch seratch assigned seratch and unassigned seratch Mar 21, 2026
@seratch seratch marked this pull request as draft March 21, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature:tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Traces are silently dropped in long-running workers

2 participants