【问题标题】:How to use @Timed of Micrometer to capture metrics for Future如何使用千分尺的@Timed 来捕获未来的指标
【发布时间】:2021-10-23 13:56:58
【问题描述】:

我想使用 Micrometer 记录异步方法最终发生时的执行时间、异常类型(如果有)。有推荐的方法吗?

例如 aws sqs 提供 sqsasync 客户端我希望千分尺监听它返回的 Future 并公开成功和失败延迟指标

@Timed(value = PrometheusConstants.SQS_ASYNC_PUBLISH_TIMER, percentiles = {0.5, 0.95, 0.99}, histogram = true)
public Future<SendMessageResult> sendAsync(String queueUrl, String message) {
        return amazonSQSAsync.sendMessageAsync(queueUrl, message);
    }

即使我的 localstack sqs 已关闭,上面的示例也无法正常工作 Micrometer 将请求视为成功。

注意:该示例应该基于文档工作,但它不工作https://micrometer.io/docs/concepts#_the_timed_annotation

【问题讨论】:

    标签: java amazon-web-services amazon-sqs amazon-sns spring-micrometer


    【解决方案1】:

    您可以使用measureTimeMillis 方法测量异步函数的时间,然后使用Timer.builder 创建计时器并使用record 方法记录您的指标。

    //create your Meter registry
    MeterRegistry registry = new SimpleMeterRegistry();
    
    // create your timer
    val myTimer = Timer.builder("my.timer")
    .publishPercentileHistogram()
    .register(registry);
    
    // Executes the given block and returns elapsed time in milliseconds
    val asyncFunctionTime = measureTimeMillis {
        sendAsync()    
    }
    
    // Record the metric and send to micrometer
    myTimer.record(asyncFunctionTime, TimeUnit.MILLISECONDS)
    

    有关更多信息,您可以查看千分尺docs

    【讨论】:

      猜你喜欢
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多