Skip to content

docs(tracing): add Long-running workers section#2839

Closed
nanookclaw wants to merge 1 commit intoopenai:mainfrom
nanookclaw:docs/tracing-long-running-workers
Closed

docs(tracing): add Long-running workers section#2839
nanookclaw wants to merge 1 commit intoopenai:mainfrom
nanookclaw:docs/tracing-long-running-workers

Conversation

@nanookclaw
Copy link
Copy Markdown

Summary

In long-lived processes (Celery workers, FastAPI background tasks, RQ, Dramatiq), traces created with agents.trace() are buffered by BatchTraceProcessor but never exported — the flush that normally happens on process shutdown never fires between tasks.

This is a well-known pain point (#2135, 5 comments, reported Nov 2025), but docs/tracing.md currently has no mention of it. The section under "Additional notes" is just "View free traces at OpenAI Traces dashboard."

This PR adds a Long-running workers section that:

  • Explains why traces are dropped in worker processes
  • Documents the get_trace_provider().force_flush() pattern using the existing public API
  • Includes working Celery and FastAPI background task examples
  • Notes that force_flush() is blocking (call it after the trace context exits)

Relationship to PR #2735

PR #2735 proposes adding a dedicated flush_traces() top-level convenience function. This docs PR is complementary — it documents the underlying mechanism that flush_traces() would wrap, using the already-public get_trace_provider().force_flush() API. If #2735 ships, this section can be updated to use the simpler import.

Code references

Closes #2135

Document force_flush() pattern for Celery workers, FastAPI background
tasks, RQ, Dramatiq, and other long-lived processes where traces are
never exported because process shutdown (which normally triggers the
BatchTraceProcessor flush) never occurs between tasks.

Closes openai#2135
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Apr 3, 2026
Copy link
Copy Markdown

@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: e0b9672564

ℹ️ 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".

Comment on lines +142 to +144
are buffered by the default `BatchTraceProcessor` and only flushed automatically on process
shutdown. Because these processes never exit between tasks, buffered traces may never be exported
to the Traces dashboard.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Correct automatic flush behavior in worker guidance

This section states that BatchTraceProcessor flushes automatically only on process shutdown and that traces in long-lived workers may never export, but the runtime currently does periodic background exports (_run flushes on schedule delay and queue threshold) even without shutdown. As written, this can mislead users into adding per-task force_flush() calls that block each job and unnecessarily reduce throughput. Please update the wording to reflect that force_flush() is for immediate delivery guarantees, not because automatic export never happens.

Useful? React with 👍 / 👎.

@seratch seratch marked this pull request as draft April 4, 2026 01:52
@seratch
Copy link
Copy Markdown
Member

seratch commented Apr 4, 2026

Thanks for your interest here. Let us close this PR in favor of #2844 and #2845

@seratch seratch closed this Apr 4, 2026
@nanookclaw
Copy link
Copy Markdown
Author

Thanks for the context, @seratch — good to see the flush_traces API landing in #2844 and the docs following in #2845. The issue is resolved at the right layer (API-first, then docs). Closing this makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Traces are silently dropped in long-running workers

2 participants