【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

相关推荐
全栈练习生4 小时前
AI Agent 沙箱
python·ai
智码看视界5 小时前
Edge AI 全栈实战 ⑤:云边协同收尾——MQTT 桥接 + 云端看板管 N 台边缘设备
mqtt·flask·iot·全栈开发·云边协同·edgeai
SEO_juper5 小时前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
用户8356290780516 小时前
使用 Python 对 Excel 工作表进行排序
后端·python
for_ever_love__7 小时前
MySQL 全文索引实战:FULLTEXT、ngram 中文分词与 MATCH AGAINST 到底该怎么用
java·python·mysql·全文检索·分词·索引·ngram
用户8356290780517 小时前
使用 Python 合并 PowerPoint 演示文稿
后端·python
weixin_461769407 小时前
VS Code 中创建 Jupyter 文件(.ipynb)
ide·python·jupyter
2601_966949657 小时前
多市场量化策略的数据接口应该如何设计:从数据层架构到策略接入
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
全栈弄潮儿7 小时前
Python实战第1期:Python环境搭建与第一个程序
python
心易行者7 小时前
Agent应用+API端点商业化进阶实战:从单体智能体到可付费调用的API全流程
运维·服务器·人工智能·python·apache