Add Dynamic Tag Support in Log4j2Metrics#7389
Conversation
|
Thank you for the PR! |
Hi @jonatan-ivanov
Since our services uses multiple dependencies, and those dependencies can also throw or error logs with throwable object, we cannot rely on the custom metric and need to utilise the micro meter since most of the dependencies utilise log4j to log the exception, therefore, I thought to utilise micrometer core to publish the tags in |
|
So if I understand correctly you are trying to parse out the necessary information from the log message (and logger name, context map ,log level, etc). This sounds like very brittle to me but I might have an alternative solution to this where you don't need to parse strings. (I'm also afraid that your solution might have a performance impact that not every user wants to pay for if they don't attach tags dynamically.) I would recommend taking a look at the Observation API, the whole Spring portfolio is instrumented with it as well as a lot of other frameworks/libraries. The idea behind it is that when an instrumentation signals an error (calls class ErrorCountingObservationHandler implements ObservationHandler<Observation.Context> {
private final Meter.MeterProvider<Counter> counterProvider;
ErrorCountingObservationHandler(MeterRegistry registry) {
this.counterProvider = Counter.builder("errors")
// here you can add all the static tags
.tag("application", "appA")
.tag("org", "eCommerce")
.withRegistry(registry);
}
@Override
public void onStop(Observation.Context context) {
Throwable error = context.getError();
if (error != null) {
counterProvider
// here you can add all the dynamic tags from the context
.withTag("error", error.getClass().getName())
.increment();
}
}
@Override
public boolean supportsContext(Observation.Context context) {
// here you can select which Observations (contexts) should this handler listen
// e.g.: return context instanceof ServerRequestObservationContext;
return true; // means all
}
}This is a very basic and simple example and there are some details that might no be apparent:
Here's a conference talk if you want to learn more: https://www.youtube.com/watch?v=Qyku6cR6ADY Please let me know if this helps or the Observation API will not work in your use-case. |
|
Thanks @jonatan-ivanov for sharing me this alternative approach. I'll look into this and get back to you in case I have any doubts. |
|
If you would like us to look at this PR, please provide the requested information. If the information is not provided within the next 7 days this PR will be closed. |
|
@jonatan-ivanov I was looking into the Observation API to understand if I can utilise this to solve my use case and got some doubts related to it.
For First point, sooner or the applications will get migrated to spring boot 3 but my major concern was more related to second point. for (Data data: dbResults) {
try {
// parsing the data which we receive from db
publisher.submit(parsedData);
} catch (Exception e) {
log.error("Error while parsing the data", e);
observationRegistry.getCurrentObservation().error(e);// this change will be required to perform in multiple applications and their dependencies
}
}But incase we add
I was thinking on a similar line and thought to have a mechanism on deciding whether to opt for dynamic tag or not. If application didn't opt for dynamic tag then then will continue using those static counter itself and their performance will not get impacted. Here are the proposed changes. 07af8ca (build is failing, I will check it and update it.) @jonatan-ivanov Let me know in case my understanding in not correct. |
Signed-off-by: Harsh Verma <harshverma3305@gmail.com> Signed-off-by: Harsh3305 <harshverma3305@gmail.com>
Signed-off-by: Harsh3305 <harshverma3305@gmail.com> Signed-off-by: Harsh3305 <harshverma3305@gmail.com>
Signed-off-by: Harsh3305 <harshverma3305@gmail.com>
Signed-off-by: Harsh3305 <harshverma3305@gmail.com>
Signed-off-by: Harsh3305 <harshverma3305@gmail.com>
|
@jonatan-ivanov In aa44178 commit, |
Adding a support of adding tag on runtime using log events in Log4j2Metrics. This will allow us to send the tags like exception name, Kafka partition, ... as a tag.