From d13756599e8300ddbb24d1db19607c54caa9681a Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 27 Dec 2024 16:41:53 +0900 Subject: [PATCH] [#noissue] Cleanup DisableOptions --- .../pinpoint/bootstrap/PinpointBootStrap.java | 10 ++---- .../pinpoint/bootstrap/PinpointStarter.java | 3 +- .../bootstrap/config/DisableOptions.java | 27 +++++++++++++++ .../bootstrap/config/DisableOptionsTest.java | 33 +++++++++++++++++++ 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/DisableOptions.java create mode 100644 agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/config/DisableOptionsTest.java diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java index df036a13b6d3..282d62b1a258 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointBootStrap.java @@ -22,6 +22,7 @@ import com.navercorp.pinpoint.bootstrap.agentdir.BootDir; import com.navercorp.pinpoint.bootstrap.agentdir.ClassPathResolver; import com.navercorp.pinpoint.bootstrap.agentdir.JavaAgentPathResolver; +import com.navercorp.pinpoint.bootstrap.config.DisableOptions; import java.lang.instrument.Instrumentation; import java.nio.file.Files; @@ -43,7 +44,7 @@ public class PinpointBootStrap { public static void premain(String agentArgs, Instrumentation instrumentation) { - if (disabled()) { + if (DisableOptions.isBootDisabled()) { if (logger.isWarnEnabled()) { logger.warn("PinPoint is disabled via Env/Property."); } @@ -61,13 +62,6 @@ public static void premain(String agentArgs, Instrumentation instrumentation) { } - private static boolean disabled() { - final String prop = System.getProperty("pinpoint.disable"); - final String disable = prop != null ? prop : System.getenv("PINPOINT_DISABLE"); - - return (disable != null) && !disable.equalsIgnoreCase("false"); - } - private final String agentArgs; private final Instrumentation instrumentation; diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java index e81c30ea2fcb..f73f151dadfc 100644 --- a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/PinpointStarter.java @@ -20,6 +20,7 @@ import com.navercorp.pinpoint.bootstrap.classloader.ProfilerLibs; import com.navercorp.pinpoint.bootstrap.config.AgentSystemConfig; import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig; +import com.navercorp.pinpoint.bootstrap.config.DisableOptions; import com.navercorp.pinpoint.bootstrap.config.LogConfig; import com.navercorp.pinpoint.bootstrap.config.OsEnvSimpleProperty; import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; @@ -106,7 +107,7 @@ boolean start() { final Properties properties = loadProperties(); ProfilerConfig profilerConfig = ProfilerConfigLoader.load(properties); - if (!profilerConfig.getPinpointDisable().equalsIgnoreCase("false")) { + if (DisableOptions.isDisabled(properties::getProperty, DisableOptions.CONFIG)) { this.logger.warn("value of disable property is not false, pinpoint.disable=" + profilerConfig.getPinpointDisable()); return false; } diff --git a/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/DisableOptions.java b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/DisableOptions.java new file mode 100644 index 000000000000..5a4aea906df8 --- /dev/null +++ b/agent-module/bootstraps/bootstrap/src/main/java/com/navercorp/pinpoint/bootstrap/config/DisableOptions.java @@ -0,0 +1,27 @@ +package com.navercorp.pinpoint.bootstrap.config; + +import java.util.function.Function; + +public class DisableOptions { + public static final String SYSTEM = "pinpoint.disable"; + public static final String ENV = "PINPOINT_DISABLE"; + public static final String CONFIG = SYSTEM; + + private DisableOptions() { + } + + public static boolean isBootDisabled() { + if (isDisabled(System::getProperty, SYSTEM)) { + return true; + } + if (isDisabled(System::getenv, ENV)) { + return true; + } + return false; + } + + public static boolean isDisabled(Function properties, String key) { + String value = properties.apply(key); + return Boolean.parseBoolean(value); + } +} diff --git a/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/config/DisableOptionsTest.java b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/config/DisableOptionsTest.java new file mode 100644 index 000000000000..52f160c2e234 --- /dev/null +++ b/agent-module/bootstraps/bootstrap/src/test/java/com/navercorp/pinpoint/bootstrap/config/DisableOptionsTest.java @@ -0,0 +1,33 @@ +package com.navercorp.pinpoint.bootstrap.config; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Properties; + +class DisableOptionsTest { + + @AfterEach + void afterEach() { + Properties properties = System.getProperties(); + properties.remove(DisableOptions.SYSTEM); + } + + @Test + void isBootDisabled() { + Properties properties = System.getProperties(); + properties.setProperty(DisableOptions.SYSTEM, "true"); + + Assertions.assertTrue(DisableOptions.isBootDisabled()); + } + + @Test + void isBootDisabled_true() { + Properties properties = System.getProperties(); + properties.setProperty(DisableOptions.SYSTEM, "false"); + + Assertions.assertFalse(DisableOptions.isBootDisabled()); + } + +} \ No newline at end of file