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

OpenTelemetry Collector attributeprocessor无法从嵌套属性插入属性

从journald嵌套body中提取属性到OpenTelemetry Collector标签的问题解决

问题描述

尝试使用OpenTelemetry Collector的attributeprocessor,从journald接收器返回的嵌套在body元素下的_HOSTNAME属性提取值,插入为新的hostname属性,使其出现在JSON输出并作为Grafana Loki的标签使用。但配置后功能未生效,启用调试日志也无法查看规则处理过程,尝试body__hostname、body._HOSTNAME、_HOSTNAME等多种from_attribute取值均无效。Grafana中显示日志的_HOSTNAME字段嵌套在body元素内。

原配置

receivers:
  journald:

processors:
  resource:
    attributes:
    - action: insert
      key: loki.format
      value: json
    - action: insert
      key: environment
      value: dev
    - action: insert
      key: loki.attribute.labels
      value: hostname, priority
  attributes:
    actions:
      - action: insert
        key: hostname
        from_attribute: _HOSTNAME
      - action: insert
        key: priority
        from_attribute: PRIORITY

exporters:
  loki:
    endpoint: xxxx
    sending_queue:
      num_consumers: 10
      queue_size: 50000
    headers:
      "X-Scope-OrgID": "xxxx"
    tls:
      ca_file: xxxx
      cert_file: xxxx
      key_file: xxxx

service:
  pipelines:
    logs:
      receivers: [journald]
      processors: [attributes,resource]
      exporters: [loki]

解决办法

核心问题

  1. attributeprocessor默认仅处理resource属性,无法直接访问日志记录(log_record)中的嵌套body字段,需要显式配置上下文为log_record。
  2. 嵌套属性的正确引用语法为body._HOSTNAME,但必须开启对应上下文权限。
  3. 处理器顺序错误:resource处理器依赖attributes生成的hostname字段,因此需要调整顺序确保标签生成时属性已存在。

修改后的完整配置

receivers:
  journald:

processors:
  resource:
    attributes:
    - action: insert
      key: loki.format
      value: json
    - action: insert
      key: environment
      value: dev
    - action: insert
      key: loki.attribute.labels
      value: hostname, priority
  attributes:
    # 指定处理日志记录(log_record)中的属性,包括body嵌套内容
    context: log_record
    actions:
      - action: insert
        key: hostname
        from_attribute: body._HOSTNAME
      - action: insert
        key: priority
        from_attribute: body.PRIORITY

exporters:
  loki:
    endpoint: xxxx
    sending_queue:
      num_consumers: 10
      queue_size: 50000
    headers:
      "X-Scope-OrgID": "xxxx"
    tls:
      ca_file: xxxx
      cert_file: xxxx
      key_file: xxxx

service:
  pipelines:
    logs:
      receivers: [journald]
      processors: [resource, attributes]  # 调整处理器顺序,确保标签配置先生效
      exporters: [loki]
  # 可选:开启调试日志排查问题
  telemetry:
    logs:
      level: debug

验证步骤

  • 重启OpenTelemetry Collector,应用新配置。
  • 在Grafana中查看日志,确认hostname标签已生成,且JSON输出中包含该字段。
  • 若仍有问题,查看Collector调试日志,搜索attributeprocessor相关条目,确认是否成功读取body._HOSTNAME的值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 13:47:33