Grafana Loki与Tempo traceId关联配置后无法自动搜索追踪
Grafana Loki与Tempo日志-追踪关联自动搜索失效问题
问题
已部署Grafana+Loki+Tempo,要实现日志与对应traceId的追踪关联。由于traceId是作为独立结构化元数据发送至Loki,所以采用关联配置(无法用衍生字段实现)。通过Grafana UI配置的关联功能完全正常,但用配置文件复刻后,点击日志中的“Trace”链接虽能打开Tempo窗口并传入正确traceId,却不会自动触发搜索——必须手动点击“Search”或“TraceQL”按钮才能看到追踪内容。当前配置如下:
correlations: - targetUID: tempo label: "Trace" description: "Show traces related to this log in Tempo" config: type: "query" target: queryType: "traceId" query: $${trace_id} field: Line transformations: - type: regex field: labels expression: '"trace_id":"(\w+)"' mapValue: trace_id
修复方案
核心问题在于配置的字段指向和变量引用逻辑有误,分两种场景修复:
场景1:traceId是Loki的结构化标签(符合你的描述)
既然traceId是独立的结构化元数据,直接关联标签字段即可,无需正则提取:
- 将
field: Line改为*field: labels.trace_id*,直接引用已有的标签字段 - 删除多余的
transformations区块(不需要从内容里提取traceId) - 将
query: $${trace_id}改为*query: "${__value}"*,使用Grafana标准变量获取匹配值
修改后的配置:
correlations: - targetUID: tempo label: "Trace" description: "Show traces related to this log in Tempo" config: type: "query" target: queryType: "traceId" query: "${__value}" field: labels.trace_id
场景2:traceId仅存在于日志行内容中(备用方案)
如果traceId不在标签里而是在日志行中,需要修正正则表达式的转义问题(原配置里的"是HTML转义,YAML中无需这么写):
- 将
field: labels改为*field: Line*,指定从日志行提取 - 把正则表达式改为
*expression: '"trace_id":"(\w+)"'*,直接用双引号(或单引号包裹表达式避免转义) - 变量引用改为
*query: "${trace_id}"*
配置示例:
correlations: - targetUID: tempo label: "Trace" description: "Show traces related to this log in Tempo" config: type: "query" target: queryType: "traceId" query: "${trace_id}" field: Line transformations: - type: regex field: Line expression: '"trace_id":"(\w+)"' mapValue: trace_id
调整后点击关联链接,Tempo会自动执行traceId搜索并展示追踪内容。
内容的提问来源于stack exchange,提问作者Dzamal
相关产品推荐
相关产品推荐

