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

App*_*ppy 5 aop histogram kotlin spring-boot prometheus

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

le="0.005", le="0.01" , le="0.025" , le="0.05", le="0.075" ...  le="+Inf"
Run Code Online (Sandbox Code Playgroud)

喜欢:

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
Run Code Online (Sandbox Code Playgroud)

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

le="0.005", le="0.01" , le="0.025" , le="0.05", le="0.075" ...  le="+Inf"
Run Code Online (Sandbox Code Playgroud)

配置类看起来像

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

控制器有注释功能:

@Timed(description = "Get hello", histogram = true)
@GetMapping(\hello)
Run Code Online (Sandbox Code Playgroud)

build.gradle 具有以下依赖项:

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
Run Code Online (Sandbox Code Playgroud)

aSe*_*emy 2

长话短说:

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

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

您可以使用实现该方法的@Timeda来控制添加的存储桶。一些快捷方式:MeterFilterconfigure

  • 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

此外,无需@Timed在 Spring MVC 和 WebFlux 端点上使用,因为这些端点是由框架自动检测的。

一般来说,我们试图阻止对直方图桶的细粒度控制,因为经验表明,人们倾向于选择导致高误差界限百分位近似值的桶(并且真正的误差界限实际上从离散分布中是不可知的,因此他们从来没有真正了解过直方图桶)。知道)。从技术上讲 sla,选项只是添加额外的存储桶值。因此,如果您确实想要反对该建议,您可以关闭百分位数直方图并添加任意数量的 SLA 边界。这实际上产生了一组定义的桶边界的直方图。

@jkschneider

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

@jkschneider