|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.opentelemetry.javaagent.instrumentation.log4j_2_13_2; |
| 17 | + |
| 18 | +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; |
| 19 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 20 | + |
| 21 | +import io.opentelemetry.javaagent.tooling.InstrumentationModule; |
| 22 | +import io.opentelemetry.javaagent.tooling.TypeInstrumentation; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import net.bytebuddy.description.method.MethodDescription; |
| 27 | +import net.bytebuddy.description.type.TypeDescription; |
| 28 | +import net.bytebuddy.matcher.ElementMatcher; |
| 29 | + |
| 30 | +public class AwsXrayLog4jInstrumentationModule extends InstrumentationModule { |
| 31 | + |
| 32 | + public AwsXrayLog4jInstrumentationModule() { |
| 33 | + super("log4j", "log4j-2.13.2", "aws-log4j", "aws-log4j-2.13.2"); |
| 34 | + } |
| 35 | + |
| 36 | + // The SPI will be merged with what's in the agent so we don't need to inject it, only our |
| 37 | + // provider implementation. |
| 38 | + @Override |
| 39 | + protected String[] additionalHelperClassNames() { |
| 40 | + return new String[] { |
| 41 | + "software.amazon.opentelemetry.javaagent.instrumentation.log4j_2_13_2." |
| 42 | + + "AwsXrayContextDataProvider" |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { |
| 48 | + return hasClassesNamed("org.apache.logging.log4j.core.util.ContextDataProvider"); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public List<TypeInstrumentation> typeInstrumentations() { |
| 53 | + return Collections.singletonList(new EmptyTypeInstrumentation()); |
| 54 | + } |
| 55 | + |
| 56 | + public static class EmptyTypeInstrumentation implements TypeInstrumentation { |
| 57 | + @Override |
| 58 | + public ElementMatcher<? super TypeDescription> typeMatcher() { |
| 59 | + // we cannot use ContextDataProvider here because one of the classes that we inject implements |
| 60 | + // this interface, causing the interface to be loaded while it's being transformed, which |
| 61 | + // leads |
| 62 | + // to duplicate class definition error after the interface is transformed and the triggering |
| 63 | + // class loader tries to load it. |
| 64 | + return named("org.apache.logging.log4j.core.impl.ThreadContextDataInjector"); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { |
| 69 | + // Nothing to instrument, no methods to match |
| 70 | + return Collections.emptyMap(); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments