Springboot 2 micrometer-registry-prometheus 如何创建自己的直方图桶

问题描述 投票:0回答:2

嗨,我试图创建直方图存储桶,但在 Springboot 2 中是否可以创建自定义存储桶或等间隔存储桶,如下所示

le="0.005", le="0.01" , le="0.025" , le="0.05", le="0.075" ...  le="+Inf"

喜欢:

XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="0.005"**,} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.01",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.025",} 1.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.05",} 1.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.075",} 1.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.1",} 2.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.25",} 2.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.5",} 2.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.75",} 2.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="1.0"**,} 2.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="2.5"**,} 2.0

/actuator/prometheus 当前的输出看起来像:

XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="0.001048576**",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="0.001398101**",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="0.001747626**",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="0.002097151**",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",**le="0.002446676**",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.002796201",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.003145726",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.003495251",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.003844776",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.004194304",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.005592405",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.006990506",} 0.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="0.008388607",} 0.0
:
cket{APP="XYZ",class="HelloController",exception="none",method="hello",le="22.906492245",} 1.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="28.633115306",} 1.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="30.0",} 1.0
XYZ_seconds_bucket{APP="XYZ",class="HelloController",exception="none",method="hello",le="+Inf",} 1.0
XYZ_seconds_count{APP="XYZ",class="HelloController",exception="none",method="hello",} 1.0

配置类看起来像

@Configuration
@EnableAutoConfiguration
@ComponentScan
class TimingConfiguration {
  @Bean
  fun timedAspect(registry: MeterRegistry?): TimedAspect {
    return TimedAspect(registry!!)
  }
}

控制器有注释功能:

@Timed(description = "Get hello", histogram = true)
@GetMapping(\hello)

build.gradle 具有以下依赖项:

implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
implementation("org.springframework.boot:spring-boot-starter-aop3")
spring-boot kotlin histogram aop prometheus
2个回答
2
投票

tl;博士:

  • 关闭百分位数直方图
  • 添加任意数量的 SLA 边界

请参阅此(已关闭)问题:https://github.com/micrometer-metrics/micrometer/issues/1947

您可以使用实现

@Timed
方法的
MeterFilter
来控制由
configure
添加的存储桶。一些快捷方式:

  • MeterFilter.maxExpected("http.server.requests", Duration.ofSeconds(1))
  • MeterFilter.minExpected("http.server.reqests", Duration.ofMillis(10))

MeterFilter
实现只需在 Spring 应用程序中连接为
@Bean
即可生效。

更快捷的是,Spring boot 具有用于设置最小和最大期望值的属性驱动配置:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#per-meter-properties

此外,无需在 Spring MVC 和 WebFlux 端点上使用

@Timed
,因为这些端点是由框架自动检测的。

一般来说,我们试图阻止对直方图桶的细粒度控制,因为经验表明,人们倾向于选择导致高误差界限百分位数近似值的桶(并且真正的误差界限实际上从离散分布中是不可知的,因此他们永远不会真正了解)。 技术上

sla
选项只是添加额外的桶值。因此,如果您确实想要反对该建议,您可以关闭百分位数直方图并添加任意数量的 SLA 边界。这实际上产生了一组定义的存储桶边界的直方图。

@jkschneider

[设置存储桶数量的诱惑]实际上正是我们删除(明显的)可配置性的原因。说“嘿,现在发布了 100 个存储桶,我们可以将其减少到 50 个存储桶吗?”这句话太容易了。但对百分位数近似的影响是未知的。缩小最小/最大(前提是您的计时不超出该范围)不会影响近似值的准确性。

@jkschneider


0
投票
# disable default percentilesHistograms
management.metrics.distribution.ph.http.server.requests=false
# set custom ServiceLevelObjectiveBoundaries
management.metrics.distribution.slo.http.server.requests=300ms,500ms,1s,2s,5s,10s,15s,20s,30sis

提示:http.server.requests是计时器指标的名称

© www.soinside.com 2019 - 2024. All rights reserved.