以 Micrometer 为单位的 Counter 指标的动态标签值
Posted
技术标签:
【中文标题】以 Micrometer 为单位的 Counter 指标的动态标签值【英文标题】:Dynamic tag values for the Counter metric in Micrometer 【发布时间】:2020-04-22 19:36:50 【问题描述】:这里是新手,正在尝试了解有关千分尺的更多信息。我目前正在探索如何实现这一目标:
我正在使用启用了执行器和千分尺的 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 语句。肯定有一种简单的方法可以做到这一点,但是我找不到。
【问题讨论】:
【参考方案1】:如果您想使用特定标签计数,则必须使用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;
想一想:
创建一个类来封装您的 Counter 逻辑 为您的标签使用常量让我知道它是否有效!
【讨论】:
在我看来你还是需要打电话给increment
好像也因为builder被缓存了,所以标签不断添加(见io.micrometer.core.instrument.Counter.Builder#tags
)。以上是关于以 Micrometer 为单位的 Counter 指标的动态标签值的主要内容,如果未能解决你的问题,请参考以下文章
如何在 io.micrometer 中启用 DiskSpaceMetrics
球衣应用中的 Micrometer Prometheus 指标(非弹簧)
为 AWS cloudwatch 的 Micrometer 配置 SpringBoot 2 应用程序
在 Spring Boot Camel 应用程序公开的 Micrometer / Prometheus 信息中包含其他 JMX 指标