Prometheus + Micrometer:如何记录时间间隔和 success/failure 速率

Prometheus + Micrometer: how to record time intervals and success/failure rates

我从前端客户端向指标微服务发送 JSON,其中包含以下数据:

{
    totalTimeOnTheNetwork: number;
    timeElasticsearch: number;
    isSuccessful: boolean;
}

指标微服务当前处理数据的方式如下:

@AllArgsConstructor
@Service
public class ClientMetricsService {
    @Autowired
    MeterRegistry registry; // abstract class, SimpleMeterRegistry gets injected
    public void metrics(final MetricsProperty metrics) {
        final long networkTime = metrics.getTotalTime() - metrics.getElasticTime();
        registry.timer(ELASTIC_TIME_LABEL).record(metrics.getElasticTime(), TimeUnit.MILLISECONDS);
        registry.timer(TOTAL_TIME_LABEL).record(metrics.getTotalTime(), TimeUnit.MILLISECONDS);
        registry.timer(NETWORK_TIME_LABEL).record(networkTime, TimeUnit.MILLISECONDS);
    }
}

如您所见,我为每个时间间隔制定了一个新指标。我想知道是否可以将所有间隔放入一个指标中?如果我不必在指标微服务上而是在 Grafana 中计算网络时间,那就太好了。

此外,我可以在 registry.timer 中放置一个 success/failure 标签吗?我假设我需要在每个请求上使用 timer.builder 然后像这样:

Timer timer = Timer
    .builder("my.timer")
    .description("a description of what this timer does") // optional
    .tags("region", "test") // optional
    .register(registry);

这是一种典型的方法吗(例如,在每个 HTTP 请求上创建一个新计时器并将其 link 发送到注册表)或者计时器是否应该像我当前版本中那样从 MeterRegistry 派生?

或者您会使用另一个指标来记录 success/failure?将来指标可能会更改为 http-error-code,而不是布尔值,因此我不确定如何以可维护的方式实现它

Timer timer = Timer
    .builder("your-timer-name-here")
    .tags("ResponseStatus", isSuccessful.toString, "ResponseCode", http-error-code.toString)
    .register(registry);

timer.record(metrics.getTotalTime);

应该是可以回答您的问题的工作代码,但我感觉其中存在误解。为什么要将所有内容都放在一个指标中?

无论哪种方式,您都可以使用标签来解决这个问题。我不知道 Grafana 端的功能,但它可能就像将 .getElasticTime 信息放入另一个标签并通过它发送一样简单。