Azure Application Insights Kusto 如何基于初始查询结果单步拉取关联ID全量日志
实现方案
方案1:标量变量法(最简洁)
利用Kusto的let关键字先把关联的ID2存为标量变量,再做全量过滤即可,单条语句即可完成需求:
// 定义要查询的目标ID1 let targetID1 = "123"; // 提取和目标ID1绑定的唯一ID2 let linkedID2 = toscalar( traces | where tostring(customDimensions.ID1) == targetID1 | where isnotempty(tostring(customDimensions.ID2)) | project ID2 = tostring(customDimensions.ID2) | limit 1 ); // 一次查询所有匹配的日志 traces | where tostring(customDimensions.ID1) == targetID1 or tostring(customDimensions.ID2) == linkedID2 | project ID1 = tostring(customDimensions.ID1), ID2 = tostring(customDimensions.ID2), message
逻辑说明
toscalar()会将单行单列的查询结果转换为标量值,刚好适配「一个ID1对应唯一非空ID2」的前提- 全程仅扫描两次traces表,性能开销和分两次查询几乎一致,无额外成本
方案2:Join实现(兼容多ID关联扩展)
如果后续需要扩展为一个ID1对应多个关联ID的场景,可以用半连接实现,无需修改核心逻辑:
let targetID1 = "123"; // 先拿到所有关联的ID映射 let idMapping = traces | where tostring(customDimensions.ID1) == targetID1 | where isnotempty(tostring(customDimensions.ID2)) | project ID2 = tostring(customDimensions.ID2) | distinct ID2; // 用半连接匹配所有符合条件的记录 traces | where tostring(customDimensions.ID1) == targetID1 or tostring(customDimensions.ID2) in (idMapping) | project ID1 = tostring(customDimensions.ID1), ID2 = tostring(customDimensions.ID2), message
内容的提问来源于stack exchange,提问作者Dudeman3000
相关产品推荐
相关产品推荐

