-
Notifications
You must be signed in to change notification settings - Fork 876
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement MDC auto-instrumentation for log4j2
- Loading branch information
mateuszrzeszutek
committed
Sep 14, 2020
1 parent
221deb1
commit e860520
Showing
11 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
instrumentation/log4j/log4j-2.13.2/auto/log4j-2.13.2-auto.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apply from: "$rootDir/gradle/instrumentation.gradle" | ||
|
||
muzzle { | ||
pass { | ||
group = "org.apache.logging.log4j" | ||
module = "log4j-core" | ||
versions = "[2.13.2,)" | ||
assertInverse = true | ||
} | ||
} | ||
|
||
dependencies { | ||
library group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.2' | ||
|
||
implementation project(':instrumentation:log4j:log4j-2.13.2:library') | ||
|
||
testImplementation project(':instrumentation:log4j:log4j-2.13.2:testing') | ||
} |
92 changes: 92 additions & 0 deletions
92
.../main/java/io/opentelemetry/instrumentation/auto/log4j/v2_13_2/Log4j2Instrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.auto.log4j.v2_13_2; | ||
|
||
import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPrivate; | ||
import static net.bytebuddy.matcher.ElementMatchers.isStatic; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.instrumentation.log4j.v2_13_2.OpenTelemetryContextDataProvider; | ||
import io.opentelemetry.javaagent.tooling.Instrumenter; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.bytecode.assign.Assigner.Typing; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.logging.log4j.core.util.ContextDataProvider; | ||
|
||
@AutoService(Instrumenter.class) | ||
public final class Log4j2Instrumentation extends Instrumenter.Default { | ||
public Log4j2Instrumentation() { | ||
super("log4j2", "log4j"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("org.apache.logging.log4j.core.util.ContextDataProvider"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return named("org.apache.logging.log4j.core.impl.ThreadContextDataInjector"); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
"io.opentelemetry.instrumentation.log4j.v2_13_2.OpenTelemetryContextDataProvider" | ||
}; | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return Collections.singletonMap( | ||
isMethod() | ||
.and(isPrivate()) | ||
.and(isStatic()) | ||
.and(named("getProviders")) | ||
.and(takesArguments(0)), | ||
Log4j2Instrumentation.class.getName() + "$GetProvidersAdvice"); | ||
} | ||
|
||
public static class GetProvidersAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Return(typing = Typing.DYNAMIC, readOnly = false) | ||
List<ContextDataProvider> providers) { | ||
// check if already instrumented | ||
for (ContextDataProvider provider : providers) { | ||
if (provider instanceof OpenTelemetryContextDataProvider) { | ||
return; | ||
} | ||
} | ||
|
||
List<ContextDataProvider> instrumentedProviders = new ArrayList<>(providers.size() + 1); | ||
instrumentedProviders.addAll(providers); | ||
instrumentedProviders.add(new OpenTelemetryContextDataProvider()); | ||
providers = instrumentedProviders; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
instrumentation/log4j/log4j-2.13.2/auto/src/test/groovy/AutoLog4j2Test.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
class AutoLog4j2Test extends Log4j2Test { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
instrumentation/log4j/log4j-2.13.2/library/src/test/groovy/LibraryLog4j2Test.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
class LibraryLog4j2Test extends Log4j2Test { | ||
} |
15 changes: 15 additions & 0 deletions
15
instrumentation/log4j/log4j-2.13.2/testing/log4j-2.13.2-testing.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apply from: "$rootDir/gradle/java.gradle" | ||
|
||
dependencies { | ||
api project(':testing-common') | ||
|
||
api group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.2' | ||
|
||
implementation deps.guava | ||
|
||
implementation deps.groovy | ||
implementation deps.opentelemetryApi | ||
implementation deps.spock | ||
|
||
annotationProcessor group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.2' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters