12

新手在这里,试图了解有关千分尺的更多信息。我目前正在探索如何做到这一点的方法:

我正在使用启用了执行器和千分尺的 Spring Boot 2。考虑以下类:

@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 语句。绝对应该有一种简单的方法可以做到这一点,但是我找不到。

4

1 回答 1

7

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

@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;
  }
}

想一想:

  • 创建一个类来封装您的 Counter 逻辑
  • 为您的标签使用常量

让我知道它是否有效!

于 2020-01-07T17:58:10.183 回答