Skip to content

fix(openai_agents):Added the attributes to the duration histogram #3150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adharshctr
Copy link
Contributor

@adharshctr adharshctr commented Jul 18, 2025

  • I have added tests that cover my changes.
  • If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • (If applicable) I have updated the documentation accordingly.

Important

Added attributes to duration histogram in OpenAI Agents instrumentation and updated README to include OpenAI Agents as a supported framework.

  • Instrumentation:
    • Added attributes gen_ai.agent.name, SpanAttributes.LLM_SYSTEM, and SpanAttributes.LLM_RESPONSE_MODEL to duration_histogram.record() in _wrap_agent_run_streamed() and _wrap_agent_run().
  • Documentation:
    • Updated README.md to include OpenAI Agents as a supported framework.

This description was created by Ellipsis for fd72cc6. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • Documentation

    • Updated the README to list OpenAI Agents as a supported framework, including a link to its documentation.
  • New Features

    • Enhanced telemetry for OpenAI Agents by adding system and response model details to recorded metrics.

Copy link

coderabbitai bot commented Jul 18, 2025

Walkthrough

The changes update the OpenLLMetry instrumentation to add two new span attributes—LLM_SYSTEM and LLM_RESPONSE_MODEL—when recording duration metrics for OpenAI Agent runs. Additionally, the README is updated to list OpenAI Agents as a supported framework with a documentation link.

Changes

File(s) Change Summary
README.md Added OpenAI Agents to the list of instrumented frameworks with a checkmark and documentation link.
.../openai_agents/init.py Added LLM_SYSTEM ("openai") and LLM_RESPONSE_MODEL (model name) span attributes to agent run duration metrics.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Agent
    participant Instrumentation

    User->>Agent: Initiate agent run
    Agent->>Instrumentation: Run wrapper (_wrap_agent_run/_wrap_agent_run_streamed)
    Instrumentation->>Instrumentation: Extract model name
    Instrumentation->>Instrumentation: Add LLM_SYSTEM and LLM_RESPONSE_MODEL to span attributes
    Instrumentation->>Agent: Execute agent logic
    Agent-->>Instrumentation: Return results
    Instrumentation->>Instrumentation: Record duration metric with attributes
    Instrumentation-->>User: Return agent results
Loading

Possibly related PRs

Suggested reviewers

  • nirga

Poem

Hopping through the code so neat,
New span attributes, what a treat!
OpenAI Agents join the show,
With system and model now in tow.
Metrics recorded, docs aligned—
A bunny’s work, precisely timed!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to fd72cc6 in 48 seconds. Click for details.
  • Reviewed 37 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/__init__.py:194
  • Draft comment:
    Added duration histogram attributes for streamed runs. Verify that get_model_name(agent) always returns a valid string (avoid returning null) and consider caching its result for consistency.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
2. packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/__init__.py:285
  • Draft comment:
    New attributes added to duration histogram in _wrap_agent_run. For readability, consider storing the elapsed time in a variable (like done in the streamed version) and ensure consistent attribute usage.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None

Workflow ID: wflow_upiXfdtmlYZz2Ccd

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/__init__.py (1)

189-198: Guard against non-string or None model names & compute once
Calling get_model_name(agent) can return None (or a non-str value), which invalidates OTLP attributes and may drop the data point. Compute it once, coerce to a string (or fall back to a sentinel), then reuse:

-                duration_histogram.record(
-                    duration,
-                    attributes={
-                        "gen_ai.agent.name": agent_name,
-                        SpanAttributes.LLM_SYSTEM: "openai",
-                        SpanAttributes.LLM_RESPONSE_MODEL: get_model_name(agent),
-                    },
-                )
+                model_name = get_model_name(agent)
+                if not isinstance(model_name, str):
+                    model_name = "unknown_model"
+                duration_histogram.record(
+                    duration,
+                    attributes={
+                        "gen_ai.agent.name": agent_name,
+                        SpanAttributes.LLM_SYSTEM: "openai",
+                        SpanAttributes.LLM_RESPONSE_MODEL: model_name,
+                    },
+                )
  • Ensures a single lookup of get_model_name(agent)
  • Falls back to "unknown_model" if the result is None or not a str
  • Satisfies OTLP’s requirement that attribute values be primitive types
🧹 Nitpick comments (2)
packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/__init__.py (1)

286-293: Apply the same model_name safeguard in the non-streaming path

Mirror the None-handling and single-lookup pattern here to keep metric attributes consistent.

README.md (1)

152-153: Maintain alphabetical order in the Frameworks list

The list has been alphabetical; inserting “OpenAI Agents” in its sorted position (after LangGraph, before LiteLLM) keeps the section tidy.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a750944 and fd72cc6.

📒 Files selected for processing (2)
  • README.md (1 hunks)
  • packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/__init__.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/__init__.py (1)
packages/opentelemetry-semantic-conventions-ai/opentelemetry/semconv_ai/__init__.py (1)
  • SpanAttributes (36-229)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: Test Packages (3.10)
  • GitHub Check: Test Packages (3.9)
  • GitHub Check: Test Packages (3.12)
  • GitHub Check: Test Packages (3.11)
  • GitHub Check: Build Packages (3.11)
  • GitHub Check: Lint

nirga
nirga previously approved these changes Jul 20, 2025
@@ -192,6 +192,8 @@ async def _wrap_agent_run_streamed(
duration,
attributes={
"gen_ai.agent.name": agent_name,
SpanAttributes.LLM_SYSTEM: "openai",
Copy link
Member

Choose a reason for hiding this comment

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

@adharshctr shouldn't this be the vendor (which may not always be OpenAI)?

@nirga nirga dismissed their stale review July 20, 2025 09:18

Not sure about the accuracy of this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants