Micrometer 中计数器指标的动态标记值

Dynamic tag values for the Counter metric in Micrometer

这里是新手,想了解更多关于千分尺的知识。我目前正在探索如何实现这一目标的方法:

我正在使用 Spring 启动器 2,启用了执行器和千分尺。考虑以下 class:

@Component
class MyService {
  @Autowired
  AuthorizeTransaction callbackTransaction;

  @Autowired
  AuthorizeTransaction immediateTransaction;

  private MeterRegistry meterRegistry;
  private Counter requestCounter;
  private Counter responseCounter;

  public MyService(MeterRegistry meterRegistry) {
    this.meterRegistry = meterRegistry;
    initCounters();
  }

  private initCounters() {
    requestCounter = Counter.builder("iso_request")
                      .tags("mti", "0100")  // how do I change the value of this tag for other request types like 0200, 0120, etc.,
                      .register(meterRegistry);
    responseCounter = Counter.builder("iso_response")
                      .tags("mti", "0101")
                      .tags("response_code", "00") // how do I change the value of this tag for other response codes like 01, 09, etc.,
                      .register(meterRegistry);
  }

  public ISOMsg process(ISOMsg request) {
    ISOMsg response = null;

    try {
      switch(request.getMTI()) {  // org.jboss.iso.ISOMsg
        case "0100":
        case "0200":
          if ("0100".equals(request.getMTI())) {
            requestCounter.increment();
          } else {
            requestCounter.increment(); // I want to increment the counter of the same metric with tag mti=0200
          }
          response = immediateTransaction.process(request);
          // here I want to increment the response counter but with different MTIs and response codes
          break;
        case "0120":
        case "0121"
          response = callbackTransaction.process(request);
          break;
        default:
          log.error("error here")
      }
    } catch (Exception e) {
      log.error("error here")
    }

    return response;
  }
}

我被困在这里,必须为标签值的每个组合创建不同的计数器变量,代码的可读性受到非常严重的影响。与上面的示例相比,我有很多 switch case 语句。应该有一个简单的方法来做到这一点,但我找不到。

如果要使用特定标签进行计数,则必须使用 Builder。方法如下:

@Component
class MyService {
  @Autowired
  AuthorizeTransaction callbackTransaction;

  @Autowired
  AuthorizeTransaction immediateTransaction;

  private MeterRegistry meterRegistry;
  private Counter.Builder requestCounter;
  private Counter.Builder responseCounter;

  public MyService(MeterRegistry meterRegistry) {
    this.meterRegistry = meterRegistry;
    initCounters();
  }

  private initCounters() {
    requestCounter = Counter.builder("iso_request");
    responseCounter = Counter.builder("iso_response");
  }

  public ISOMsg process(ISOMsg request) {
    ISOMsg response = null;

    try {
      switch(request.getMTI()) {  // org.jboss.iso.ISOMsg
        case "0100":
        case "0200":
          requestCounter.tag("mti", request.getMTI()).registry(meterRegistry);
          response = immediateTransaction.process(request);
          responseCounter.tags("mti", request.getMTI(), "response_code", "0101").registry(meterRegistry);
          // here I want to increment the response counter but with different MTIs and response codes
          break;
        case "0120":
        case "0121"
          response = callbackTransaction.process(request);
          break;
        default:
          log.error("error here")
      }
    } catch (Exception e) {
      log.error("error here")
    }

    return response;
  }
}

想想:

  • 创建一个 class 来封装您的计数器逻辑
  • 为你的标签使用常量

让我知道它是否有效!