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

如何在Spring Boot Actuator Prometheus指标中添加时间戳

解决方案

Spring Boot Actuator整合Prometheus默认关闭指标时间戳输出,可通过以下两种方式开启:

方式一:通过配置文件直接开启(适配Spring Boot 2.5.4版本)

直接在你的application.yaml中添加以下配置即可:

management:
  metrics:
    export:
      prometheus:
        enabled: true
        # 开启指标时间戳输出
        timestamps:
          enabled: true

方式二:自定义Prometheus注册器配置(适配所有兼容版本)

如果配置项不生效,可通过自定义Bean覆盖默认注册器配置,新增如下配置类即可:

import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.micrometer.prometheus.PrometheusConfig;
import io.prometheus.client.CollectorRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PrometheusMetricsConfig {

    @Bean
    public PrometheusMeterRegistry prometheusMeterRegistry(CollectorRegistry collectorRegistry) {
        PrometheusConfig config = new PrometheusConfig() {
            @Override
            public String get(String key) {
                return null;
            }

            @Override
            public boolean timestampsEnabled() {
                // 开启时间戳输出
                return true;
            }
        };
        return PrometheusMeterRegistry.builder(config)
                .collectorRegistry(collectorRegistry)
                .build();
    }
}

效果验证

配置完成后重启应用,访问/actuator/prometheus端点,即可看到所有指标末尾已经追加了毫秒级Unix时间戳,示例如下:

# HELP jvm_threads_live_threads The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live_threads gauge
jvm_threads_live_threads 28.0 1725001234000

注意事项

Prometheus默认拉取指标时会使用自身拉取时刻的时间戳覆盖指标自带的时间戳,仅当你使用Pushgateway推送指标、或者有明确的指标时间戳保留需求时才建议开启该配置,避免因应用节点时间同步问题导致指标数据异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 00:06:02