【Azure APIM】APIM的诊断日志与Application Insights的日志是否可以串联为一个端到端的日志链路呢?

问题描述

使用 Azure API Management(APIM)作为统一 API 网关,希望基于诊断日志(AzureDiagnostics表)中的日志数据,分析不同产品和服务之间的调用关系与性能指标。

**核心问题是:**如果注册在 APIM 上的所有 API 以及后端服务均已集成 Application Insights,是否可以通过 AzureDiagnostics 表中的 CorrelationId 字段作为 TraceId,将一次请求经过的所有服务串联起来,构建完整的端到端调用链路?

问题解答

可以,但CorrelationId本身不是最终的 TraceId,而是进入 Application Insights 调用链的入口。

真正能跨服务统一追踪的标识是 Application Insights 中的 operation_Id,两者之间存在一条固定的映射路径:

复制代码
AzureDiagnostics.CorrelationId
        ↓
Application Insights requests 表 customDimensions["Request-Id"](值与 CorrelationId 相同)
        ↓
requests.operation_Id
        ↓
所有后端服务的 requests / dependencies / traces / exceptions

第一步:通过 CorrelationId 找到 operation_Id

复制代码
AzureDiagnostics
| where ResourceType == "APIMANAGEMENT"
| where Category == "GatewayLogs"
| project TimeGenerated, CorrelationId, ApiId, ResponseCode, DurationMs
| join kind=inner (
    app('<your-appinsights-name>').requests
    | extend reqId = tostring(customDimensions["Request-Id"])
    | project reqId, operation_Id, duration, resultCode
) on $left.CorrelationId == $right.reqId
| project TimeGenerated, CorrelationId, operation_Id, ApiId, ResponseCode, DurationMs

第二步:用 operation_Id 拉取完整调用链

复制代码
let targetCorrelationId = "<填入你的 CorrelationId>";
let opId = app('<your-appinsights-name>').requests
    | extend reqId = tostring(customDimensions["Request-Id"])
    | where reqId == targetCorrelationId
    | project operation_Id
    | take 1;
union
    app('<your-appinsights-name>').requests,
    app('<your-appinsights-name>').dependencies,
    app('<your-appinsights-name>').exceptions,
    app('<your-appinsights-name>').traces
| where operation_Id in (opId)
| project timestamp, itemType, cloud_RoleName, name, duration, success, operation_ParentId
| order by timestamp asc

注意:多个 Application Insights 实例时需要跨资源查询

若 APIM 与各后端服务使用不同的 Application Insights 实例,需在 union 中同时引用多个 app() 实例,否则即使 operation_Id 一致,跨实例的 Telemetry 数据也无法被查询到:

复制代码
union
    app('apim-appinsights').requests,
    app('webapp-appinsights').requests,
    app('funcapp-appinsights').dependencies
| where operation_Id == "<your-operation-id>"
| order by timestamp asc

参考资料

如何将 Azure API 管理与 Azure 应用程序 Insights 集成 :https://learn.microsoft.com/zh-cn/azure/api-management/api-management-howto-app-insights?tabs=rest

应用程序映射:分类整理分布式应用程序 :https://learn.microsoft.com/zh-cn/azure/azure-monitor/app/app-map

AzureDiagnostics :https://learn.microsoft.com/zh-cn/azure/azure-monitor/reference/tables/azurediagnostics

在 Azure Monitor 中跨 Log Analytics 工作区、应用程序和资源查询数据 :https://learn.microsoft.com/zh-cn/azure/azure-monitor/logs/cross-workspace-query

相关推荐
代码山河5 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
在世修行5 小时前
智学文枢:题目流水线
python·多线程·状态机·pyside
梯度下降者6 小时前
零基础入门Python 爬虫
爬虫·python·数据分析·入门指南·webcrawler
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
benchmark_cc8 小时前
用 Python 拉 A 股全市场实时快照:Notebook 请求、结果校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
伞伞悦读8 小时前
【第70期】Adam二阶矩不是方差:偏差校正、动量与AdamW完整实验
python
外收内放9 小时前
Python基础语法练习题(73)
java·前端·python
锋行天下9 小时前
Langgraph HITL 中断运行,用户输入后输出打字效果
人工智能·python
时早玖沐9 小时前
给 AstrBot 写一个天气推送插件:12 次提交、6 个坑
python·开源·ai编程
触底反弹9 小时前
🚀 FastAPI 五大重难点深度剖析:从 500 报错到彻底搞懂 Depends、Pydantic 与 SQLAlchemy
python·fastapi