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

Spring Boot+Micrometer:micrometer-registry-prometheus不显示Exemplars

实现Spring Boot + Micrometer 指标关联Exemplars(链路追踪跳转)

1. 开启核心配置

在application.properties或application.yml中添加以下配置,启用Exemplars并关联追踪:

# 暴露Actuator端点
management.endpoints.web.exposure.include=prometheus,health
management.endpoint.prometheus.enabled=true

# 开启Micrometer Prometheus Exemplars
management.metrics.export.prometheus.exemplars.enabled=true
management.metrics.export.prometheus.exemplars.tracing-enabled=true

# 配置追踪采样率(测试用全采样,生产按需调整)
management.tracing.sampling.probability=1.0
# 配置OTel Collector地址(替换为你的实际地址)
management.tracing.otel.exporter.otlp.endpoint=http://your-otel-collector:4318/v1/traces

2. 确认依赖完整性

你的现有依赖已覆盖核心需求,Spring Boot 3.4.5已自动对齐Micrometer、OpenTelemetry的兼容版本,无需额外调整:

  • spring-boot-starter-web:Web服务基础
  • spring-boot-starter-actuator:指标暴露端点
  • micrometer-registry-prometheus:Prometheus指标导出
  • micrometer-tracing-bridge-otel:桥接Micrometer与OpenTelemetry追踪
  • opentelemetry-exporter-otlp:OTLP格式追踪导出

3. 自定义PrometheusMeterRegistry(可选,自动配置失效时用)

如果自动配置未生效,可手动创建Registry并绑定Tracer:

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.micrometer.tracing.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MetricsConfig {

    @Bean
    public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig config, Tracer tracer) {
        PrometheusMeterRegistry registry = new PrometheusMeterRegistry(config);
        // 绑定Tracer以生成关联Span的Exemplars
        registry.config().exemplarSampler(() -> tracer::currentSpan);
        return registry;
    }
}

4. 验证Exemplars生成

访问Actuator的Prometheus端点:http://localhost:8080/actuator/prometheus,查看指标是否包含Exemplar注释,示例如下:

http_server_requests_seconds_sum{method="GET",uri="/io_task",status="200",} 1.000345678 # {span_id="abc123",trace_id="def456"} 1620000000000

若出现# {span_id="xxx",trace_id="xxx"}格式内容,说明Exemplars已成功生成。

5. OTel Collector配置调整

确保Collector正确处理并传递Exemplars,示例配置片段:

receivers:
  prometheus:
    config:
      scrape_configs:
        - job_name: 'spring-boot-app'
          scrape_interval: 15s
          static_configs:
            - targets: ['your-app:8080']
          metrics_path: '/actuator/prometheus'
          enable_exemplars: true # 开启Exemplars接收

exporters:
  prometheus:
    endpoint: "prometheus:9090"
    enable_open_metrics: true # 启用OpenMetrics以支持Exemplars
  otlp:
    endpoint: "tempo:4317"
    tls:
      insecure: true

service:
  pipelines:
    metrics:
      receivers: [prometheus]
      exporters: [prometheus]
    traces:
      receivers: [otlp]
      exporters: [otlp]

6. Grafana关联配置

在Grafana中完成以下设置实现指标-链路跳转:

  1. 进入Prometheus数据源配置页面
  2. 找到「Exemplars」板块,勾选「Enable」
  3. 选择你的Tempo数据源作为关联的Trace数据源

常见排查点

  • 若Actuator无Exemplars:检查management.metrics.export.prometheus.exemplars.enabled=true是否配置正确,代码中tracer.currentSpan()是否返回非空值
  • 若Grafana看不到Exemplars:确认Prometheus数据源已开启Exemplars支持,Collector是否正确传递Exemplars到Prometheus
  • 采样率问题:测试阶段将management.tracing.sampling.probability设为1.0,确保生成足够的关联Span

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 04:52:33