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

无法将OTLP资源属性映射至Loki标签的技术求助

问题:Loki标签未解析OTLP资源/属性实际值,显示为属性名

我使用Spring Boot应用,通过OpenTelemetry Agent将包含日志的遥测数据发送至OpenTelemetry Collector。日志已成功传输至Collector并被Grafana Loki接收,但Loki标签未更新为OTLP资源及日志属性的实际值。预期Loki标签host_name对应host.name的实际值(如06605506424f)或service.name等属性,但实际显示为host.name属性名。

OpenTelemetry Collector配置文件

receivers:
  otlp:
    protocols:
      grpc:
      http:

processors:  
  attributes:
    actions:
    - action: insert
      key: loki.attribute.labels
      value: host.name
  
  resource:
    attributes:
    - action: insert
      key: loki.attribute.labels
      value: service.name
    - action: insert
      key: loki.resource.labels
      value: host.name, container.id  
      
  batch:

exporters:
  jaeger:
    endpoint: "jaeger:14250"
    tls:
      insecure: true
  logging:
    loglevel: debug    
   
  loki:
    endpoint: "http://loki:3100/loki/api/v1/push"
    
  prometheus:
    endpoint: "0.0.0.0:8889"
    namespace: "default"    

extensions:
  health_check:
  pprof:
  zpages:

service:
  extensions: [health_check,pprof,zpages]
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [jaeger]
    metrics:
      receivers: [otlp]
      processors: [batch]
      exporters: [prometheus]
    logs:
      receivers: [otlp]
      processors: [resource,attributes,batch]
      exporters: [logging,loki] 

OpenTelemetry Collector日志

Resource SchemaURL: https://opentelemetry.io/schemas/1.12.0
Resource attributes:
     -> container.id: STRING(06605506424f3396e1c92aeedc22337288177bc25c97af21beeda42a898e9be4)
     -> host.arch: STRING(amd64)
     -> host.name: STRING(06605506424f)
     -> os.description: STRING(Linux 5.10.16.3-microsoft-standard-WSL2)
     -> os.type: STRING(linux)
     -> process.command_line: STRING(/usr/lib/jvm/java-1.8-openjdk/jre:bin:java -javaagent:/dir/opentelagent.jar)
     -> process.executable.path: STRING(/usr/lib/jvm/java-1.8-openjdk/jre:bin:java)
     -> process.pid: INT(1)
     -> process.runtime.description: STRING(IcedTea OpenJDK 64-Bit Server VM 25.212-b04)
     -> process.runtime.name: STRING(OpenJDK Runtime Environment)
     -> process.runtime.version: STRING(1.8.0_212-b04)
     -> service.name: STRING(spring-api-gateway)
     -> telemetry.auto.version: STRING(1.18.0)
     -> telemetry.sdk.language: STRING(java)
     -> telemetry.sdk.name: STRING(opentelemetry)
     -> telemetry.sdk.version: STRING(1.18.0)
     -> loki.attribute.labels: STRING(service.name)
     -> loki.resource.labels: STRING(host.name, container.id)
ScopeLogs #0
ScopeLogs SchemaURL: 
InstrumentationScope com.apigateway.SpringApiGatewayApplication 
LogRecord #0
ObservedTimestamp: 1970-01-01 00:00:00 +0000 UTC
Timestamp: 2022-10-04 10:02:10.386 +0000 UTC
SeverityText: INFO
SeverityNumber: SEVERITY_NUMBER_INFO(9)
Body: Started SpringApiGatewayApplication in 10.038 seconds (JVM running for 16.182)
Attributes:
     -> loki.attribute.labels: STRING(host.name)
Trace ID: 
Span ID: 
Flags: 0
        {"kind": "exporter", "data_type": "logs", "name": "logging"}

Grafana Loki截图

Loki标签显示异常截图

解决方案

问题根源

之前的配置错误地使用resource和attributes处理器将属性名(如host.name)作为字符串值插入到loki.resource.labels和loki.attribute.labels中,而非配置标签映射规则。Loki exporter需要明确的属性到标签的映射关系,才能将OTLP资源/属性的实际值转为Loki标签。

修正后的Collector配置

直接在Loki exporter中配置资源和属性到标签的映射,移除错误的处理器配置:

receivers:
  otlp:
    protocols:
      grpc:
      http:

processors:  
  batch:

exporters:
  jaeger:
    endpoint: "jaeger:14250"
    tls:
      insecure: true
  logging:
    loglevel: debug    
   
  loki:
    endpoint: "http://loki:3100/loki/api/v1/push"
    # 将OTLP资源属性映射为Loki标签
    resource_to_labels:
      host.name: host_name
      service.name: service_name
      container.id: container_id
    # 若需将日志自身属性映射为Loki标签,可在此配置
    # attributes_to_labels:
    #   request_id: request_id

  prometheus:
    endpoint: "0.0.0.0:8889"
    namespace: "default"    

extensions:
  health_check:
  pprof:
  zpages:

service:
  extensions: [health_check,pprof,zpages]
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [jaeger]
    metrics:
      receivers: [otlp]
      processors: [batch]
      exporters: [prometheus]
    logs:
      receivers: [otlp]
      processors: [batch]
      exporters: [logging,loki] 

配置说明

  • resource_to_labels:定义OTLP资源属性与Loki标签的映射,比如将host.name映射为host_name,Loki会自动替换为该属性的实际值(如06605506424f)。
  • attributes_to_labels:可选配置,用于将日志自身携带的属性(非资源属性)映射为Loki标签。
  • 移除了原配置中错误的attributes和resource处理器,避免插入无效的字符串值干扰Loki exporter的解析。

验证步骤

  1. 重启OpenTelemetry Collector,加载新配置。
  2. 查看Collector的日志,确认资源属性被正确识别。
  3. 在Grafana中查看Loki日志,标签将显示对应属性的实际值(如host_name=06605506424f)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 07:50:29