Skip to content

Support log trace injection for log4j 1.x and log4j 2.x when used without Slf4j#894

Merged
labbati merged 16 commits into
masterfrom
labbati/log4j
Jun 21, 2019
Merged

Support log trace injection for log4j 1.x and log4j 2.x when used without Slf4j#894
labbati merged 16 commits into
masterfrom
labbati/log4j

Conversation

@labbati
Copy link
Copy Markdown
Member

@labbati labbati commented Jun 20, 2019

Previously we only supported log correlation when the java application was using Slf4j.

This PR add support for trace <-> log correlation outside Slf4j if the app is using log4j 1.x or logj 2.x.

In addition to that we only defined a mandatory test suite that a logging library integration must satisfy in order to properly support trace correlation.

}

def "mdc shows trace and span ids for active scope"() {
when:
Copy link
Copy Markdown
Member Author

@labbati labbati Jun 20, 2019

Choose a reason for hiding this comment

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

@labbati labbati changed the title Labbati/log4j Support log trace injection for log4j 1.x and 2.x when used without Sl4fj Jun 20, 2019
@labbati labbati changed the title Support log trace injection for log4j 1.x and 2.x when used without Sl4fj Support log trace injection for log4j 1.x and log4j 2.x when used without Slf4j Jun 20, 2019
@labbati labbati added the comp: core Tracer core label Jun 20, 2019
@labbati labbati added this to the 0.31.0 milestone Jun 20, 2019
@labbati labbati marked this pull request as ready for review June 21, 2019 01:44
@labbati labbati requested a review from a team as a code owner June 21, 2019 01:44
.or(nameStartsWith("com.jinspired."))
.or(nameStartsWith("org.jinspired."))
.or(nameStartsWith("org.apache.log4j."))
.and(not(named("org.apache.log4j.MDC")))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems wrong - and should be inside or, shouldn't it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, at the end of the day it worked but it was very wrong 😄

@mar-kolya
Copy link
Copy Markdown
Contributor

You may want to look into why tests failing there - looks like jacoco needs some tuning


@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(isConstructor(), MDCContextAdvice.class.getName());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

org.apache.log4j.MDC seems to be class with only static methods. It seems slightly awkward to instrument it's constructor. Are you sure it is being called at all?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, nm, I was looking at only one implementation out of a few... using constructor as advice hook still seems odd.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It took a while to find out the source code corresponding to the version being downloaded by gradle. Here is the link for future reference: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/MDC.java?view=markup

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You do not have to change this now, or at all - but I think isTypeInitializer might have been better matcher here.

compile project(':dd-java-agent:agent-tooling')

testCompile group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4jVersion
testCompile group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpic: you may want to group testCompile clauses together and put them after non-test dependencies.


@AutoService(Instrumenter.class)
public class ThreadContextInstrumentation extends Instrumenter.Default {
public static final String MDC_INSTRUMENTATION_NAME = "log4j-thread-context";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should naming simply be log4j1 and log4j2 so make things less confusing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That is a good idea. Alternatively we could name it mdc in the same way it happens for the Slf4j instrumentation. WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not have a strong preference

Copy link
Copy Markdown
Contributor

@mar-kolya mar-kolya left a comment

Choose a reason for hiding this comment

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

LGTM overall.
But need to make sure that build is passing and couple of minor comments.

@labbati
Copy link
Copy Markdown
Member Author

labbati commented Jun 21, 2019

@mar-kolya might I have your final approval to merge?

@labbati labbati merged commit f01d473 into master Jun 21, 2019
@labbati labbati deleted the labbati/log4j branch June 21, 2019 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: core Tracer core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants