Skip to content

Commit 9b2bb24

Browse files
authored
Suppress duplicate AI agents (#1434)
1 parent 4a0a1df commit 9b2bb24

File tree

5 files changed

+129
-5
lines changed

5 files changed

+129
-5
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/BeforeAgentInstaller.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public static void beforeInstallBytebuddyAgent(Instrumentation instrumentation)
8989
instrumentation.addTransformer(new HeartBeatModuleClassFileTransformer());
9090
instrumentation.addTransformer(new ApplicationInsightsAppenderClassFileTransformer());
9191
instrumentation.addTransformer(new WebRequestTrackingFilterClassFileTransformer());
92+
instrumentation.addTransformer(new DuplicateAgentClassFileTransformer());
9293
}
9394

9495
private static void start(Instrumentation instrumentation) throws Exception {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* ApplicationInsights-Java
3+
* Copyright (c) Microsoft Corporation
4+
* All rights reserved.
5+
*
6+
* MIT License
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
8+
* software and associated documentation files (the ""Software""), to deal in the Software
9+
* without restriction, including without limitation the rights to use, copy, modify, merge,
10+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11+
* persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* The above copyright notice and this permission notice shall be included in all copies or
13+
* substantial portions of the Software.
14+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
* DEALINGS IN THE SOFTWARE.
20+
*/
21+
package com.microsoft.applicationinsights.agent.internal;
22+
23+
import java.lang.instrument.ClassFileTransformer;
24+
import java.security.ProtectionDomain;
25+
import java.util.HashSet;
26+
import java.util.Set;
27+
28+
import net.bytebuddy.jar.asm.ClassReader;
29+
import net.bytebuddy.jar.asm.ClassVisitor;
30+
import net.bytebuddy.jar.asm.ClassWriter;
31+
import net.bytebuddy.jar.asm.MethodVisitor;
32+
import org.checkerframework.checker.nullness.qual.Nullable;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
36+
import static net.bytebuddy.jar.asm.Opcodes.ASM7;
37+
import static net.bytebuddy.jar.asm.Opcodes.RETURN;
38+
39+
public class DuplicateAgentClassFileTransformer implements ClassFileTransformer {
40+
41+
private static final Logger logger = LoggerFactory.getLogger(DuplicateAgentClassFileTransformer.class);
42+
43+
// using constant here so that it will NOT get shaded
44+
// IMPORTANT FOR THIS NOT TO BE FINAL (or private)
45+
// OTHERWISE COMPILER COULD THEORETICALLY INLINE IT BELOW AND APPLY .substring(1)
46+
// and then it WOULD be shaded
47+
static String[] UNSHADED_CLASS_NAMES = new String[] {
48+
"!io/opentelemetry/auto/bootstrap/AgentBootstrap", // early 3.0 previews
49+
"!com/microsoft/applicationinsights/agent/internal/Premain", // 2.5.0+
50+
"!com/microsoft/applicationinsights/agent/internal/agent/AgentImplementation" // prior to 2.5.0
51+
};
52+
53+
private final Set<String> unshadedClassNames;
54+
55+
public DuplicateAgentClassFileTransformer() {
56+
Set<String> unshadedClassNames = new HashSet<>();
57+
for (String unshadedClassName : UNSHADED_CLASS_NAMES) {
58+
unshadedClassNames.add(unshadedClassName.substring(1));
59+
}
60+
this.unshadedClassNames = unshadedClassNames;
61+
}
62+
63+
@Override
64+
public byte /*@Nullable*/[] transform(@Nullable ClassLoader loader, @Nullable String className,
65+
@Nullable Class<?> classBeingRedefined,
66+
@Nullable ProtectionDomain protectionDomain,
67+
byte[] classfileBuffer) {
68+
69+
if (!unshadedClassNames.contains(className)) {
70+
return null;
71+
}
72+
try {
73+
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
74+
ClassVisitor cv = new DuplicateAgentClassVisitor(cw);
75+
ClassReader cr = new ClassReader(classfileBuffer);
76+
cr.accept(cv, 0);
77+
return cw.toByteArray();
78+
} catch (Throwable t) {
79+
logger.error(t.getMessage(), t);
80+
return null;
81+
}
82+
}
83+
84+
private static class DuplicateAgentClassVisitor extends ClassVisitor {
85+
86+
private final ClassWriter cw;
87+
88+
private DuplicateAgentClassVisitor(ClassWriter cw) {
89+
super(ASM7, cw);
90+
this.cw = cw;
91+
}
92+
93+
@Override
94+
public MethodVisitor visitMethod(int access, String name, String descriptor, @Nullable String signature,
95+
String /*@Nullable*/[] exceptions) {
96+
MethodVisitor mv = cw.visitMethod(access, name, descriptor, signature, exceptions);
97+
if (name.equals("premain") && descriptor.equals("(Ljava/lang/String;Ljava/lang/instrument/Instrumentation;)V")) {
98+
// no-op the initialize() method
99+
mv.visitCode();
100+
mv.visitInsn(RETURN);
101+
mv.visitMaxs(0, 1);
102+
mv.visitEnd();
103+
return null;
104+
} else {
105+
return mv;
106+
}
107+
}
108+
}
109+
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/JulListeningClassFileTransformer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
class JulListeningClassFileTransformer implements ClassFileTransformer {
3232

3333
// using constant here so that it will NOT get shaded
34-
// IMPORTANT FOR THIS NOT TO BE FINAL, OTHERWISE COMPILER COULD INLINE IT BELOW AND APPLY .substring(1)
34+
// IMPORTANT FOR THIS NOT TO BE FINAL (or private)
35+
// OTHERWISE COMPILER COULD THEORETICALLY INLINE IT BELOW AND APPLY .substring(1)
3536
// and then it WOULD be shaded
36-
public static String UNSHADED_PREFIX = "!java/util/logging/Logger";
37+
static String UNSHADED_PREFIX = "!java/util/logging/Logger";
3738

3839
private final String unshadedClassName = UNSHADED_PREFIX.substring(1);
3940

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/instrumentation/sdk/UnshadedSdkPackageName.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
public class UnshadedSdkPackageName {
2424

2525
// using constant here so that it will NOT get shaded
26-
// IMPORTANT FOR THIS NOT TO BE PUBLIC AND NOT FINAL, OTHERWISE COMPILER COULD INLINE IT BELOW AND APPLY
27-
// .substring(1) and then it WOULD be shaded
28-
public static String ALMOST_PREFIX = "!com/microsoft/applicationinsights";
26+
// IMPORTANT FOR THIS NOT TO BE FINAL (or private)
27+
// OTHERWISE COMPILER COULD THEORETICALLY INLINE IT BELOW AND APPLY .substring(1)
28+
// and then it WOULD be shaded
29+
static String ALMOST_PREFIX = "!com/microsoft/applicationinsights";
2930

3031
public static String get() {
3132
return ALMOST_PREFIX.substring(1);

agent/agent/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,22 @@
5959
* <li>Do dot touch any logging facilities here so we can configure them later
6060
* </ul>
6161
*/
62+
// IMPORTANT!! If this class is renamed, be sure to add the previous name to DuplicateAgentClassFileTransformer
63+
// so that previous versions will be suppressed (current versions with the same class name are suppressed
64+
// below via the alreadyLoaded flag
6265
public class OpenTelemetryAgent {
6366

67+
// this is to prevent the agent from loading and instrumenting everything twice
68+
// (leading to unpredictable results) when -javaagent:applicationinsights-agent.jar
69+
// appears multiple times on the command line
70+
private static volatile boolean alreadyLoaded;
71+
6472
public static void premain(final String agentArgs, final Instrumentation inst) {
73+
if (alreadyLoaded) {
74+
return;
75+
}
6576
agentmain(agentArgs, inst);
77+
alreadyLoaded = true;
6678
}
6779

6880
public static void agentmain(final String agentArgs, final Instrumentation inst) {

0 commit comments

Comments
 (0)