-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instrumentation to inject x-ray formatted IDs into log4j and logb… (
#49) * Add instrumentation to inject x-ray formatted IDs into log4j and logback contexts. * CLO * Remove prefix
- Loading branch information
Showing
19 changed files
with
418 additions
and
20 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
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,29 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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. | ||
*/ | ||
|
||
plugins { | ||
java | ||
id("com.github.johnrengelman.shadow") | ||
} | ||
|
||
base.archivesBaseName = "aws-instrumentation-log4j-2.13.2" | ||
|
||
dependencies { | ||
compileOnly("io.opentelemetry:opentelemetry-api") | ||
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling") | ||
compileOnly("net.bytebuddy:byte-buddy") | ||
|
||
compileOnly("org.apache.logging.log4j:log4j-core:2.13.2") | ||
} |
48 changes: 48 additions & 0 deletions
48
...azon/opentelemetry/javaagent/instrumentation/log4j_2_13_2/AwsXrayContextDataProvider.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,48 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.opentelemetry.javaagent.instrumentation.log4j_2_13_2; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.apache.logging.log4j.core.util.ContextDataProvider; | ||
|
||
/** | ||
* A {@link ContextDataProvider} which injects the trace and span ID of the current {@link Span} in | ||
* a format for consumption by AWS X-Ray and related services. | ||
*/ | ||
public class AwsXrayContextDataProvider implements ContextDataProvider { | ||
private static final String TRACE_ID_KEY = "AWS-XRAY-TRACE-ID"; | ||
|
||
@Override | ||
public Map<String, String> supplyContextData() { | ||
Span currentSpan = Span.current(); | ||
SpanContext spanContext = currentSpan.getSpanContext(); | ||
if (!spanContext.isValid()) { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
String value = | ||
"1-" | ||
+ spanContext.getTraceId().substring(0, 8) | ||
+ "-" | ||
+ spanContext.getTraceId().substring(8) | ||
+ "@" | ||
+ spanContext.getSpanId(); | ||
return Collections.singletonMap(TRACE_ID_KEY, value); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...entelemetry/javaagent/instrumentation/log4j_2_13_2/AwsXrayLog4jInstrumentationModule.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,73 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.opentelemetry.javaagent.instrumentation.log4j_2_13_2; | ||
|
||
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.javaagent.tooling.InstrumentationModule; | ||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class AwsXrayLog4jInstrumentationModule extends InstrumentationModule { | ||
|
||
public AwsXrayLog4jInstrumentationModule() { | ||
super("log4j", "log4j-2.13.2", "aws-log4j", "aws-log4j-2.13.2"); | ||
} | ||
|
||
// The SPI will be merged with what's in the agent so we don't need to inject it, only our | ||
// provider implementation. | ||
@Override | ||
protected String[] additionalHelperClassNames() { | ||
return new String[] { | ||
"software.amazon.opentelemetry.javaagent.instrumentation.log4j_2_13_2." | ||
+ "AwsXrayContextDataProvider" | ||
}; | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("org.apache.logging.log4j.core.util.ContextDataProvider"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Collections.singletonList(new EmptyTypeInstrumentation()); | ||
} | ||
|
||
public static class EmptyTypeInstrumentation implements TypeInstrumentation { | ||
@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 Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
// Nothing to instrument, no methods to match | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...main/resources/META-INF/services/io.opentelemetry.javaagent.tooling.InstrumentationModule
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 @@ | ||
software.amazon.opentelemetry.javaagent.instrumentation.log4j_2_13_2.AwsXrayLog4jInstrumentationModule |
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 @@ | ||
software.amazon.opentelemetry.javaagent.instrumentation.log4j_2_13_2.AwsXrayContextDataProvider |
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,31 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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. | ||
*/ | ||
|
||
plugins { | ||
java | ||
id("com.github.johnrengelman.shadow") | ||
} | ||
|
||
base.archivesBaseName = "aws-instrumentation-logback-1.0" | ||
|
||
dependencies { | ||
compileOnly("io.opentelemetry:opentelemetry-api") | ||
compileOnly("io.opentelemetry.instrumentation:opentelemetry-logback-1.0") | ||
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-api") | ||
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling") | ||
compileOnly("net.bytebuddy:byte-buddy") | ||
|
||
compileOnly("ch.qos.logback:logback-classic:1.0.0") | ||
} |
40 changes: 40 additions & 0 deletions
40
...ntelemetry/javaagent/instrumentation/logback_1_0/AwsXrayLogbackInstrumentationModule.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,40 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.opentelemetry.javaagent.instrumentation.logback_1_0; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.javaagent.tooling.InstrumentationModule; | ||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class AwsXrayLogbackInstrumentationModule extends InstrumentationModule { | ||
public AwsXrayLogbackInstrumentationModule() { | ||
super("logback", "logback-1.0", "aws-logback", "aws-logback-1.0"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Collections.singletonList(new AwsXrayLoggingEventInstrumentation()); | ||
} | ||
|
||
@Override | ||
public Map<String, String> contextStore() { | ||
return Collections.singletonMap( | ||
"ch.qos.logback.classic.spi.ILoggingEvent", Span.class.getName()); | ||
} | ||
} |
Oops, something went wrong.