1

Log error info as below:

try {
    invokeRomoteB();
} catch (Exception e){
    LOGGER.error("invoke B with exception!", e);
}

This will print error message and stack trace. We can find the cause easily.

But in high QPS situation, this will print lots of same stack info with huge CPU uses when many same exceptions occurred.

I find JDK can omit stack trace after same exception is thrown several times for built-in exceptions: JDK5.0 Release Notes.

Is it possible to omit stack trace (or limit frequency) after same common exception is thrown many times?

Reference:

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
bluearrow
  • 856
  • 2
  • 11
  • 26
  • For your own custom exception, you could instead override `fillInStackTrace()` and just return `this`. – Mark Rotteveel Jul 18 '22 at 14:34
  • The answer to this question might also depend on which specific logging framework you are using. Some of them allow setting a "cooldown" for specific log messages (but for the complete message). Additionally, what do you consider to be the "same exception"? Is only the location where it was thrown relevant, or the complete stack trace (in the latter case determining exception equality would also introduce some overhead). – Marcono1234 Jul 19 '22 at 12:49
  • @Marcono1234 It is a good idea to count log message frequency, and then "cooldown" to protect the system. With the same log message and the same type of throwable, we can omit calcLocation and traverse stacktrace. As for the implementation principle of JDK, I need to spend some time to learn. – bluearrow Jul 19 '22 at 16:14
  • JDK count exception frequency and then recompile method using preallocated exceptions that do not provide a stack trace. To disable use this flag: -XX:-OmitStackTraceInFastThrow – bluearrow Jul 19 '22 at 16:29
  • One choice to solve the problem is RateLimiter. Add RateLimit filter for error level log. – bluearrow Jul 28 '22 at 12:25

1 Answers1

0

This can be avoid by current limiting. Add one log4j2 interceptor to limit error log printing. The limiter filter can be implemented by guava

<configuration status="info">
    <RateLimitFilter level="ERROR" rate="10"/>
    <appenders>
    ...
    </appenders>
    <loggers>
    ...
    </loggers>
</configuration>

Another way, we can avoid this by circuit breaker(such as resilience4j or Hystrix).

bluearrow
  • 856
  • 2
  • 11
  • 26