Skip to content

fix instrumentation module not loading silently when duplicate helper… #13005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public HelperInjector(

List<HelperClassDefinition> helpers =
helperClassNames.stream()
.distinct()
.map(
className ->
HelperClassDefinition.create(
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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<TypeDescription> 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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<TypeInstrumentation> typeInstrumentations() {
return singletonList(new DuplicateHelperInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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() {}
}
Loading