Skip to content

Commit 260f237

Browse files
fix instrumentation module not loading silently when duplicate helper… (#13005)
Co-authored-by: Lauri Tulmin <[email protected]> Co-authored-by: Lauri Tulmin <[email protected]>
1 parent 4ea2005 commit 260f237

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

muzzle/src/main/java/io/opentelemetry/javaagent/tooling/HelperInjector.java

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public HelperInjector(
117117

118118
List<HelperClassDefinition> helpers =
119119
helperClassNames.stream()
120+
.distinct()
120121
.map(
121122
className ->
122123
HelperClassDefinition.create(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package helper;
7+
8+
public class DuplicateHelper {
9+
public static String addSuffix(String string, String suffix) {
10+
return string + suffix;
11+
}
12+
13+
private DuplicateHelper() {}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package helper;
7+
8+
import static net.bytebuddy.matcher.ElementMatchers.named;
9+
10+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
11+
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
12+
import net.bytebuddy.asm.Advice;
13+
import net.bytebuddy.description.type.TypeDescription;
14+
import net.bytebuddy.matcher.ElementMatcher;
15+
16+
public class DuplicateHelperInstrumentation implements TypeInstrumentation {
17+
@Override
18+
public ElementMatcher<TypeDescription> typeMatcher() {
19+
return named("helper.DuplicateHelperTestClass");
20+
}
21+
22+
@Override
23+
public void transform(TypeTransformer transformer) {
24+
transformer.applyAdviceToMethod(named("transform"), this.getClass().getName() + "$TestAdvice");
25+
}
26+
27+
@SuppressWarnings("unused")
28+
public static class TestAdvice {
29+
@Advice.OnMethodExit(suppress = Throwable.class)
30+
public static void addSuffix(@Advice.Return(readOnly = false) String string) {
31+
string = DuplicateHelper.addSuffix(string, " foo");
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package helper;
7+
8+
import static java.util.Collections.singletonList;
9+
10+
import com.google.auto.service.AutoService;
11+
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
12+
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
@AutoService(InstrumentationModule.class)
17+
public class DuplicateHelperInstrumentationModule extends InstrumentationModule {
18+
public DuplicateHelperInstrumentationModule() {
19+
super("duplicate-helper");
20+
}
21+
22+
@Override
23+
public List<String> getAdditionalHelperClassNames() {
24+
// muzzle adds the same class as helper, listing it twice to ensure it doesn't fail
25+
return Arrays.asList("helper.DuplicateHelper", "helper.DuplicateHelper");
26+
}
27+
28+
@Override
29+
public boolean isHelperClass(String className) {
30+
return "helper.DuplicateHelper".equals(className);
31+
}
32+
33+
@Override
34+
public List<TypeInstrumentation> typeInstrumentations() {
35+
return singletonList(new DuplicateHelperInstrumentation());
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package helper;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
class DuplicateHelperTest {
13+
14+
@Test
15+
void duplicateHelper() {
16+
String string = DuplicateHelperTestClass.transform("test");
17+
assertThat(string).isEqualTo("test foo");
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package helper;
7+
8+
class DuplicateHelperTestClass {
9+
10+
// this method is transformed by instrumentation
11+
public static String transform(String string) {
12+
return string;
13+
}
14+
15+
private DuplicateHelperTestClass() {}
16+
}

0 commit comments

Comments
 (0)