-
Notifications
You must be signed in to change notification settings - Fork 873
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 2.7
- Loading branch information
mateuszrzeszutek
committed
Sep 14, 2020
1 parent
e860520
commit 5687f2e
Showing
11 changed files
with
128 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
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
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
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
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/instrumentation.gradle" | ||
|
||
muzzle { | ||
pass { | ||
group = "org.apache.logging.log4j" | ||
module = "log4j-core" | ||
versions = "[2.7,2.13.2)" | ||
} | ||
} | ||
|
||
dependencies { | ||
library group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7' | ||
|
||
testImplementation project(':instrumentation:log4j:log4j-2-testing') | ||
} |
86 changes: 86 additions & 0 deletions
86
...rc/main/java/io/opentelemetry/instrumentation/auto/log4j/v2_7/Log4j27Instrumentation.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,86 @@ | ||
/* | ||
* 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_7; | ||
|
||
import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed; | ||
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.tooling.Instrumenter; | ||
import io.opentelemetry.trace.SpanContext; | ||
import io.opentelemetry.trace.TracingContextUtils; | ||
import java.util.Collections; | ||
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.util.SortedArrayStringMap; | ||
import org.apache.logging.log4j.util.StringMap; | ||
|
||
@AutoService(Instrumenter.class) | ||
public class Log4j27Instrumentation extends Instrumenter.Default { | ||
public Log4j27Instrumentation() { | ||
super("log4j2", "log4j", "log4j2.7"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderMatcher() { | ||
return not(hasClassesNamed("org.apache.logging.log4j.core.util.ContextDataProvider")); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
return implementsInterface(named("org.apache.logging.log4j.core.ContextDataInjector")); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return Collections.singletonMap( | ||
isMethod() | ||
.and(named("injectContextData")) | ||
.and(returns(named("org.apache.logging.log4j.util.StringMap"))), | ||
Log4j27Instrumentation.class.getName() + "$InjectContextDataAdvice"); | ||
} | ||
|
||
public static class InjectContextDataAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Return(typing = Typing.DYNAMIC, readOnly = false) StringMap contextData) { | ||
SpanContext currentContext = TracingContextUtils.getCurrentSpan().getContext(); | ||
if (!currentContext.isValid()) { | ||
return; | ||
} | ||
|
||
if (contextData.containsKey("traceId")) { | ||
// Assume already instrumented event if traceId is present. | ||
return; | ||
} | ||
|
||
StringMap newContextData = new SortedArrayStringMap(contextData); | ||
newContextData.putValue("traceId", currentContext.getTraceId().toLowerBase16()); | ||
newContextData.putValue("spanId", currentContext.getSpanId().toLowerBase16()); | ||
newContextData.putValue("traceFlags", currentContext.getTraceFlags().toLowerBase16()); | ||
contextData = newContextData; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
instrumentation/log4j/log4j-2.7/src/test/groovy/Log4j27Test.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 Log4j27Test 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