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

Spring Boot Prometheus指标缺失状态码及请求自动采集问题

问题解答

1. 为@Timed指标添加HTTP状态码标签

你当前用@Timed生成的指标默认仅包含class、method、exception标签,要添动态HTTP状态码标签,需通过自定义AOP切面实现——因为状态码是方法执行后的动态结果。

实现步骤

创建AOP切面类,拦截带@Timed注解的方法,在方法执行前后提取状态码并作为标签注入Micrometer Timer:

import io.micrometer.core.annotation.Timed
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Timer
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Component

@Aspect
@Component
class TimedWithStatusAspect(private val meterRegistry: MeterRegistry) {

    @Around("@annotation(timed)")
    fun aroundTimedMethod(pjp: ProceedingJoinPoint, timed: Timed): Any? {
        val sample = Timer.start(meterRegistry)
        return try {
            val result = pjp.proceed()
            // 从ResponseEntity提取状态码,非ResponseEntity默认200
            val status = (result as? ResponseEntity<*>)?.statusCode?.value()?.toString() ?: "200"
            // 停止计时并注册带状态码标签的Timer
            sample.stop(
                Timer.builder(timed.value())
                    .tags(*timed.extraTags)
                    .tag("class", pjp.target.javaClass.name)
                    .tag("method", pjp.signature.name)
                    .tag("exception", "none")
                    .tag("status", status)
                    .register(meterRegistry)
            )
            result
        } catch (e: Exception) {
            // 异常场景标记状态码为500,同时记录异常类型
            sample.stop(
                Timer.builder(timed.value())
                    .tags(*timed.extraTags)
                    .tag("class", pjp.target.javaClass.name)
                    .tag("method", pjp.signature.name)
                    .tag("exception", e.javaClass.simpleName)
                    .tag("status", "500")
                    .register(meterRegistry)
            )
            throw e
        }
    }
}

修改后,你的search_something指标会新增status标签,示例如下:

search_something_seconds_count{class="org.example.TestController",exception="none",method="search",status="200"} 1.0

2. 自动捕获所有入站请求(无需手动加@Timed)

Spring Boot 2.7.x默认通过WebMvcMetricsFilter自动收集所有HTTP入站请求指标,无需为每个端点添加@Timed注解。

默认指标说明

默认生成的指标名为http_server_requests_seconds,包含核心标签:

  • status:HTTP状态码(如200、404、500)
  • method:请求方法(如GET、POST)
  • uri:请求路径
  • exception:异常类型(无异常则为none)

可选配置调整

若需自定义默认指标名称或标签,可在application.yaml中添加配置:

management:
  metrics:
    # 添加全局标签(可选)
    tags:
      application: your-app-name
    web:
      server:
        request:
          # 自定义指标名称(可选)
          metric-name: custom_http_requests
          # 确保自动计时开启(默认已开启)
          autotime:
            enabled: true

访问/actuator/prometheus即可看到自动收集的请求指标,示例如下:

http_server_requests_seconds_count{application="your-app-name",exception="none",method="GET",status="200",uri="/",} 1.0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 03:40:35