【问题标题】:gauge-like calculation of DistributionSummary with micrometer and prometheus使用 micrometer 和 prometheus 对 DistributionSummary 进行仪表式计算
【发布时间】:2019-01-14 08:23:53
【问题描述】:

我想获取一些不经常更改的域数据的 DistributionSummary。所以这不是关于监控请求或类似的东西。

我们以办公室的座位数为例。每个办公室的价值可能会不时变化,可能会出现新办公室,也可能会移除办公室。

所以现在我需要所有办公室的当前 DistributionSummary,每次我想都需要计算(类似于 Gauge)。

我有一个带有千分尺的 Spring Boot 2 应用程序,并使用 prometheus 收集指标并将它们显示在 grafana 中。

到目前为止我尝试了什么:

当我注册 DistributionSummary 时,我可以在启动期间记录所有值...这给了我分布,但是像 max 这样的计算值会随着时间的推移而丢失,我无法更新 DistributionSummary (记录新办公室会起作用,但不会改变现有办公室)

// during startup
seatsInOffice = DistributionSummary.builder("office.seats")
    .publishPercentileHistogram()
    .sla(1, 5, 20, 50)
    .register(meterRegistry);

officeService.getAllOffices().forEach(p -> seatsInOffice.record(o.getNumberOfSeats()));

我还尝试使用@Scheduled 任务来删除并完全重建 DistributionSummary。这似乎有效,但不知何故感觉不对。这是推荐的方法吗?这也可能需要一些同步来不收集删除和重新计算分布之间的指标。

@Scheduled(fixedRate = 5 * 60 * 1000)
public void recalculateMetrics() {
    if (seatsInOffice != null) {
       meterRegistry.remove(seatsInOffice);
    }
    seatsInOffice = DistributionSummary.builder("office.seats")
        .publishPercentileHistogram()
        .sla(1, 5, 20, 50)
        .register(meterRegistry);

    officeService.getAllOffices().forEach(p -> seatsInOffice.record(o.getNumberOfSeats()));
}

我刚刚认识到这种方法的另一个问题:/actuator/prometheus 端点仍然返回旧(已删除)指标的值,因此所有内容都存在多次。

对于 sla 边界之类的东西,我还可以使用一些量规来提供值(通过自己计算),但这不会给我分位数。是否可以在不注册的情况下创建一个新的 DistributionSummary 并仅提供它以某种方式收集的值?

meterRegistry.gauge("office.seats", Tags.of("le", "1"), officeService,
    x -> x.getAllOfficesWithLessThanXSeats(1).size());
meterRegistry.gauge("office.seats", Tags.of("le", "5"), officeService,
    x -> x.getAllOfficesWithLessThanXSeats(5).size());
meterRegistry.gauge("office.seats", Tags.of("le", "20"), officeService,
    x -> x.getAllOfficesWithLessThanXSeats(20).size());
meterRegistry.gauge("office.seats", Tags.of("le", "50"), officeService,
    x -> x.getAllOfficesWithLessThanXSeats(50).size());

我想要一个 DistributionSummary,它需要一个 lambda 或类似的东西来获取值。但也许这些工具不是为这个用例制作的,我应该使用其他东西。可以推荐一下吗?

【问题讨论】:

  • 您的仪表方法看起来不错。普罗米修斯不满意吗?
  • 嗨@checketts,它可以工作并且适合某事。像表格或条形图,但对于百分位数,我需要很多桶(比如 DistributionSummary 确实提供了.publishPercentileHistogram()

标签: spring-boot grafana prometheus micrometer


【解决方案1】:

DistributionSummary 有一个配置 distributionStatisticExpiry 可以控制旋转数据。这是一种解决方法

但是 PrometheusDistributionSummary 不会使用这个字段

           case Prometheus:
                    histogram = new TimeWindowFixedBoundaryHistogram(clock, DistributionStatisticConfig.builder()
                            .expiry(Duration.ofDays(1825)) // effectively never roll over
                            .bufferLength(1)
                            .build()
                            .merge(distributionStatisticConfig), true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 2015-07-11
    • 1970-01-01
    相关资源
    最近更新 更多