Skip to content

OpenAI auto instrumentation fix attributes#122

Merged
nikhilNava merged 4 commits intomainfrom
nikhilc/OpenAIAutoInstrumentation-fixattributes
Jan 21, 2026
Merged

OpenAI auto instrumentation fix attributes#122
nikhilNava merged 4 commits intomainfrom
nikhilc/OpenAIAutoInstrumentation-fixattributes

Conversation

@nikhilNava
Copy link
Contributor

@nikhilNava nikhilNava commented Jan 21, 2026

Task
For open ai auto instrumentation fix update the spanProcessor so the generated spans pass certification.
Aditionally ensure that on invoke span the input and outputs are string or list of strings

Solution
After the updates view the spans below
Invoke agent span

{
    "name": "invoke_agent MathsProfessor",
    "context": {
        "trace_id": "0x4c25bc0348ff2c181824525113ca9873",
        "span_id": "0x314f3aaa59991b93",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2026-01-21T17:23:41.139763Z",
    "end_time": "2026-01-21T17:23:43.208501Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "gen_ai.operation.name": "invoke_agent",
        "gen_ai.system": "openai",
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "bcb35e95-8792-47a5-853e-90a026374de1",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.caller.id": "a92962f3-9ed4-4bcd-9ae0-ad0002b6ca76",
        "gen_ai.caller.name": "Alex Wilber",
        "gen_ai.caller.upn": "Sample UPN",
        "gen_ai.execution.type": "HumanToAgent",
        "gen_ai.input.messages": "Compute 15 % 4",
        "gen_ai.output.messages": "The result of \\( 15 \\mod 4 \\) is \\( 3 \\)."
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.39.1",
            "service.namespace": "OpenAIAgentsTesting",
            "service.name": "OpenAIAgentsTracing"
        },
        "schema_url": ""
    }
}

Execute tool span

{
    "name": "execute_tool calc",
    "context": {
        "trace_id": "0x4c25bc0348ff2c181824525113ca9873",
        "span_id": "0xed1ac21c316e4853",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x314f3aaa59991b93",
    "start_time": "2026-01-21T17:23:42.585274Z",
    "end_time": "2026-01-21T17:23:42.586274Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "gen_ai.operation.name": "execute_tool",
        "gen_ai.system": "openai",
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "bcb35e95-8792-47a5-853e-90a026374de1",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.tool.name": "calc",
        "gen_ai.tool.arguments": "{\"expression\":\"15 % 4\"}",
        "gen_ai.event.content": "3",
        "gen_ai.tool.type": "function",
        "gen_ai.tool.call.id": "call_emD9ZN2vUWIOTsUvSytDXaWY"
    },

Inference Span

{
 "name": "chat gpt-4o-mini",
 "context": {
     "trace_id": "0x4c25bc0348ff2c181824525113ca9873",
     "span_id": "0xca814e4403a6f008",
     "trace_state": "[]"
 },
 "kind": "SpanKind.INTERNAL",
 "parent_id": "0x314f3aaa59991b93",
 "start_time": "2026-01-21T17:23:42.593281Z",
 "end_time": "2026-01-21T17:23:43.204489Z",
 "status": {
     "status_code": "OK"
 },
 "attributes": {
     "gen_ai.operation.name": "chat",
     "gen_ai.system": "openai",
     "operation.source": "SDK",
     "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
     "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
     "gen_ai.conversation.id": "bcb35e95-8792-47a5-853e-90a026374de1",
     "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
     "gen_ai.agent.name": "Perplexity02 Agent",
     "gen_ai.agent.upn": "Sample Agent UPN",
     "gen_ai.agent.applicationid": "Sample Blueprint ID",
     "gen_ai.caller.client.ip": "0.0.0.0",
     "gen_ai.channel.name": "msteams",
     "gen_ai.provider.name": "openai",
     "gen_ai.request.model": "gpt-4o-mini",
     "gen_ai.execution.payload": "{\"base_url\": \"https://nikhilc-aoai.cognitiveservices.azure.com/openai/deployments/gpt-4o-mini/\"}",
     "gen_ai.input.messages": "[{\"content\": \"\\nYou are a smart Maths professor. Prefer precise, terse mathematical answers.\\nWhen arithmetic or evaluation is needed, USE the `calc` tool with a clean expression.\\n\", \"role\": \"system\"}, {\"role\": \"user\", \"content\": \"Compute 15 % 4\"}, {\"role\": \"assistant\", \"tool_calls\": [{\"id\": \"call_emD9ZN2vUWIOTsUvSytDXaWY\", \"type\": \"function\", \"function\": {\"name\": \"calc\", \"arguments\": \"{\\\"expression\\\":\\\"15 % 4\\\"}\"}}]}, {\"role\": \"tool\", \"tool_call_id\": \"call_emD9ZN2vUWIOTsUvSytDXaWY\", \"content\": \"3\"}]",
     "gen_ai.input.messages.0.message_role": "system",
     "gen_ai.input.messages.0.message_content": "\nYou are a smart Maths professor. Prefer precise, terse mathematical answers.\nWhen arithmetic or evaluation is needed, USE the `calc` tool with a clean expression.\n",
     "gen_ai.input.messages.1.message_role": "user",
     "gen_ai.input.messages.1.message_content": "Compute 15 % 4",
     "gen_ai.input.messages.2.message_role": "assistant",
     "gen_ai.input.messages.2.message_tool_calls.0.gen_ai.tool.call.id": "call_emD9ZN2vUWIOTsUvSytDXaWY",
     "gen_ai.input.messages.2.message_tool_calls.0.gen_ai.tool.name": "calc",
     "gen_ai.input.messages.2.message_tool_calls.0.gen_ai.tool.arguments": "{\"expression\":\"15 % 4\"}",
     "gen_ai.input.messages.3.message_role": "tool",
     "gen_ai.input.messages.3.message_content": "3",
     "gen_ai.input.messages.3.message_tool_id": "call_emD9ZN2vUWIOTsUvSytDXaWY",       
     "gen_ai.output.messages": "[{\"content\": \"The result of \\\\( 15 \\\\mod 4 \\\\) is \\\\( 3 \\\\).\", \"refusal\": null, \"role\": \"assistant\", \"annotations\": [], \"audio\": null, \"function_call\": null, \"tool_calls\": null}]",
     "gen_ai.output.messages.0.message_role": "assistant",
     "gen_ai.output.messages.0.message_content": "The result of \\( 15 \\mod 4 \\) is \\( 3 \\).",
     "gen_ai.usage.input_tokens": 175,
     "gen_ai.usage.output_tokens": 20
 },
 "events": [],
 "links": [],
 "resource": {
     "attributes": {
         "telemetry.sdk.language": "python",
         "telemetry.sdk.name": "opentelemetry",
         "telemetry.sdk.version": "1.39.1",
         "service.namespace": "OpenAIAgentsTesting",
         "service.name": "OpenAIAgentsTracing"
     },
     "schema_url": ""
 }
}

@nikhilNava nikhilNava requested a review from a team as a code owner January 21, 2026 16:34
Copilot AI review requested due to automatic review settings January 21, 2026 16:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes attribute mapping issues in OpenAI auto-instrumentation to ensure proper telemetry data collection according to the observability schema.

Changes:

  • Updated FunctionSpan attributes to use correct semantic convention keys (GEN_AI_TOOL_ARGS_KEY instead of GEN_AI_INPUT_MESSAGES_KEY, GEN_AI_EVENT_CONTENT instead of GEN_AI_OUTPUT_MESSAGES_KEY)
  • Implemented input/output message capture mechanism from GenerationSpan to populate AgentSpan attributes
  • Added tool_call_id tracking to link tool calls from GenerationSpan to FunctionSpan execution
  • Added new integration test validating required attributes on invoke_agent spans

Reviewed changes

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

File Description
test_openai_trace_processor.py Added comprehensive integration test to validate all required attributes on invoke_agent spans, including execution type, agent details, and input/output messages
utils.py Fixed attribute keys in get_attributes_from_function_span_data; added helper functions to capture and track tool call IDs, input/output messages, and find ancestor agent spans
trace_processor.py Added tracking dictionaries for agent spans, parent-child relationships, and pending tool calls; updated span processing to capture and propagate messages and tool call IDs; changed agent span attribute from GEN_AI_GRAPH_NODE_ID to GEN_AI_EXECUTION_TYPE_KEY
util.py Added GEN_AI_CALLER_CLIENT_IP_KEY to COMMON_ATTRIBUTES list for proper telemetry collection

Copy link
Contributor

Copilot AI commented Jan 21, 2026

@nikhilNava I've opened a new pull request, #125, to work on those changes. Once the pull request is ready, I'll request review from you.

* Initial plan

* Implement bounded size for _pending_tool_calls to prevent memory growth

Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>

* Fix trailing empty line in test file

Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
juliomenendez
juliomenendez previously approved these changes Jan 21, 2026
@nikhilNava
Copy link
Contributor Author

@copilot - fix formatting my running
Run uv run --frozen ruff format --check .

Copy link
Contributor

Copilot AI commented Jan 21, 2026

@nikhilNava I've opened a new pull request, #126, to work on those changes. Once the pull request is ready, I'll request review from you.

* Initial plan

* Fix formatting with ruff format

Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 9 comments.

@nikhilNava nikhilNava enabled auto-merge (squash) January 21, 2026 19:27
@nikhilNava nikhilNava merged commit 2d24550 into main Jan 21, 2026
14 checks passed
@nikhilNava nikhilNava deleted the nikhilc/OpenAIAutoInstrumentation-fixattributes branch January 21, 2026 19:35
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.

6 participants