gpt4 book ai didi

java - 微米计数器指标的动态标记值

转载 作者:行者123 更新时间:2023-12-03 11:18:04 25 4
gpt4 key购买 nike

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

我正在使用启用了执行器和千分尺的 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 语句,而不是上面的例子。绝对应该有一个简单的方法来做到这一点,但是我找不到。

最佳答案

您必须使用 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;
}
}

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

  • 让我知道它是否有效!

    关于java - 微米计数器指标的动态标记值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592118/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com