You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法迭代Azure ItemPaged对象及SDK相关问题咨询

问题解决方案

一、WebTestsOperations 正确参数传递与实例化

azure-mgmt-applicationinsights 4.0.0 中,不要直接实例化WebTestsOperations类,这类操作类依赖管理客户端的内部配置(如HTTP管道、认证信息等),直接实例化会缺失必要参数,导致迭代ItemPaged时触发AttributeError。正确方式是通过ApplicationInsightsManagementClient获取操作实例:

代码示例

from azure.identity import DefaultAzureCredential
from azure.mgmt.applicationinsights import ApplicationInsightsManagementClient

# 初始化管理客户端
credential = DefaultAzureCredential()
subscription_id = "你的订阅ID"
client = ApplicationInsightsManagementClient(credential, subscription_id)

# 获取WebTestsOperations实例(无需手动传参)
web_tests_ops = client.web_tests

# 调用方法并迭代结果(以list_by_resource_group为例)
for web_test in web_tests_ops.list_by_resource_group("你的资源组名称"):
    print(web_test.name)

如果必须手动构造(不推荐),需要传入完整的_config和PipelineClient实例,但这种方式极易出错,官方完全不建议这么做。


二、获取Application Insights失败请求的响应体

1. 通过SDK实现

使用azure-monitor-query包(需单独安装:pip install azure-monitor-query),通过日志查询获取失败请求的响应体:

from azure.identity import DefaultAzureCredential
from azure.monitor.query import LogsQueryClient
from datetime import timedelta

client = LogsQueryClient(DefaultAzureCredential())
workspace_id = "你的工作区ID"
app_insights_resource_id = "/subscriptions/{订阅ID}/resourceGroups/{资源组}/providers/microsoft.insights/components/{AI实例名称}"

# Kusto查询语句:筛选失败请求,提取响应体
query = f"""
requests
| where success == false
| where _ResourceId == "{app_insights_resource_id}"
| where timestamp > ago(1h)  # 近1小时内的数据
| project timestamp, name, responseBody, resultCode
"""

response = client.query_workspace(workspace_id, query, timespan=timedelta(hours=1))

for table in response.tables:
    for row in table.rows:
        print(f"时间: {row[0]}, 请求名: {row[1]}, 响应体: {row[2]}")

2. 通过REST API实现

调用Azure Monitor Logs查询API,步骤如下:

  • 获取Azure AD访问令牌(针对https://api.loganalytics.io资源)
  • 发送POST请求到:https://api.loganalytics.io/v1/workspaces/{workspace-id}/query
  • 请求体示例:
{
    "query": "requests | where success == false | where _ResourceId == \"{AI实例资源ID}\" | project timestamp, responseBody",
    "timespan": "PT1H"
}
  • 响应结果中,tables[0].rows包含查询到的失败请求数据,对应responseBody的列即为响应体内容。

内容的提问来源于stack exchange,提问作者VINAY TALLA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 08:02:43