Skip to content

Commit 5cffa07

Browse files
authored
Add a config agent.enable(default: true) to support disabling agent (#439)
1 parent 9d03d56 commit 5cffa07

File tree

5 files changed

+160
-139
lines changed

5 files changed

+160
-139
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Release Notes.
2424
* Support to customize the collect period of JVM relative metrics.
2525
* Upgrade netty-codec-http2 to 4.1.86.Final.
2626
* Put `Agent-Version` property reading in the premain stage to avoid deadlock when using `jarsigner`.
27+
* Add a config `agent.enable`(default: true) to support disabling the agent through system property `-Dskywalking.agent.disable=false`
28+
or system environment variable setting `SW_AGENT_ENABLE=false`.
2729

2830
#### Documentation
2931

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ public static class Agent {
183183
* Agent version. This is set by the agent kernel through reading MANIFEST.MF file in the skywalking-agent.jar.
184184
*/
185185
public static String VERSION = "UNKNOWN";
186+
187+
/**
188+
* Enable the agent kernel services and instrumentation.
189+
*/
190+
public static boolean ENABLE = true;
186191
}
187192

188193
public static class OsInfo {

apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Collections;
2424
import java.util.List;
2525
import java.util.Map;
26-
2726
import net.bytebuddy.ByteBuddy;
2827
import net.bytebuddy.agent.builder.AgentBuilder;
2928
import net.bytebuddy.description.NamedElement;
@@ -70,13 +69,18 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
7069
} catch (Exception e) {
7170
// try to resolve a new logger, and use the new logger to write the error log here
7271
LogManager.getLogger(SkyWalkingAgent.class)
73-
.error(e, "SkyWalking agent initialized failure. Shutting down.");
72+
.error(e, "SkyWalking agent initialized failure. Shutting down.");
7473
return;
7574
} finally {
7675
// refresh logger again after initialization finishes
7776
LOGGER = LogManager.getLogger(SkyWalkingAgent.class);
7877
}
7978

79+
if (!Config.Agent.ENABLE) {
80+
LOGGER.warn("SkyWalking agent is disabled.");
81+
return;
82+
}
83+
8084
try {
8185
pluginFinder = new PluginFinder(new PluginBootstrap().loadPlugins());
8286
} catch (AgentPackageNotFoundException ape) {
@@ -90,15 +94,15 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
9094
final ByteBuddy byteBuddy = new ByteBuddy().with(TypeValidation.of(Config.Agent.IS_OPEN_DEBUGGING_CLASS));
9195

9296
AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy).ignore(
93-
nameStartsWith("net.bytebuddy.")
94-
.or(nameStartsWith("org.slf4j."))
95-
.or(nameStartsWith("org.groovy."))
96-
.or(nameContains("javassist"))
97-
.or(nameContains(".asm."))
98-
.or(nameContains(".reflectasm."))
99-
.or(nameStartsWith("sun.reflect"))
100-
.or(allSkyWalkingAgentExcludeToolkit())
101-
.or(ElementMatchers.isSynthetic()));
97+
nameStartsWith("net.bytebuddy.")
98+
.or(nameStartsWith("org.slf4j."))
99+
.or(nameStartsWith("org.groovy."))
100+
.or(nameContains("javassist"))
101+
.or(nameContains(".asm."))
102+
.or(nameContains(".reflectasm."))
103+
.or(nameStartsWith("sun.reflect"))
104+
.or(allSkyWalkingAgentExcludeToolkit())
105+
.or(ElementMatchers.isSynthetic()));
102106

103107
JDK9ModuleExporter.EdgeClasses edgeClasses = new JDK9ModuleExporter.EdgeClasses();
104108
try {
@@ -140,7 +144,7 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
140144
}
141145

142146
Runtime.getRuntime()
143-
.addShutdownHook(new Thread(ServiceManager.INSTANCE::shutdown, "skywalking service shutdown thread"));
147+
.addShutdownHook(new Thread(ServiceManager.INSTANCE::shutdown, "skywalking service shutdown thread"));
144148
}
145149

146150
private static class Transformer implements AgentBuilder.Transformer {
@@ -163,7 +167,7 @@ public DynamicType.Builder<?> transform(final DynamicType.Builder<?> builder,
163167
EnhanceContext context = new EnhanceContext();
164168
for (AbstractClassEnhancePluginDefine define : pluginDefines) {
165169
DynamicType.Builder<?> possibleNewBuilder = define.define(
166-
typeDescription, newBuilder, classLoader, context);
170+
typeDescription, newBuilder, classLoader, context);
167171
if (possibleNewBuilder != null) {
168172
newBuilder = possibleNewBuilder;
169173
}
@@ -233,7 +237,10 @@ public void onBatch(int index, List<Class<?>> batch, List<Class<?>> types) {
233237
}
234238

235239
@Override
236-
public Iterable<? extends List<Class<?>>> onError(int index, List<Class<?>> batch, Throwable throwable, List<Class<?>> types) {
240+
public Iterable<? extends List<Class<?>>> onError(int index,
241+
List<Class<?>> batch,
242+
Throwable throwable,
243+
List<Class<?>> types) {
237244
LOGGER.error(throwable, "index={}, batch={}, types={}", index, batch, types);
238245
return Collections.emptyList();
239246
}

apm-sniffer/config/agent.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ agent.ssl_key_path=${SW_AGENT_SSL_KEY_PATH:}
8787

8888
agent.ssl_cert_chain_path=${SW_AGENT_SSL_CERT_CHAIN_PATH:}
8989

90+
# Enable the agent kernel services and instrumentation.
91+
agent.enable=${SW_AGENT_ENABLE:true}
92+
9093
# Limit the length of the ipv4 list size.
9194
osinfo.ipv4_list_size=${SW_AGENT_OSINFO_IPV4_LIST_SIZE:10}
9295

0 commit comments

Comments
 (0)