【问题标题】:@TImed with extraTags and @PathVariable@TImed 与 extraTags 和 @PathVariable
【发布时间】:2019-05-01 06:36:34
【问题描述】:

我有一个带有这样注释的方法:

@Timed(value="timed", extraTags={"account", Account.getById(@PathVariable("id")}
public Info getInfo(@PathVariable("id") String id) {
    return Info.getById(id);
}

以上当然行不通。希望你能看到我正在尝试做的事情。在给定路径上的 id 的情况下,将帐户标签设置为 Account.getById() 返回的值的正确语法是什么?

【问题讨论】:

    标签: java spring annotations micrometer spring-micrometer


    【解决方案1】:

    不能以您期望的方式将值传递给 @Timed 或任何其他注释。传递给任何注解的值必须是编译时间常数。

    编译时间常数类似于:

    private static final String tag = "account";
    

    Account.getById() 返回的值不是编译时间常数,因此不能被注解接受。

    【讨论】:

      【解决方案2】:

      我不相信您将能够使用@Timed 注释来完成您正在做的事情,但您可以手动计时该方法并做您想做的事情。这是一个例子:

      .
      .
      .
      @Autowired MeterRegistry meterRegistry;
      .
      .
      .
      public Info getInfo(@PathVariable("id") String id) {
          final Sample sample = Timer.start(meterRegistry);
      
          final Info info = Info.getById(id);
      
          sample.stop(Timer.builder("timed")
                           .tag("account", Account.getById(id)
                           .register(meterRegistry));
          return info;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-22
        • 1970-01-01
        • 2013-11-02
        • 2018-01-31
        • 2020-09-16
        • 2020-01-14
        • 2014-08-16
        相关资源
        最近更新 更多