Skip to content

Conversation

lpriima
Copy link
Contributor

@lpriima lpriima commented Jul 16, 2020

  1. revert of this revert : https://github.com/DataDog/dd-trace-java/pull/1662revert of this revert : Revert log injection changes #1662
  2. fix for ClassCastException #1654 and test for it:

The issue #1654 was introduced after instrumentation of log4j2. Original instrumentation assumed that interface ThreadContextMap always contains threadlocal field with instance of Map in generic parameter.

ThreadLocal<Map<?, ?>> localMap

In fact, log4j2 has 2 other implementations ( CopyOnWriteSortedArrayThreadContextMap , GarbageFreeSortedArrayThreadContextMap ) which contains StringMap in generic parameter:

ThreadLocal<org.apache.logging.log4j.util.StringMap> localMap

And this org.apache.logging.log4j.util.StringMap is not instance of java.util.Map. And It was causing ClassCastException in #1654 .

As a solution in this changeset class ThreadLocalWithDDTagsInitValue extends ThreadLocal has also generic parameter, which will contain "Map-like" type of instrumented implementation. "Map-like" property means method ThreadLocalWithDDTagsInitValue#putToMap looks for the way how to "put" datadog key/values in the instance of this type by following duck typing scenario:

  1. if this type is instance of Map, then use Map#put(String,String) method. If Not
  2. if this type has put(String, ?) method then use it. If not
  3. if this type has putValue(String, ?) method then use it. If not
  4. skip insertion of DD tags with WARN log message

Regression test Log4jThreadContextWithEnableThreadLocalsTest for this issue, modifies static final field to ensure log4j2 uses org.apache.logging.log4j.util.StringMap.

@lpriima lpriima force-pushed the lpriima/mdc_log_injection_v2 branch 6 times, most recently from 2a2d21a to 7ff119b Compare July 16, 2020 05:45
@lpriima lpriima marked this pull request as ready for review July 16, 2020 06:12
@lpriima lpriima requested a review from a team as a code owner July 16, 2020 06:12
@lpriima lpriima force-pushed the lpriima/mdc_log_injection_v2 branch from 7ff119b to f99c506 Compare July 17, 2020 00:00
Comment on lines +207 to +209
log.debug("file '{}' added to shutdown delete hook", file);
} else {
log.debug("file '{}' deleted", file);
Copy link
Contributor Author

@lpriima lpriima Jul 17, 2020

Choose a reason for hiding this comment

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

This is just more debug logging not really related to this change

@lpriima lpriima force-pushed the lpriima/mdc_log_injection_v2 branch from f99c506 to 7aa0225 Compare July 17, 2020 04:27
Copy link
Contributor

@tylerbenson tylerbenson left a comment

Choose a reason for hiding this comment

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

Can you identify which tests were added to validate which previous problems were solved?

Comment on lines +43 to +47
def "helper class names are hardcoded in Log Instrumentations"() {
expect:
LogContextScopeListener.name == "datadog.trace.agent.tooling.log.LogContextScopeListener"
ThreadLocalWithDDTagsInitValue.name == "datadog.trace.agent.tooling.log.ThreadLocalWithDDTagsInitValue"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the point of this test. Why would you want to assert the full name of these classes?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would assume the purpose is to guard against automated refactoring changing the classes but not the class names hard coded in various places. i.e this test would fail if either of these classes were renamed.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe most refactoring tools would actually detect this usage and also update the string since it's a fully qualified class name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is for possible class renaming and it's temporary solution.
Better solution should be running tests with actual classpath restricted by this method:
https://github.com/DataDog/dd-trace-java/pull/1688/files#diff-ddc855d905a56896d1c42dfd404e8541R50-R53

@lpriima
Copy link
Contributor Author

lpriima commented Jul 20, 2020

Can you identify which tests were added to validate which previous problems were solved?

Log4jThreadContextWithEnableThreadLocalsTest that's bullet 2 of description

@tylerbenson
Copy link
Contributor

tylerbenson commented Jul 20, 2020

It isn't clear to me how that validates the fix. Please clarify.

Perhaps better describe the problem this is solving and how this fixes it?

@lpriima
Copy link
Contributor Author

lpriima commented Jul 20, 2020

It isn't clear to me how that validates the fix. Please clarify.

Perhaps better describe the problem this is solving and how this fixes it?

I've updates description here.
Should I also put it in the issue #1654 comments ?

Copy link
Contributor

@richardstartin richardstartin left a comment

Choose a reason for hiding this comment

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

I did some manual testing with this and didn't find any issues.

@lpriima lpriima merged commit 1086145 into master Jul 22, 2020
@lpriima lpriima deleted the lpriima/mdc_log_injection_v2 branch July 22, 2020 03:56
@github-actions github-actions bot added this to the 0.59.0 milestone Jul 22, 2020
cecile75 added a commit to DataDog/documentation that referenced this pull request Jul 21, 2021
Java tracer support configuration of UST for logs since DataDog/dd-trace-java#1688 (0.59)
apigirl pushed a commit to DataDog/documentation that referenced this pull request Jul 21, 2021
Java tracer support configuration of UST for logs since DataDog/dd-trace-java#1688 (0.59)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants