Skip to content

fix(otel): stop sending traces to api.openai.com and cleanup - #2400

Merged
EItanya merged 2 commits into
kagent-dev:mainfrom
krisztianfekete:fix/openai-agents-native-trace-exporter-and-cleanup
Aug 7, 2026
Merged

fix(otel): stop sending traces to api.openai.com and cleanup#2400
EItanya merged 2 commits into
kagent-dev:mainfrom
krisztianfekete:fix/openai-agents-native-trace-exporter-and-cleanup

Conversation

@krisztianfekete

@krisztianfekete krisztianfekete commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

The Agents SDK registers a trace processor that POSTs everything to a hardcoded https://api.openai.com/v1/traces/ingest using OPENAI_API_KEY. It ignores OPENAI_API_BASE, so anyone behind other proxies is shipping their gateway's key to OpenAI and getting 401s back.

OpenAIAgentsInstrumentor adds the OTel processor next to that one rather than replacing it, and set_tracing_disabled(True) would silence the OTel spans too, so we now drop the built-in processor before instrumenting. Set KAGENT_OPENAI_AGENTS_NATIVE_TRACING=true to keep it if you're using a real OpenAI key.

Using OpenAIAgentsInstrumentor(replace_existing_processors=True) as suggested in review, with the pin bumped to >=0.52.3,<0.53.0 since that's when the kwarg landed. Doing it through the kwarg also means a second build() can't wipe the OTel processor, since instrument() no-ops once instrumented.

Cleaned up a couple of other smaller stuff:

  • Resource(...) -> Resource.create(...): the bare constructor ignores OTEL_RESOURCE_ATTRIBUTES and drops telemetry.sdk.*, so nobody could set deployment.environment.name or service.version at all.
  • Same bug in the Go ADK, resource.New starts empty so it needs WithFromEnv() + WithTelemetrySDK().
  • HTTPXClientInstrumentor().instrument(excluded_urls=...) did nothing, cleaned it up.
  • _a2a.py read self.config.kagent_url but KAgentConfig only has url, which blows up whenever KAGENT_URL isn't set.

Rebased onto main, which dropped the Gemini bullet from this PR since main landed the same fix independently.

@krisztianfekete
krisztianfekete requested a review from a team as a code owner August 7, 2026 11:10
Copilot AI lite review requested due to automatic review settings August 7, 2026 11:10
@github-actions github-actions Bot added the bug Something isn't working label Aug 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens and cleans up OpenTelemetry integration around the OpenAI Agents SDK runtime to prevent unintended trace export to OpenAI’s hardcoded ingest endpoint (which can leak non-OpenAI gateway keys and cause 401s), while preserving OpenTelemetry-based tracing.

Changes:

  • Drop the OpenAI Agents SDK native trace processor by default (opt-in via KAGENT_OPENAI_AGENTS_NATIVE_TRACING=true) and instrument the SDK via OpenAIAgentsInstrumentor.
  • Ensure Resource.create(...) is used so OTEL_RESOURCE_ATTRIBUTES and telemetry.sdk.* attributes are honored.
  • Ensure Gemini instrumentation still runs when OTEL_LOGGING_ENABLED=true, and remove a no-op excluded_urls argument from HTTPXClientInstrumentor().instrument(...).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
python/packages/kagent-openai/tests/test_tracing.py Adds regression tests proving the Agents SDK default exporter targets OpenAI, and that kagent drops it by default while keeping OTel spans working.
python/packages/kagent-openai/src/kagent/openai/_a2a.py Introduces _configure_openai_agents_tracing() to remove native exporting (by default) and warn when SDK tracing is disabled, then wires it into app startup.
python/packages/kagent-core/tests/test_tracing_configure.py Adds tests ensuring Google/Gemini instrumentation still runs in the logging-only branch and that OTEL_RESOURCE_ATTRIBUTES are merged into the Resource.
python/packages/kagent-core/src/kagent/core/tracing/_utils.py Switches to Resource.create(...), removes ineffective excluded_urls for HTTPX instrumentor, and ensures Google/Gemini instrumentation also runs when logging is enabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@krisztianfekete
krisztianfekete force-pushed the fix/openai-agents-native-trace-exporter-and-cleanup branch from e90b6d4 to 7109dd5 Compare August 7, 2026 11:13

@dhaifley dhaifley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, but what if we just used the newer version mentioned in the PR description? >=0.52.3,<0.53.0? I'm not familiar enough to know if there is a reason we cannot do that.

Also, does the Go ADK code need a similar fix: go/adk/pkg/telemetry/tracing.go:81-84?

Comment thread python/packages/kagent-openai/src/kagent/openai/_a2a.py Outdated
Comment thread python/packages/kagent-openai/src/kagent/openai/_a2a.py
Comment thread python/packages/kagent-core/src/kagent/core/tracing/_utils.py Outdated
@krisztianfekete

Copy link
Copy Markdown
Contributor Author

Thanks @dhaifley, addressed your comments and fixed a new bug with KAgentConfig. Didn't want to upgrade originally, as hopefully shortly we're moving away from the Traceloop libraries, but couldn't find dangerous changes, so went with it this time.

@krisztianfekete
krisztianfekete force-pushed the fix/openai-agents-native-trace-exporter-and-cleanup branch from c69c8ff to 52fdeac Compare August 7, 2026 16:02
…urce attrs

Signed-off-by: krisztianfekete <git@krisztianfekete.org>
Signed-off-by: krisztianfekete <git@krisztianfekete.org>
@krisztianfekete
krisztianfekete force-pushed the fix/openai-agents-native-trace-exporter-and-cleanup branch from 52fdeac to 7cb25f1 Compare August 7, 2026 17:22
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Aug 7, 2026
@EItanya
EItanya merged commit 74321ee into kagent-dev:main Aug 7, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants