Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Conversation

@tigerquoll
Copy link
Contributor

@tigerquoll tigerquoll commented Jul 29, 2019

Contributor Comments

This PR illustrates a proof on concept for a logging wrapper that supports lazy argument evaluation. Lazy parameter evaluation technique is automatically supported in Java 8 native logging (which we don't use), and log4j 2.4+ (which we appear to use via SLF4J wrappers).
Unfortunately the lazy parameter evaluation provided by log4j 2.4+ is not currently exposed in the interfaces provided by the current SLF4J logging framework (support is scheduled for release with SLF4J 2.x) So to support lazy evaluation while maintaining use of the current SLF4j logging libraries a wrapper is required.

Rationale for lazy parameter evaluation

Even though the logging framework will not construct a string unless the logging level indicates a log message will be needed, all parameter arguments are still evaluated by the JVM as part of the standard java parameter passing process.

Lazy parameter evaluation is a useful technique for when some arguments are very expensive to calculate (eg. Serialising a collection object into a JSON string). When disabled logging calls are deeply nested under multiple levels of iteration, then there can be a real hidden cost to this unneeded parameter evaluation overhead.

This PR illustrates a technique of utilising a logging wrapper that supports lambdas as logging parameters. Typical scenarios where this could be used include code scenarios such as:

LOG.trace("Printed result of an expensive operation: {}", expensiveOperation().toString())

Where expensiveOperation().toString is executed regardless of what logging level is enabled.

The suggested code to use with this wrapper would be

private static final LazyLogger LOG = LazyLoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

LOG.trace("Expensive operation only if trace logging enabled: {}", () -> expensiveOperation().toString()

As an example of usage
metron-platform/metron-writer/metron-writer-storm/src/main/java/org/apache/metron/writer/hdfs/HdfsWriter.java
has been updated to utilise this wrapper module.

Metron code has been analysed for logging usage, and those statements have been updated to use the lazy evaluation wrapper where it would be beneficial to performance.

The general guidance would be that any logging use that involves anything more than extremely basic string construction should use the lambda versions of the logging functions for performance reasons.
Error-level logging would be exempt from the requirement to use lambda versions of the logging functions due to error logging usually never being disabled, and thus no performance benefit is likely to come from forcing lazy parameter evaluation in this case.

If the lazy evaluation logging concept is approved I will add some unit testing before finalising the PR.

Pull Request Checklist

Thank you for submitting a contribution to Apache Metron.
Please refer to our Development Guidelines for the complete guide to follow for contributions.
Please refer also to our Build Verification Guidelines for complete smoke testing guides.

In order to streamline the review of the contribution we ask you follow these guidelines and ask you to double check the following:

For all changes:

  • Is there a JIRA ticket associated with this PR? If not one needs to be created at Metron Jira.
  • Does your PR title start with METRON-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
  • Has your PR been rebased against the latest commit within the target branch (typically master)?

For code changes:

  • Have you included steps to reproduce the behavior or problem that is being changed or addressed?

  • Have you included steps or a guide to how the change may be verified and tested manually?

  • Have you ensured that the full suite of tests and checks have been executed in the root metron folder via:

    mvn -q clean integration-test install && dev-utilities/build-utils/verify_licenses.sh 
    
  • Have you written or updated unit tests and or integration tests to verify your changes?

  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?

  • Have you verified the basic functionality of the build by building and running locally with Vagrant full-dev environment or the equivalent?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered by building and verifying the site-book? If not then run the following commands and the verify changes via site-book/target/site/index.html:

    cd site-book
    mvn site
    
  • Have you ensured that any documentation diagrams have been updated, along with their source files, using draw.io? See Metron Development Guidelines for instructions.

Note:

Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
It is also recommended that travis-ci is set up for your personal repository such that your branches are built there before submitting a pull request.

@tigerquoll
Copy link
Contributor Author

Since this is supposed to be a temporary fix pending Metron moving to SLF4J version 2.x, I've tried to minimise changes to those areas that would get a performance benefit from it, rather than doing a complete change over the the new API. Some classes where already shielding debug/trace statements by wrapping logging statements with "ifDebugEnabled()/ifTraceEnabled()"

@tigerquoll tigerquoll marked this pull request as ready for review July 31, 2019 12:15
Copy link
Contributor

@mmiklavc mmiklavc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 this looks solid to me. All my concerns have been addressed. Thanks @tigerquoll!

* 2. If the log level is enabled, then all passed lamdas are called and parmeters are passed in the correct order
* 3. All methods call their wrapped messages correctly
*/
public class LazyLoggerImplTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work on the tests!

@mmiklavc
Copy link
Contributor

mmiklavc commented Aug 1, 2019

One ask - can you update your PR title to match the Jira description? Thanks!

@mmiklavc
Copy link
Contributor

mmiklavc commented Aug 1, 2019

I missed one thing, which was requested by @ottobackwards on the Jira ticket - can you port my perf test into this PR? Don't have it run as part of a normal JUnit test run, include in the test code base similar to what was done with the HLLP example I provided on the Jira ticket.

@tigerquoll tigerquoll changed the title METRON-2195 rebase onto master METRON-2195 Add defensive log level checks when constructing logs is expensive Aug 1, 2019
@mmiklavc
Copy link
Contributor

mmiklavc commented Aug 5, 2019

+1

@asfgit asfgit closed this in 771d66b Aug 5, 2019
@tigerquoll tigerquoll mentioned this pull request Nov 14, 2019
11 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants