getProperties() {
+ return Map.of(
+ "otel.exporter.otlp.endpoint", "http://collector:55680",
+ "otel.exporter.otlp.insecure", "true",
+ "otel.config.max.attrs", "16"
+ );
+ }
+}
diff --git a/examples/extension/custom/src/main/java/com/example/javaagent/DemoResourceProvider.java b/examples/extension/custom/src/main/java/com/example/javaagent/DemoResourceProvider.java
new file mode 100644
index 000000000000..cad1ba0981ff
--- /dev/null
+++ b/examples/extension/custom/src/main/java/com/example/javaagent/DemoResourceProvider.java
@@ -0,0 +1,16 @@
+package com.example.javaagent;
+
+import com.google.auto.service.AutoService;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.sdk.autoconfigure.ConfigProperties;
+import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
+import io.opentelemetry.sdk.resources.Resource;
+
+@AutoService(ResourceProvider.class)
+public class DemoResourceProvider implements ResourceProvider {
+ @Override
+ public Resource createResource(ConfigProperties config) {
+ Attributes attributes = Attributes.builder().put("custom.resource", "demo").build();
+ return Resource.create(attributes);
+ }
+}
diff --git a/examples/extension/custom/src/main/java/com/example/javaagent/DemoSampler.java b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSampler.java
new file mode 100644
index 000000000000..67bcade15e36
--- /dev/null
+++ b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSampler.java
@@ -0,0 +1,35 @@
+package com.example.javaagent;
+
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.api.trace.SpanKind;
+import io.opentelemetry.context.Context;
+import io.opentelemetry.sdk.trace.data.LinkData;
+import io.opentelemetry.sdk.trace.samplers.Sampler;
+import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
+import io.opentelemetry.sdk.trace.samplers.SamplingResult;
+import java.util.List;
+
+/**
+ * This demo sampler filters out all internal spans whose name contains string "greeting".
+ *
+ * See
+ * OpenTelemetry Specification for more information about span sampling.
+ *
+ * @see DemoSdkTracerProviderConfigurer
+ */
+public class DemoSampler implements Sampler {
+ @Override
+ public SamplingResult shouldSample(Context parentContext, String traceId, String name,
+ SpanKind spanKind, Attributes attributes, List parentLinks) {
+ if (spanKind == SpanKind.INTERNAL && name.contains("greeting")) {
+ return SamplingResult.create(SamplingDecision.DROP);
+ } else {
+ return SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE);
+ }
+ }
+
+ @Override
+ public String getDescription() {
+ return "DemoSampler";
+ }
+}
diff --git a/examples/extension/custom/src/main/java/com/example/javaagent/DemoSdkTracerProviderConfigurer.java b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSdkTracerProviderConfigurer.java
new file mode 100644
index 000000000000..8ad8ab828787
--- /dev/null
+++ b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSdkTracerProviderConfigurer.java
@@ -0,0 +1,30 @@
+package com.example.javaagent;
+
+import com.google.auto.service.AutoService;
+import io.opentelemetry.sdk.autoconfigure.spi.SdkTracerProviderConfigurer;
+import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
+import io.opentelemetry.sdk.trace.SpanLimits;
+import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
+
+/**
+ * This is one of the main entry points for Instrumentation Agent's customizations.
+ * It allows configuring {@link SdkTracerProviderBuilder}.
+ * See the {@link #configure(SdkTracerProviderBuilder)} method below.
+ *
+ * Also see https://github.com/open-telemetry/opentelemetry-java/issues/2022
+ *
+ * @see SdkTracerProviderConfigurer
+ * @see DemoPropagatorProvider
+ */
+@AutoService(SdkTracerProviderConfigurer.class)
+public class DemoSdkTracerProviderConfigurer implements SdkTracerProviderConfigurer {
+ @Override
+ public void configure(SdkTracerProviderBuilder tracerProvider) {
+ tracerProvider
+ .setIdGenerator(new DemoIdGenerator())
+ .setSpanLimits(SpanLimits.builder().setMaxNumberOfAttributes(1024).build())
+ .setSampler(new DemoSampler())
+ .addSpanProcessor(new DemoSpanProcessor())
+ .addSpanProcessor(SimpleSpanProcessor.create(new DemoSpanExporter()));
+ }
+}
diff --git a/examples/extension/custom/src/main/java/com/example/javaagent/DemoSpanExporter.java b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSpanExporter.java
new file mode 100644
index 000000000000..476093a76b6b
--- /dev/null
+++ b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSpanExporter.java
@@ -0,0 +1,30 @@
+package com.example.javaagent;
+
+import io.opentelemetry.sdk.common.CompletableResultCode;
+import io.opentelemetry.sdk.trace.data.SpanData;
+import io.opentelemetry.sdk.trace.export.SpanExporter;
+import java.util.Collection;
+
+/**
+ * See
+ * OpenTelemetry Specification for more information about {@link SpanExporter}.
+ *
+ * @see DemoSdkTracerProviderConfigurer
+ */
+public class DemoSpanExporter implements SpanExporter {
+ @Override
+ public CompletableResultCode export(Collection spans) {
+ System.out.printf("%d spans exported%n", spans.size());
+ return CompletableResultCode.ofSuccess();
+ }
+
+ @Override
+ public CompletableResultCode flush() {
+ return CompletableResultCode.ofSuccess();
+ }
+
+ @Override
+ public CompletableResultCode shutdown() {
+ return CompletableResultCode.ofSuccess();
+ }
+}
diff --git a/examples/extension/custom/src/main/java/com/example/javaagent/DemoSpanProcessor.java b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSpanProcessor.java
new file mode 100644
index 000000000000..8abe2183e375
--- /dev/null
+++ b/examples/extension/custom/src/main/java/com/example/javaagent/DemoSpanProcessor.java
@@ -0,0 +1,45 @@
+package com.example.javaagent;
+
+import io.opentelemetry.context.Context;
+import io.opentelemetry.sdk.common.CompletableResultCode;
+import io.opentelemetry.sdk.trace.ReadWriteSpan;
+import io.opentelemetry.sdk.trace.ReadableSpan;
+import io.opentelemetry.sdk.trace.SpanProcessor;
+
+/**
+ * See
+ * OpenTelemetry Specification for more information about {@link SpanProcessor}.
+ *
+ * @see DemoSdkTracerProviderConfigurer
+ */
+public class DemoSpanProcessor implements SpanProcessor {
+ @Override
+ public void onStart(Context parentContext, ReadWriteSpan span) {
+ span.setAttribute("custom", "demo");
+ }
+
+ @Override
+ public boolean isStartRequired() {
+ return true;
+ }
+
+ @Override
+ public void onEnd(ReadableSpan span) {
+
+ }
+
+ @Override
+ public boolean isEndRequired() {
+ return false;
+ }
+
+ @Override
+ public CompletableResultCode shutdown() {
+ return CompletableResultCode.ofSuccess();
+ }
+
+ @Override
+ public CompletableResultCode forceFlush() {
+ return CompletableResultCode.ofSuccess();
+ }
+}
diff --git a/examples/extension/custom/src/main/java/com/example/javaagent/instrumentation/DemoServlet3InstrumentationModule.java b/examples/extension/custom/src/main/java/com/example/javaagent/instrumentation/DemoServlet3InstrumentationModule.java
new file mode 100644
index 000000000000..14bdb3f90be1
--- /dev/null
+++ b/examples/extension/custom/src/main/java/com/example/javaagent/instrumentation/DemoServlet3InstrumentationModule.java
@@ -0,0 +1,92 @@
+package com.example.javaagent.instrumentation;
+
+import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.safeHasSuperType;
+import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf;
+import static java.util.Collections.singletonList;
+import static java.util.Collections.singletonMap;
+import static net.bytebuddy.matcher.ElementMatchers.isPublic;
+import static net.bytebuddy.matcher.ElementMatchers.named;
+import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
+
+import com.google.auto.service.AutoService;
+import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
+import io.opentelemetry.javaagent.tooling.InstrumentationModule;
+import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
+import io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers;
+import io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher;
+import io.opentelemetry.javaagent.tooling.bytebuddy.matcher.NameMatchers;
+import java.util.List;
+import java.util.Map;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
+import net.bytebuddy.asm.Advice;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.matcher.ElementMatcher;
+import net.bytebuddy.matcher.ElementMatchers;
+
+/**
+ * This is a demo instrumentation which hooks into servlet invocation and modifies the http
+ * response.
+ */
+@AutoService(InstrumentationModule.class)
+public final class DemoServlet3InstrumentationModule extends InstrumentationModule {
+ public DemoServlet3InstrumentationModule() {
+ super("servlet-demo", "servlet-3");
+ }
+
+ /*
+ We want this instrumentation to be applied after the standard servlet instrumentation.
+ The latter creates a server span around http request.
+ This instrumentation needs access to that server span.
+ */
+ @Override
+ public int getOrder() {
+ return 1;
+ }
+
+ @Override
+ public ElementMatcher.Junction classLoaderMatcher() {
+ return ClassLoaderMatcher.hasClassesNamed("javax.servlet.http.HttpServlet");
+ }
+
+ @Override
+ public List typeInstrumentations() {
+ return singletonList(new DemoServlet3Instrumentation());
+ }
+
+ public static class DemoServlet3Instrumentation implements TypeInstrumentation {
+ @Override
+ public ElementMatcher typeMatcher() {
+ return AgentElementMatchers.safeHasSuperType(
+ NameMatchers.namedOneOf("javax.servlet.Filter", "javax.servlet.http.HttpServlet"));
+ }
+
+ @Override
+ public Map extends ElementMatcher super MethodDescription>, String> transformers() {
+ return singletonMap(
+ NameMatchers.namedOneOf("doFilter", "service")
+ .and(ElementMatchers.takesArgument(0, ElementMatchers.named("javax.servlet.ServletRequest")))
+ .and(ElementMatchers.takesArgument(1, ElementMatchers.named("javax.servlet.ServletResponse")))
+ .and(ElementMatchers.isPublic()),
+ DemoServlet3Instrumentation.class.getName() + "$DemoServlet3Advice");
+ }
+
+ @SuppressWarnings("unused")
+ public static class DemoServlet3Advice {
+
+ @Advice.OnMethodEnter(suppress = Throwable.class)
+ public static void onEnter(@Advice.Argument(value = 1) ServletResponse response) {
+ if (!(response instanceof HttpServletResponse)) {
+ return;
+ }
+
+ HttpServletResponse httpServletResponse = (HttpServletResponse) response;
+ if (!httpServletResponse.containsHeader("X-server-id")) {
+ httpServletResponse.setHeader(
+ "X-server-id", Java8BytecodeBridge.currentSpan().getSpanContext().getTraceId());
+ }
+ }
+ }
+ }
+}
diff --git a/examples/extension/gradle/wrapper/gradle-wrapper.jar b/examples/extension/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 000000000000..62d4c053550b
Binary files /dev/null and b/examples/extension/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/examples/extension/gradle/wrapper/gradle-wrapper.properties b/examples/extension/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 000000000000..be52383ef49c
--- /dev/null
+++ b/examples/extension/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,5 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
diff --git a/examples/extension/gradlew b/examples/extension/gradlew
new file mode 100755
index 000000000000..fbd7c515832d
--- /dev/null
+++ b/examples/extension/gradlew
@@ -0,0 +1,185 @@
+#!/usr/bin/env sh
+
+#
+# Copyright 2015 the original author or 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
+#
+# https://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.
+#
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+ echo "$*"
+}
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=`expr $i + 1`
+ done
+ case $i in
+ 0) set -- ;;
+ 1) set -- "$args0" ;;
+ 2) set -- "$args0" "$args1" ;;
+ 3) set -- "$args0" "$args1" "$args2" ;;
+ 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Escape application args
+save () {
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
+ echo " "
+}
+APP_ARGS=`save "$@"`
+
+# Collect all arguments for the java command, following the shell quoting and substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+exec "$JAVACMD" "$@"
diff --git a/examples/extension/gradlew.bat b/examples/extension/gradlew.bat
new file mode 100644
index 000000000000..5093609d512a
--- /dev/null
+++ b/examples/extension/gradlew.bat
@@ -0,0 +1,104 @@
+@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/examples/extension/settings.gradle b/examples/extension/settings.gradle
new file mode 100644
index 000000000000..92d3b56ac2ef
--- /dev/null
+++ b/examples/extension/settings.gradle
@@ -0,0 +1,5 @@
+rootProject.name = 'opentelemetry-java-instrumentation-extension'
+
+include "custom"
+include "smoke-tests"
+
diff --git a/examples/extension/smoke-tests/build.gradle b/examples/extension/smoke-tests/build.gradle
new file mode 100644
index 000000000000..ff208b79c112
--- /dev/null
+++ b/examples/extension/smoke-tests/build.gradle
@@ -0,0 +1,26 @@
+plugins {
+ id "java"
+}
+
+dependencies {
+ testImplementation("org.testcontainers:testcontainers:1.15.2")
+ testImplementation("com.fasterxml.jackson.core:jackson-databind:2.11.2")
+ testImplementation("com.google.protobuf:protobuf-java-util:3.12.4")
+ testImplementation("com.squareup.okhttp3:okhttp:3.12.12")
+ testImplementation("io.opentelemetry:opentelemetry-proto:1.0.0-alpha")
+ testImplementation("io.opentelemetry:opentelemetry-api:1.0.0")
+
+ testImplementation("ch.qos.logback:logback-classic:1.2.3")
+}
+
+tasks.test {
+ useJUnitPlatform()
+
+ def shadowTask = project(":custom").tasks.jar
+ inputs.files(layout.files(shadowTask))
+
+ doFirst {
+ jvmArgs("-Dio.opentelemetry.smoketest.agent.shadowJar.path=/Users/nsalnikovtarnovski/Documents/workspace/opentelemetry-java-instrumentation/javaagent/build/libs/opentelemetry-javaagent-1.2.0-SNAPSHOT-all.jar")
+ jvmArgs("-Dio.opentelemetry.smoketest.agent.extensionPath=${shadowTask.archiveFile.get()}")
+ }
+}
\ No newline at end of file
diff --git a/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/OkHttpUtils.java b/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/OkHttpUtils.java
new file mode 100644
index 000000000000..b8956aa297ca
--- /dev/null
+++ b/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/OkHttpUtils.java
@@ -0,0 +1,23 @@
+package com.example.javaagent.smoketest;
+
+import java.util.concurrent.TimeUnit;
+import okhttp3.OkHttpClient;
+
+public class OkHttpUtils {
+
+ static OkHttpClient.Builder clientBuilder() {
+ TimeUnit unit = TimeUnit.MINUTES;
+ return new OkHttpClient.Builder()
+ .connectTimeout(1, unit)
+ .writeTimeout(1, unit)
+ .readTimeout(1, unit);
+ }
+
+ public static OkHttpClient client() {
+ return client(false);
+ }
+
+ public static OkHttpClient client(boolean followRedirects) {
+ return clientBuilder().followRedirects(followRedirects).build();
+ }
+}
diff --git a/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/SmokeTest.java b/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/SmokeTest.java
new file mode 100644
index 000000000000..e6f2290cf81c
--- /dev/null
+++ b/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/SmokeTest.java
@@ -0,0 +1,197 @@
+package com.example.javaagent.smoketest;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.util.JsonFormat;
+import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
+import io.opentelemetry.proto.trace.v1.Span;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.ResponseBody;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.Network;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.containers.wait.strategy.Wait;
+import org.testcontainers.utility.MountableFile;
+
+abstract class SmokeTest {
+ private static final Logger logger = LoggerFactory.getLogger(SmokeTest.class);
+
+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+ protected static OkHttpClient client = OkHttpUtils.client();
+
+ private static final Network network = Network.newNetwork();
+ protected static final String agentPath =
+ System.getProperty("io.opentelemetry.smoketest.agent.shadowJar.path");
+ protected static final String extensionPath =
+ System.getProperty("io.opentelemetry.smoketest.agent.extensionPath");
+
+ protected abstract String getTargetImage(int jdk);
+
+ /**
+ * Subclasses can override this method to customise target application's environment
+ */
+ protected Map getExtraEnv() {
+ return Collections.emptyMap();
+ }
+
+ private static GenericContainer backend;
+ private static GenericContainer collector;
+
+ @BeforeAll
+ static void setupSpec() {
+ backend =
+ new GenericContainer<>(
+ "open-telemetry-docker-dev.bintray.io/java/smoke-fake-backend:latest")
+ .withExposedPorts(8080)
+ .waitingFor(Wait.forHttp("/health").forPort(8080))
+ .withNetwork(network)
+ .withNetworkAliases("backend")
+ .withLogConsumer(new Slf4jLogConsumer(logger));
+ backend.start();
+
+ collector =
+ new GenericContainer<>("otel/opentelemetry-collector-dev:latest")
+ .dependsOn(backend)
+ .withNetwork(network)
+ .withNetworkAliases("collector")
+ .withLogConsumer(new Slf4jLogConsumer(logger))
+ .withCopyFileToContainer(
+ MountableFile.forClasspathResource("/otel.yaml"), "/etc/otel.yaml")
+ .withCommand("--config /etc/otel.yaml");
+ collector.start();
+ }
+
+ protected GenericContainer target;
+
+ void startTarget(int jdk) {
+ target =
+ new GenericContainer<>(getTargetImage(jdk))
+ .withExposedPorts(8080)
+ .withNetwork(network)
+ .withLogConsumer(new Slf4jLogConsumer(logger))
+ .withCopyFileToContainer(
+ MountableFile.forHostPath(agentPath), "/opentelemetry-javaagent.jar")
+ .withCopyFileToContainer(
+ MountableFile.forHostPath(extensionPath), "/opentelemetry-extensions.jar")
+ .withEnv("JAVA_TOOL_OPTIONS", "-javaagent:/opentelemetry-javaagent.jar -Dotel.javaagent.debug=true")
+ .withEnv("OTEL_JAVAAGENT_EXTENSIONS", "/opentelemetry-extensions.jar")
+ .withEnv("OTEL_BSP_MAX_EXPORT_BATCH", "1")
+ .withEnv("OTEL_BSP_SCHEDULE_DELAY", "10")
+ .withEnv("OTEL_PROPAGATORS", "tracecontext,baggage,demo")
+ .withEnv(getExtraEnv());
+ target.start();
+ }
+
+ @AfterEach
+ void cleanup() throws IOException {
+ client
+ .newCall(
+ new Request.Builder()
+ .url(
+ String.format(
+ "http://localhost:%d/clear-requests", backend.getMappedPort(8080)))
+ .build())
+ .execute()
+ .close();
+ }
+
+ void stopTarget() {
+ target.stop();
+ }
+
+ @AfterAll
+ static void cleanupSpec() {
+ backend.stop();
+ collector.stop();
+ }
+
+ protected static int countResourcesByValue(Collection traces, String resourceName, String value) {
+ return (int) traces.stream()
+ .flatMap(it -> it.getResourceSpansList().stream())
+ .flatMap(it -> it.getResource().getAttributesList().stream())
+ .filter(kv -> kv.getKey().equals(resourceName) && kv.getValue().getStringValue().equals(value))
+ .count();
+ }
+
+ protected static int countSpansByName(
+ Collection traces, String spanName) {
+ return (int) getSpanStream(traces).filter(it -> it.getName().equals(spanName)).count();
+ }
+
+ protected static int countSpansByAttributeValue(
+ Collection traces, String attributeName, String attributeValue) {
+ return (int) getSpanStream(traces)
+ .flatMap(it -> it.getAttributesList().stream())
+ .filter(kv -> kv.getKey().equals(attributeName) && kv.getValue().getStringValue().equals(attributeValue))
+ .count();
+ }
+
+ protected static Stream getSpanStream(Collection traces) {
+ return traces.stream()
+ .flatMap(it -> it.getResourceSpansList().stream())
+ .flatMap(it -> it.getInstrumentationLibrarySpansList().stream())
+ .flatMap(it -> it.getSpansList().stream());
+ }
+
+ protected Collection waitForTraces()
+ throws IOException, InterruptedException {
+ String content = waitForContent();
+
+ return StreamSupport.stream(OBJECT_MAPPER.readTree(content).spliterator(), false)
+ .map(
+ it -> {
+ ExportTraceServiceRequest.Builder builder = ExportTraceServiceRequest.newBuilder();
+ // TODO(anuraaga): Register parser into object mapper to avoid de -> re ->
+ // deserialize.
+ try {
+ JsonFormat.parser().merge(OBJECT_MAPPER.writeValueAsString(it), builder);
+ } catch (InvalidProtocolBufferException | JsonProcessingException e) {
+ e.printStackTrace();
+ }
+ return builder.build();
+ })
+ .collect(Collectors.toList());
+ }
+
+ private String waitForContent() throws IOException, InterruptedException {
+ long previousSize = 0;
+ long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(30);
+ String content = "[]";
+ while (System.currentTimeMillis() < deadline) {
+
+ Request request =
+ new Request.Builder()
+ .url(String.format("http://localhost:%d/get-requests", backend.getMappedPort(8080)))
+ .build();
+
+ try (ResponseBody body = client.newCall(request).execute().body()) {
+ content = body.string();
+ }
+
+ if (content.length() > 2 && content.length() == previousSize) {
+ break;
+ }
+ previousSize = content.length();
+ System.out.printf("Current content size %d%n", previousSize);
+ TimeUnit.MILLISECONDS.sleep(500);
+ }
+
+ return content;
+ }
+}
diff --git a/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/SpringBootSmokeTest.java b/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/SpringBootSmokeTest.java
new file mode 100644
index 000000000000..813605add7c4
--- /dev/null
+++ b/examples/extension/smoke-tests/src/test/java/com/example/javaagent/smoketest/SpringBootSmokeTest.java
@@ -0,0 +1,52 @@
+package com.example.javaagent.smoketest;
+
+import io.opentelemetry.api.trace.TraceId;
+import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import okhttp3.Request;
+import okhttp3.Response;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class SpringBootSmokeTest extends SmokeTest {
+
+ protected String getTargetImage(int jdk) {
+ return "ghcr.io/open-telemetry/java-test-containers:smoke-springboot-jdk" + jdk
+ + "-20210218.577304949";
+ }
+
+ @Test
+ public void springBootSmokeTestOnJDK() throws IOException, InterruptedException {
+ startTarget(11);
+ String url = String.format("http://localhost:%d/greeting", target.getMappedPort(8080));
+ Request request = new Request.Builder().url(url).get().build();
+
+ String currentAgentVersion =
+ (String) new JarFile(agentPath)
+ .getManifest()
+ .getMainAttributes()
+ .get(Attributes.Name.IMPLEMENTATION_VERSION);
+
+ Response response = client.newCall(request).execute();
+ System.out.println(response.headers().toString());
+
+ Collection traces = waitForTraces();
+
+ Assertions.assertNotNull(response.header("X-server-id"));
+ Assertions.assertEquals(1, response.headers("X-server-id").size());
+ Assertions.assertTrue(TraceId.isValid(response.header("X-server-id")));
+ Assertions.assertEquals("Hi!", response.body().string());
+ Assertions.assertEquals(1, countSpansByName(traces, "/greeting"));
+ Assertions.assertEquals(0, countSpansByName(traces, "WebController.greeting"));
+ Assertions.assertEquals(1, countSpansByName(traces, "WebController.withSpan"));
+ Assertions.assertEquals(2, countSpansByAttributeValue(traces, "custom", "demo"));
+ Assertions.assertNotEquals(0,
+ countResourcesByValue(traces, "telemetry.auto.version", currentAgentVersion));
+ Assertions.assertNotEquals(0, countResourcesByValue(traces, "custom.resource", "demo"));
+
+ stopTarget();
+ }
+}
diff --git a/examples/extension/smoke-tests/src/test/resources/logback.xml b/examples/extension/smoke-tests/src/test/resources/logback.xml
new file mode 100644
index 000000000000..3fefcd72bae9
--- /dev/null
+++ b/examples/extension/smoke-tests/src/test/resources/logback.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+ %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/extension/smoke-tests/src/test/resources/otel.yaml b/examples/extension/smoke-tests/src/test/resources/otel.yaml
new file mode 100644
index 000000000000..a25201f896fd
--- /dev/null
+++ b/examples/extension/smoke-tests/src/test/resources/otel.yaml
@@ -0,0 +1,31 @@
+extensions:
+ health_check:
+ pprof:
+ endpoint: 0.0.0.0:1777
+ zpages:
+ endpoint: 0.0.0.0:55679
+
+receivers:
+ otlp:
+ protocols:
+ grpc:
+ zipkin:
+
+processors:
+ batch:
+
+exporters:
+ logging:
+ loglevel: debug
+ otlp:
+ endpoint: backend:8080
+ insecure: true
+
+service:
+ pipelines:
+ traces:
+ receivers: [otlp, zipkin]
+ processors: [batch]
+ exporters: [logging, otlp]
+
+ extensions: [health_check, pprof, zpages]
diff --git a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java
index ede1e9480e91..35996a8c1417 100644
--- a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java
+++ b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentClassLoader.java
@@ -26,6 +26,7 @@ public class AgentClassLoader extends URLClassLoader {
ClassLoader.registerAsParallelCapable();
}
+ // TODO remove in favor of ExtensionClassLoader
private static final String AGENT_INITIALIZER_JAR =
System.getProperty("otel.javaagent.experimental.initializer.jar", "");
diff --git a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentInitializer.java b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentInitializer.java
index 225d00513e60..c646093537d1 100644
--- a/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentInitializer.java
+++ b/javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentInitializer.java
@@ -34,21 +34,27 @@ private static synchronized void startAgent(Instrumentation inst, URL bootstrapU
throws Exception {
if (AGENT_CLASSLOADER == null) {
ClassLoader agentClassLoader = createAgentClassLoader("inst", bootstrapUrl);
+ AGENT_CLASSLOADER = agentClassLoader;
+
Class> agentInstallerClass =
agentClassLoader.loadClass("io.opentelemetry.javaagent.tooling.AgentInstaller");
Method agentInstallerMethod =
agentInstallerClass.getMethod("installBytebuddyAgent", Instrumentation.class);
ClassLoader savedContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
- Thread.currentThread().setContextClassLoader(AGENT_CLASSLOADER);
+ Thread.currentThread().setContextClassLoader(agentClassLoader);
agentInstallerMethod.invoke(null, inst);
} finally {
Thread.currentThread().setContextClassLoader(savedContextClassLoader);
}
- AGENT_CLASSLOADER = agentClassLoader;
}
}
+ // TODO misleading name
+ public static synchronized ClassLoader getAgentClassloader() {
+ return AGENT_CLASSLOADER;
+ }
+
/**
* Create the agent classloader. This must be called after the bootstrap jar has been appended to
* the bootstrap classpath.
@@ -57,6 +63,7 @@ private static synchronized void startAgent(Instrumentation inst, URL bootstrapU
* classloader
* @return Agent Classloader
*/
+ @SuppressWarnings("unchecked")
private static ClassLoader createAgentClassLoader(String innerJarFilename, URL bootstrapUrl)
throws Exception {
ClassLoader agentParent;
@@ -70,9 +77,18 @@ private static ClassLoader createAgentClassLoader(String innerJarFilename, URL b
Class> loaderClass =
ClassLoader.getSystemClassLoader()
.loadClass("io.opentelemetry.javaagent.bootstrap.AgentClassLoader");
- Constructor constructor =
- loaderClass.getDeclaredConstructor(URL.class, String.class, ClassLoader.class);
- return (ClassLoader) constructor.newInstance(bootstrapUrl, innerJarFilename, agentParent);
+ Constructor constructor =
+ (Constructor)
+ loaderClass.getDeclaredConstructor(URL.class, String.class, ClassLoader.class);
+ ClassLoader agentClassLoader =
+ constructor.newInstance(bootstrapUrl, innerJarFilename, agentParent);
+
+ Class> extensionClassLoaderClass =
+ agentClassLoader.loadClass("io.opentelemetry.javaagent.tooling.ExtensionClassLoader");
+ return (ClassLoader)
+ extensionClassLoaderClass
+ .getDeclaredMethod("getInstance", ClassLoader.class)
+ .invoke(null, agentClassLoader);
}
private static ClassLoader getPlatformClassLoader()
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java
index 1fb9dd5d5f02..08bad107832c 100644
--- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java
@@ -170,7 +170,6 @@ public static ResettableClassFileTransformer installBytebuddyAgent(
private static void installComponentsBeforeByteBuddy(
Iterable componentInstallers, Config config) {
- Thread.currentThread().setContextClassLoader(AgentInstaller.class.getClassLoader());
for (ComponentInstaller componentInstaller : componentInstallers) {
componentInstaller.beforeByteBuddyAgent(config);
}
@@ -243,7 +242,7 @@ private static Iterable loadByteBuddyAgentCustomizers(
private static List loadInstrumentationModules() {
return SafeServiceLoader.load(
- InstrumentationModule.class, AgentInstaller.class.getClassLoader())
+ InstrumentationModule.class, Thread.currentThread().getContextClassLoader())
.stream()
.sorted(Comparator.comparingInt(InstrumentationModule::getOrder))
.collect(Collectors.toList());
@@ -297,7 +296,7 @@ private static List loadBootstrapPackagePrefixes() {
new ArrayList<>(Arrays.asList(Constants.BOOTSTRAP_PACKAGE_PREFIXES));
Iterable bootstrapPackagesProviders =
SafeServiceLoader.load(
- BootstrapPackagesProvider.class, AgentInstaller.class.getClassLoader());
+ BootstrapPackagesProvider.class, Thread.currentThread().getContextClassLoader());
for (BootstrapPackagesProvider provider : bootstrapPackagesProviders) {
List packagePrefixes = provider.getPackagePrefixes();
log.debug(
@@ -492,7 +491,9 @@ private static Stream streamOf(Iterable iterable) {
private static boolean isIgnored(Class> c) {
ClassLoader cl = c.getClassLoader();
- if (cl != null && cl.getClass() == AgentClassLoader.class) {
+ if (cl != null
+ && (cl.getClass() == AgentClassLoader.class
+ || cl.getClass() == ExtensionClassLoader.class)) {
return true;
}
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExporterClassLoader.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExporterClassLoader.java
index 71ac1f199d62..b48780a451df 100644
--- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExporterClassLoader.java
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExporterClassLoader.java
@@ -21,6 +21,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+@Deprecated
public class ExporterClassLoader extends URLClassLoader {
private static final Logger log = LoggerFactory.getLogger(ExporterClassLoader.class);
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExtensionClassLoader.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExtensionClassLoader.java
new file mode 100644
index 000000000000..2ec20348e50f
--- /dev/null
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/ExtensionClassLoader.java
@@ -0,0 +1,172 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.tooling;
+
+import static io.opentelemetry.javaagent.tooling.ShadingRemapper.rule;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+import net.bytebuddy.jar.asm.ClassReader;
+import net.bytebuddy.jar.asm.ClassWriter;
+import net.bytebuddy.jar.asm.commons.ClassRemapper;
+
+/**
+ * This classloader is used to load arbitrary extensions for Otel Java instrumentation agent. Such
+ * extensions may include SDK components (exporters or propagators) and additional instrumentations.
+ * They have to be isolated and shaded to reduce interference with the user application and to make
+ * it compatible with shaded SDK used by the agent.
+ */
+// TODO find a way to initialize logging before using this class
+public class ExtensionClassLoader extends URLClassLoader {
+
+ // We need to prefix the names to prevent the gradle shadowJar relocation rules from touching
+ // them. It's possible to do this by excluding this class from shading, but it may cause issue
+ // with transitive dependencies down the line.
+ private static final ShadingRemapper remapper =
+ new ShadingRemapper(
+ rule("#io.opentelemetry.api", "#io.opentelemetry.javaagent.shaded.io.opentelemetry.api"),
+ rule(
+ "#io.opentelemetry.context",
+ "#io.opentelemetry.javaagent.shaded.io.opentelemetry.context"),
+ rule(
+ "#io.opentelemetry.extension.aws",
+ "#io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws"),
+ rule("#java.util.logging.Logger", "#io.opentelemetry.javaagent.bootstrap.PatchLogger"),
+ rule("#org.slf4j", "#io.opentelemetry.javaagent.slf4j"));
+
+ private final Manifest manifest;
+
+ public static ClassLoader getInstance(ClassLoader parent) {
+ // TODO add support for old deprecated properties, otel.exporter.jar and otel.initializer.jar
+ // TODO add support for system properties
+ URL extension = parseLocation(System.getenv("OTEL_JAVAAGENT_EXTENSIONS"));
+ if (extension != null) {
+ return new ExtensionClassLoader(extension, parent);
+ }
+ return parent;
+ }
+
+ private static URL parseLocation(String name) {
+ if (name == null) {
+ return null;
+ }
+ try {
+ return new File(name).toURI().toURL();
+ } catch (MalformedURLException e) {
+ System.err.println("Filename could not be parsed: %s. Extension location is ignored");
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public ExtensionClassLoader(URL url, ClassLoader parent) {
+ super(new URL[] {url}, parent);
+ this.manifest = getManifest(url);
+ }
+
+ @Override
+ protected Class> findClass(String name) throws ClassNotFoundException {
+ // Use resource loading to get the class as a stream of bytes, then use ASM to transform it.
+ InputStream in = super.getResourceAsStream(name.replace('.', '/') + ".class");
+ if (in == null) {
+ throw new ClassNotFoundException(name);
+ }
+ try {
+ byte[] bytes = remapClassBytes(in);
+ definePackageIfNeeded(name);
+ return defineClass(name, bytes, 0, bytes.length);
+ } catch (IOException e) {
+ throw new ClassNotFoundException(name, e);
+ } finally {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ // log.debug(e.getMessage(), e);
+ }
+ }
+ }
+
+ @Override
+ public InputStream getResourceAsStream(String name) {
+ InputStream originalStream = super.getResourceAsStream(name);
+ if (name.endsWith(".class")) {
+ try {
+ byte[] remappedClass = remapClassBytes(originalStream);
+ return new ByteArrayInputStream(remappedClass);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ return originalStream;
+ }
+
+ private void definePackageIfNeeded(String className) {
+ String packageName = getPackageName(className);
+ if (packageName == null) {
+ // default package
+ return;
+ }
+ if (isPackageDefined(packageName)) {
+ // package has already been defined
+ return;
+ }
+ try {
+ definePackage(packageName);
+ } catch (IllegalArgumentException e) {
+ // this exception is thrown when the package has already been defined, which is possible due
+ // to race condition with the check above
+ if (!isPackageDefined(packageName)) {
+ // this shouldn't happen however
+ e.printStackTrace();
+ // log.error(e.getMessage(), e);
+ }
+ }
+ }
+
+ private boolean isPackageDefined(String packageName) {
+ return getPackage(packageName) != null;
+ }
+
+ private void definePackage(String packageName) {
+ if (manifest == null) {
+ definePackage(packageName, null, null, null, null, null, null, null);
+ } else {
+ definePackage(packageName, manifest, null);
+ }
+ }
+
+ private static byte[] remapClassBytes(InputStream in) throws IOException {
+ ClassWriter cw = new ClassWriter(0);
+ ClassReader cr = new ClassReader(in);
+ cr.accept(new ClassRemapper(cw, remapper), ClassReader.EXPAND_FRAMES);
+ return cw.toByteArray();
+ }
+
+ private static String getPackageName(String className) {
+ int index = className.lastIndexOf('.');
+ return index == -1 ? null : className.substring(0, index);
+ }
+
+ private static Manifest getManifest(URL url) {
+ try (JarFile jarFile = new JarFile(url.toURI().getPath())) {
+ return jarFile.getManifest();
+ } catch (IOException | URISyntaxException e) {
+ e.printStackTrace();
+ // log.warn(e.getMessage(), e);
+ }
+ return null;
+ }
+}
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/InstrumentationModule.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/InstrumentationModule.java
index 5cf4f23bbebd..1f716d99b3d9 100644
--- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/InstrumentationModule.java
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/InstrumentationModule.java
@@ -184,7 +184,10 @@ private AgentBuilder.Identified.Extendable applyInstrumentationTransformers(
agentBuilder =
agentBuilder.transform(
new AgentBuilder.Transformer.ForAdvice()
- .include(Utils.getBootstrapProxy(), Utils.getAgentClassLoader())
+ .include(
+ Utils.getBootstrapProxy(),
+ Utils.getAgentClassLoader(),
+ Utils.getExtensionsClassLoader())
.withExceptionHandler(ExceptionHandlers.defaultExceptionHandler())
.advice(entry.getKey(), entry.getValue()));
}
@@ -233,18 +236,17 @@ public boolean matches(
muzzleLog.warn("-- {}", mismatch);
}
}
- } else {
- if (log.isDebugEnabled()) {
- log.debug(
- "Applying instrumentation: {} -- {} on {}",
- mainInstrumentationName(),
- InstrumentationModule.this.getClass().getName(),
- classLoader);
- }
+ return false;
}
-
- return isMatch;
}
+ if (log.isDebugEnabled()) {
+ log.debug(
+ "Applying instrumentation: {} -- {} on {}",
+ mainInstrumentationName(),
+ InstrumentationModule.this.getClass().getName(),
+ classLoader);
+ }
+
return true;
}
}
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/LoggingConfigurer.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/LoggingConfigurer.java
index cba5469ac17f..b745aa091bee 100644
--- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/LoggingConfigurer.java
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/LoggingConfigurer.java
@@ -31,6 +31,7 @@ static void configureLogger() {
setSystemPropertyDefault(SIMPLE_LOGGER_PREFIX + "io.perfmark.PerfMark", "INFO");
setSystemPropertyDefault(SIMPLE_LOGGER_PREFIX + "io.grpc.Context", "INFO");
setSystemPropertyDefault(SIMPLE_LOGGER_PREFIX + "io.grpc.internal.ServerImplBuilder", "INFO");
+ setSystemPropertyDefault(SIMPLE_LOGGER_PREFIX + "io.grpc.ManagedChannelRegistry", "INFO");
setSystemPropertyDefault(
SIMPLE_LOGGER_PREFIX + "io.grpc.internal.ManagedChannelImplBuilder", "INFO");
} else {
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/Utils.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/Utils.java
index a9d897eac1ee..88ba8fd721c8 100644
--- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/Utils.java
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/Utils.java
@@ -9,6 +9,7 @@
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader;
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader.BootstrapClassLoaderProxy;
+import io.opentelemetry.javaagent.bootstrap.AgentInitializer;
import java.net.URL;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDefinition;
@@ -23,6 +24,10 @@ public static ClassLoader getAgentClassLoader() {
return AgentInstaller.class.getClassLoader();
}
+ public static ClassLoader getExtensionsClassLoader() {
+ return AgentInitializer.getAgentClassloader();
+ }
+
/** Return a classloader which can be used to look up bootstrap resources. */
public static BootstrapClassLoaderProxy getBootstrapProxy() {
if (getAgentClassLoader() instanceof AgentClassLoader) {
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/bytebuddy/AgentLocationStrategy.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/bytebuddy/AgentLocationStrategy.java
index c6d497bda0b1..4c9b52253007 100644
--- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/bytebuddy/AgentLocationStrategy.java
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/bytebuddy/AgentLocationStrategy.java
@@ -18,6 +18,7 @@
* cannot find the desired resource, check up the classloader hierarchy until a resource is found.
*/
public class AgentLocationStrategy implements AgentBuilder.LocationStrategy {
+
public ClassFileLocator classFileLocator(ClassLoader classLoader) {
return classFileLocator(classLoader, null);
}
@@ -30,6 +31,7 @@ public ClassFileLocator classFileLocator(ClassLoader classLoader, JavaModule jav
locators.add(ClassFileLocator.ForClassLoader.WeaklyReferenced.of(classLoader));
classLoader = classLoader.getParent();
}
+
return new ClassFileLocator.Compound(locators.toArray(new ClassFileLocator[0]));
}
}
diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigInitializer.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigInitializer.java
index cefb4a098574..8463611d358c 100644
--- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigInitializer.java
+++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigInitializer.java
@@ -8,7 +8,6 @@
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.config.ConfigBuilder;
import io.opentelemetry.javaagent.spi.config.PropertySource;
-import io.opentelemetry.javaagent.tooling.AgentInstaller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -43,8 +42,7 @@ static Config create(Properties spiConfiguration, Properties configurationFile)
/** Retrieves all default configuration overloads using SPI and initializes Config. */
private static Properties loadSpiConfiguration() {
Properties propertiesFromSpi = new Properties();
- for (PropertySource propertySource :
- ServiceLoader.load(PropertySource.class, AgentInstaller.class.getClassLoader())) {
+ for (PropertySource propertySource : ServiceLoader.load(PropertySource.class)) {
propertiesFromSpi.putAll(propertySource.getProperties());
}
return propertiesFromSpi;