-
Notifications
You must be signed in to change notification settings - Fork 872
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 (#1200)
* Implement MDC auto-instrumentation for log4j2 * Implement MDC auto-instrumentation for log4j2 2.7 * Implement MDC auto-instrumentation for log4j2 * Implement MDC auto-instrumentation for log4j2 * Implement MDC auto-instrumentation for log4j2 * Implement MDC auto-instrumentation for log4j1 * Implement MDC auto-instrumentation for log4j2
- Loading branch information
Mateusz Rzeszutek
authored
Sep 18, 2020
1 parent
beb1901
commit d89ce81
Showing
22 changed files
with
414 additions
and
39 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
39 changes: 39 additions & 0 deletions
39
...n-api/src/main/java/io/opentelemetry/instrumentation/api/log/LoggingContextConstants.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,39 @@ | ||
/* | ||
* 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.api.log; | ||
|
||
/** | ||
* This class contains several constants used in logging libraries' Mapped Diagnostic Context | ||
* instrumentations. | ||
* | ||
* @see org.slf4j.MDC | ||
* @see org.apache.logging.log4j.ThreadContext | ||
* @see org.apache.log4j.MDC | ||
*/ | ||
public final class LoggingContextConstants { | ||
/** Key under which the current trace id will be injected into the context data. */ | ||
public static final String TRACE_ID = "traceId"; | ||
/** Key under which the current span id will be injected into the context data. */ | ||
public static final String SPAN_ID = "spanId"; | ||
/** | ||
* Key under which a boolean indicating whether current span is sampled will be injected into the | ||
* context data. | ||
*/ | ||
public static final String SAMPLED = "sampled"; | ||
|
||
private LoggingContextConstants() {} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
instrumentation/log4j/log4j-2-testing/log4j-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.7' | ||
|
||
implementation deps.guava | ||
|
||
implementation deps.groovy | ||
implementation deps.opentelemetryApi | ||
implementation deps.spock | ||
|
||
annotationProcessor group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7' | ||
} |
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.
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-testing') | ||
} |
69 changes: 69 additions & 0 deletions
69
...in/java/io/opentelemetry/instrumentation/auto/log4j/v2_13_2/Log4j2MdcInstrumentation.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,69 @@ | ||
/* | ||
* 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.named; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.tooling.Instrumenter; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(Instrumenter.class) | ||
public final class Log4j2MdcInstrumentation extends Instrumenter.Default { | ||
public Log4j2MdcInstrumentation() { | ||
super("log4j2", "log4j", "log4j-2.13.2"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("org.apache.logging.log4j.core.util.ContextDataProvider"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<? super TypeDescription> typeMatcher() { | ||
// we cannot use ContextDataProvider here because one of the classes that we inject implements | ||
// this interface, causing the interface to be loaded while it's being transformed, which leads | ||
// to duplicate class definition error after the interface is transformed and the triggering | ||
// class loader tries to load it. | ||
return named("org.apache.logging.log4j.core.impl.ThreadContextDataInjector"); | ||
} | ||
|
||
@Override | ||
public String[] helperResourceNames() { | ||
return new String[] { | ||
"META-INF/services/org.apache.logging.log4j.core.util.ContextDataProvider", | ||
}; | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
"io.opentelemetry.instrumentation.log4j.v2_13_2.OpenTelemetryContextDataProvider" | ||
}; | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
// Nothing to instrument, injecting helper resource & class is enough | ||
return Collections.emptyMap(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import io.opentelemetry.auto.test.AgentTestTrait | ||
|
||
class AutoLog4j2Test extends Log4j2Test implements AgentTestTrait { | ||
} |
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
1 change: 1 addition & 0 deletions
1
...c/main/resources/META-INF/services/org.apache.logging.log4j.core.util.ContextDataProvider
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 @@ | ||
io.opentelemetry.instrumentation.log4j.v2_13_2.OpenTelemetryContextDataProvider |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import io.opentelemetry.auto.test.InstrumentationTestTrait | ||
|
||
class LibraryLog4j2Test extends Log4j2Test implements InstrumentationTestTrait { | ||
} |
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') | ||
} |
Oops, something went wrong.