Newbie here, trying to learn more about the micrometer. I'm currently exploring ways on how to accomplish this:
I'm using Spring boot 2 with actuator and micrometer enabled. Consider the following 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;
}
}
I'm stuck here and have to create different counter variables for each combination of tag values and the readability of the code gets affected really bad. I've many switch case statements than the above example. There should be definitely an easy way to do this, however I'm unable to find.
Tags can be used for slicing the metric for reasoning about the values. In the code above, page.visitors is the name of the meter, with age=20s as its tag. In this case, the counter is counting the visitors to the page with ages between 20 and 30. For a large system, we can append common tags to a registry.
There's static global registry support in Micrometer, Metrics.globalRegistry. Also, a set of static builders based on this global registry is provided to generate meters in Metrics: 4. Tags and Meters
MeterRegistry In Micrometer, a MeterRegistry is the core component used for registering meters. We can iterate over the registry and further each meter’s metrics to generate a time series in the backend with combinations of metrics and their dimension values. The simplest form of the registry is SimpleMeterRegistry.
An identifier of a Meter consists of a name and tags. It is suggested that we should follow a naming convention that separates words with a dot, to help guarantee the portability of metric names across multiple monitoring systems. Counter counter = registry.counter ("page.visitors", "age", "20s");
You have to use a Builder
if you want to count with a specific Tag. Here is how :
@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;
}
}
Think about:
Let me know if it worked !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With