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

基于Micrometer/Spring Boot,如何将请求头值作为Span属性自动添加?

在Spring Boot/Micrometer中配置OTLP Agent捕获响应头为Span属性

首先明确你提到的配置属性中文对应:otel.instrumentation.http.server.capture-response-headers(OTLP HTTP服务端埋点捕获响应头)

以下是具体实现步骤:

1. 引入必要依赖

确保Spring Boot项目已添加Micrometer链路追踪和OTLP相关依赖:

  • Maven(pom.xml):
<!-- Micrometer Tracing 核心依赖 -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing</artifactId>
</dependency>
<!-- 桥接OpenTelemetry -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<!-- OTLP 导出器 -->
<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
  • Gradle(build.gradle):
implementation 'io.micrometer:micrometer-tracing'
implementation 'io.micrometer:micrometer-tracing-bridge-otel'
implementation 'io.opentelemetry:opentelemetry-exporter-otlp'

2. 配置OTLP Agent属性

在Spring Boot配置文件中添加对应配置,指定要捕获的响应头:

  • 若用application.properties:
# 逗号分隔多个要捕获的响应头名称
otel.instrumentation.http.server.capture-response-headers=X-Request-ID,User-Agent
# 配置OTLP Agent的地址(根据实际部署调整)
otel.exporter.otlp.endpoint=http://localhost:4317
  • 若用application.yml:
otel:
  instrumentation:
    http:
      server:
        capture-response-headers: X-Request-ID,User-Agent
  exporter:
    otlp:
      endpoint: http://localhost:4317

3. 验证功能

启动应用后,发起请求并让接口返回配置中指定的响应头,OTLP Agent会自动将这些响应头的键值对作为属性附加到HTTP服务端Span中。你可以通过Jaeger、Zipkin等链路追踪工具查看Span详情,确认属性已添加。

补充:如果需要捕获请求头而非响应头,替换配置属性为otel.instrumentation.http.server.capture-request-headers即可,用法完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:52:07