From 260f2379a6eb835e403e15a085e22e47cacc7080 Mon Sep 17 00:00:00 2001 From: FlorianBruckner Date: Fri, 10 Jan 2025 17:20:15 +0100 Subject: [PATCH] =?UTF-8?q?fix=20instrumentation=20module=20not=20loading?= =?UTF-8?q?=20silently=20when=20duplicate=20helper=E2=80=A6=20(#13005)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lauri Tulmin Co-authored-by: Lauri Tulmin --- .../javaagent/tooling/HelperInjector.java | 1 + .../src/main/java/helper/DuplicateHelper.java | 14 +++++++ .../DuplicateHelperInstrumentation.java | 34 +++++++++++++++++ .../DuplicateHelperInstrumentationModule.java | 37 +++++++++++++++++++ .../test/java/helper/DuplicateHelperTest.java | 19 ++++++++++ .../java/helper/DuplicateHelperTestClass.java | 16 ++++++++ 6 files changed, 121 insertions(+) create mode 100644 testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java create mode 100644 testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java create mode 100644 testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java create mode 100644 testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java create mode 100644 testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java diff --git a/muzzle/src/main/java/io/opentelemetry/javaagent/tooling/HelperInjector.java b/muzzle/src/main/java/io/opentelemetry/javaagent/tooling/HelperInjector.java index b5464273b60b..381d66a6ec06 100644 --- a/muzzle/src/main/java/io/opentelemetry/javaagent/tooling/HelperInjector.java +++ b/muzzle/src/main/java/io/opentelemetry/javaagent/tooling/HelperInjector.java @@ -117,6 +117,7 @@ public HelperInjector( List helpers = helperClassNames.stream() + .distinct() .map( className -> HelperClassDefinition.create( diff --git a/testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java b/testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java new file mode 100644 index 000000000000..258e6bac73fd --- /dev/null +++ b/testing-common/integration-tests/src/main/java/helper/DuplicateHelper.java @@ -0,0 +1,14 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package helper; + +public class DuplicateHelper { + public static String addSuffix(String string, String suffix) { + return string + suffix; + } + + private DuplicateHelper() {} +} diff --git a/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java new file mode 100644 index 000000000000..8cf4db9008cd --- /dev/null +++ b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentation.java @@ -0,0 +1,34 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package helper; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +public class DuplicateHelperInstrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return named("helper.DuplicateHelperTestClass"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod(named("transform"), this.getClass().getName() + "$TestAdvice"); + } + + @SuppressWarnings("unused") + public static class TestAdvice { + @Advice.OnMethodExit(suppress = Throwable.class) + public static void addSuffix(@Advice.Return(readOnly = false) String string) { + string = DuplicateHelper.addSuffix(string, " foo"); + } + } +} diff --git a/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java new file mode 100644 index 000000000000..4b31e4fecc18 --- /dev/null +++ b/testing-common/integration-tests/src/main/java/helper/DuplicateHelperInstrumentationModule.java @@ -0,0 +1,37 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package helper; + +import static java.util.Collections.singletonList; + +import com.google.auto.service.AutoService; +import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import java.util.Arrays; +import java.util.List; + +@AutoService(InstrumentationModule.class) +public class DuplicateHelperInstrumentationModule extends InstrumentationModule { + public DuplicateHelperInstrumentationModule() { + super("duplicate-helper"); + } + + @Override + public List getAdditionalHelperClassNames() { + // muzzle adds the same class as helper, listing it twice to ensure it doesn't fail + return Arrays.asList("helper.DuplicateHelper", "helper.DuplicateHelper"); + } + + @Override + public boolean isHelperClass(String className) { + return "helper.DuplicateHelper".equals(className); + } + + @Override + public List typeInstrumentations() { + return singletonList(new DuplicateHelperInstrumentation()); + } +} diff --git a/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java new file mode 100644 index 000000000000..b0aed068d476 --- /dev/null +++ b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTest.java @@ -0,0 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package helper; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class DuplicateHelperTest { + + @Test + void duplicateHelper() { + String string = DuplicateHelperTestClass.transform("test"); + assertThat(string).isEqualTo("test foo"); + } +} diff --git a/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java new file mode 100644 index 000000000000..6efbec4b0d22 --- /dev/null +++ b/testing-common/integration-tests/src/test/java/helper/DuplicateHelperTestClass.java @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package helper; + +class DuplicateHelperTestClass { + + // this method is transformed by instrumentation + public static String transform(String string) { + return string; + } + + private DuplicateHelperTestClass() {} +}