Skip to content

Commit

Permalink
[#noissue] Cleanup DisableOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 27, 2024
1 parent 7c8be87 commit d137565
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.");
}
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> properties, String key) {
String value = properties.apply(key);
return Boolean.parseBoolean(value);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit d137565

Please sign in to comment.