[AI Projects] AIProjectInstrumentor: non-streamed responses.create raises AttributeError ('NonRecordingSpan' object has no attribute 'attributes') when the span is not sampled

未关闭 适合新手
#49,854 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
observability

调研方向

从 azure/ai/projects/telemetry/_responses_instrumentor.py 中的 _append_to_message_attribute 开始,将其对 span 的处理与 _set_span_attribute_safe 和流式包装器进行比较。使用 OpenTelemetry ALWAYS_OFF 采样器和非流式 responses.create 调用复现问题。完成标准是:成功收到响应后,未记录的 span 不再引发异常,且响应仍可正常工作。

由索引模型根据 Issue 内容生成。

描述

AI Projects Client customer-reported question

Package: azure-ai-projects 2.10.0. Also seen on 2.9.0, and the helper is unchanged on main as of 2026-11-05.
Also installed: openai 3.35.1 to 3.46.0, opentelemetry-sdk 1.40.0, azure-core-tracing-opentelemetry 1.0.0b13, Python 3.12.

What happens

With AIProjectInstrumentor enabled, a non-streamed responses.create raises whenever the current span is not recording, for example when a sampler drops the trace:

AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'

It raises after the service has answered with HTTP 200, so the caller loses a completed and billed response. Streamed calls are not affected.

Cause

In azure/ai/projects/telemetry/_responses_instrumentor.py (line 681 in 2.10.0), _append_to_message_attribute reads span.span_instance.attributes without checking span.span_instance.is_recording(). OpenTelemetry's NonRecordingSpan has no attributes. The neighbouring helpers, such as _set_span_attribute_safe and the stream wrappers, already guard with is_recording().

_responses_instrumentor.py:4777 inner -> self.trace_responses_create(...)
_responses_instrumentor.py:2248 trace_responses_create -> self._add_message_event(...)
_responses_instrumentor.py:765  _add_message_event -> self._append_to_message_attribute(...)
_responses_instrumentor.py:681  _append_to_message_attribute
    existing_value = span.span_instance.attributes.get(attribute_name) if span.span_instance.attributes else None
AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'

Repro (no network)

pip install azure-ai-projects==2.10.0 opentelemetry-sdk==1.40.0 azure-core-tracing-opentelemetry==1.0.0b13

import os

import httpx2
import openai
from azure.ai.projects.telemetry import AIProjectInstrumentor
from azure.core.settings import settings
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF

# Every span non-recording, as when a sampler drops the trace.
trace.set_tracer_provider(TracerProvider(sampler=ALWAYS_OFF))
settings.tracing_implementation = "opentelemetry"
os.environ["AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING"] = "true"
AIProjectInstrumentor().instrument()

RESPONSE = {
    "id": "resp_1", "object": "response", "created_at": 0, "status": "completed", "model": "m",
    "output": [{"type": "message", "id": "msg_1", "status": "completed", "role": "assistant",
                "content": [{"type": "output_text", "text": "done", "annotations": []}]}],
    "parallel_tool_calls": True, "tool_choice": "auto", "tools": [],
}
calls = []
def handler(request):
    calls.append(request.url)
    return httpx2.Response(200, json=RESPONSE)

client = openai.OpenAI(api_key="test", base_url="https://example.invalid/v1",
                       http_client=httpx2.Client(transport=httpx2.MockTransport(handler)))
try:
    client.responses.create(input="hi")
    print("no error")
except AttributeError as exc:
    print(f"HTTP calls: {len(calls)}; then: AttributeError: {exc}")

Output:

HTTP calls: 1; then: AttributeError: 'NonRecordingSpan' object has no attribute 'attributes'

Why it matters in practice

When OTEL_TRACES_SAMPLER is unset, azure-monitor-opentelemetry 1.8.8 defaults to a rate-limited sampler at 5 traces/s. In an ordinary App Insights setup, some of these spans are therefore not recording, and production calls fail intermittently after the model has already run.

Expected

Telemetry should never change the outcome of the call. When the span is not recording, skip the attribute aggregation, as the other helpers already do.

Workaround

Leave AIProjectInstrumentor off (AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING unset), or stream the call.

主要语言
Python
星标
5.6k
派生
3.4k
平均合并
2 天 19 小时
30 天内合并 PR
183

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Azure/azure-sdk-for-python 的其他 Issue

查看 Azure/azure-sdk-for-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。