diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/Agent.java b/newrelic-agent/src/main/java/com/newrelic/agent/Agent.java index 2fdb444c08..f6568db66c 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/Agent.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/Agent.java @@ -9,7 +9,11 @@ import com.google.common.collect.ImmutableMap; import com.newrelic.agent.bridge.AgentBridge; -import com.newrelic.agent.config.*; +import com.newrelic.agent.config.AgentJarHelper; +import com.newrelic.agent.config.ConfigService; +import com.newrelic.agent.config.ConfigServiceFactory; +import com.newrelic.agent.config.JarResource; +import com.newrelic.agent.config.JavaVersionUtils; import com.newrelic.agent.core.CoreService; import com.newrelic.agent.core.CoreServiceImpl; import com.newrelic.agent.logging.AgentLogManager; @@ -31,8 +35,16 @@ import java.lang.instrument.Instrumentation; import java.net.URL; import java.text.MessageFormat; -import java.util.*; -import java.util.jar.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.ResourceBundle; +import java.util.jar.Attributes; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; import java.util.logging.Level; import java.util.regex.Pattern; @@ -51,7 +63,6 @@ public final class Agent { private static final String NEWRELIC_BOOTSTRAP = "newrelic-bootstrap"; private static final String AGENT_ENABLED_PROPERTY = "newrelic.config.agent_enabled"; - private static final boolean DEBUG = Boolean.getBoolean("newrelic.debug"); private static final String VERSION = Agent.initVersion(); private static long agentPremainTime; @@ -73,7 +84,7 @@ private static String initVersion() { } public static boolean isDebugEnabled() { - return DEBUG; + return DebugFlag.DEBUG; } private static volatile boolean canFastPath = true; @@ -310,7 +321,8 @@ public static long getAgentPremainTimeInMillis() { private static void recordPremainTime(StatsService statsService, long startTime) { agentPremainTime = System.currentTimeMillis() - startTime; LOG.log(Level.INFO, "Premain startup complete in {0}ms", agentPremainTime); - statsService.doStatsWork(StatsWorks.getRecordResponseTimeWork(MetricNames.SUPPORTABILITY_TIMING_PREMAIN, agentPremainTime), MetricNames.SUPPORTABILITY_TIMING_PREMAIN); + statsService.doStatsWork(StatsWorks.getRecordResponseTimeWork(MetricNames.SUPPORTABILITY_TIMING_PREMAIN, agentPremainTime), + MetricNames.SUPPORTABILITY_TIMING_PREMAIN); Map environmentInfo = ImmutableMap.builder() .put("Duration", agentPremainTime) @@ -336,7 +348,7 @@ private static void recordPremainTime(StatsService statsService, long startTime) private static void recordAgentVersion(StatsService statsService) { statsService.doStatsWork( StatsWorks.getIncrementCounterWork(MessageFormat.format(MetricNames.SUPPORTABILITY_JAVA_AGENTVERSION, getVersion()), 1), - MetricNames.SUPPORTABILITY_JAVA_AGENTVERSION ); + MetricNames.SUPPORTABILITY_JAVA_AGENTVERSION); } /** diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/DebugFlag.java b/newrelic-agent/src/main/java/com/newrelic/agent/DebugFlag.java index 7c9775cdad..2b83c05717 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/DebugFlag.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/DebugFlag.java @@ -11,4 +11,10 @@ public class DebugFlag { public static final AtomicBoolean tokenEnabled = new AtomicBoolean(); + + // This replaces a previous check that duplicated code in several places. We need to check the debug flag in AgentJarHelper before the Agent class has been loaded. + // This flag cannot be set via newrelic.yml (AgentConfigImpl) because a ServiceManager and ConfigService have not been initialized for the earliest checks + // for the debug setting. + public static final boolean DEBUG = Boolean.getBoolean("newrelic.debug") || Boolean.parseBoolean(System.getenv("NEWRELIC_DEBUG")); + } diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentConfigImpl.java b/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentConfigImpl.java index 7c36b2341a..a955360667 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentConfigImpl.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentConfigImpl.java @@ -9,6 +9,7 @@ import com.google.common.base.Joiner; import com.newrelic.agent.Agent; +import com.newrelic.agent.DebugFlag; import com.newrelic.agent.transaction.TransactionNamingScheme; import com.newrelic.agent.transport.DataSenderImpl; @@ -69,7 +70,6 @@ public class AgentConfigImpl extends BaseConfig implements AgentConfig { public static final String MAX_STACK_TRACE_LINES = "max_stack_trace_lines"; public static final String METRIC_INGEST_URI = "metric_ingest_uri"; public static final String EVENT_INGEST_URI = "event_ingest_uri"; - public static final String DEBUG = "newrelic.debug"; public static final String METRIC_DEBUG = "metric_debug"; public static final String PLATFORM_INFORMATION_ENABLED = "platform_information_enabled"; public static final String PORT = "port"; @@ -293,7 +293,7 @@ private AgentConfigImpl(Map props) { putForDataSend = getProperty(PUT_FOR_DATA_SEND_PROPERTY, DEFAULT_PUT_FOR_DATA_SEND_ENABLED); isApdexTSet = getProperty(APDEX_T) != null; apdexTInMillis = (long) (getDoubleProperty(APDEX_T, DEFAULT_APDEX_T) * 1000L); - debug = Boolean.getBoolean(DEBUG); + debug = DebugFlag.DEBUG; metricDebug = initMetricDebugConfig(); enabled = getProperty(ENABLED, DEFAULT_ENABLED) && getProperty(AGENT_ENABLED, DEFAULT_ENABLED); experimentalRuntime = allowExperimentalRuntimeVersions(); diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentJarHelper.java b/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentJarHelper.java index d9072e3f74..14acf31aab 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentJarHelper.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/config/AgentJarHelper.java @@ -7,6 +7,8 @@ package com.newrelic.agent.config; +import com.newrelic.agent.DebugFlag; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -223,12 +225,9 @@ public static String getAgentJarAttribute(String name) { } } - // The "newrelic.debug" flag redirects all Agent logging to the standard output. Unfortunately, - // we haven't initialized the Agent yet, so we cannot check it in the usual low-cost way by - // calling Agent.isDebugEnabled(). So we duplicate the functionality here for use in a few cases. private static final boolean isNewRelicDebug() { - final String newrelicDebug = "newrelic.debug"; - return System.getProperty(newrelicDebug) != null && Boolean.getBoolean(newrelicDebug); + + return DebugFlag.DEBUG; } // Use of this method should be limited to serious error cases that would cause the Agent to diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/config/ConfigFileHelper.java b/newrelic-agent/src/main/java/com/newrelic/agent/config/ConfigFileHelper.java index 4dd27e3fde..963e660afb 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/config/ConfigFileHelper.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/config/ConfigFileHelper.java @@ -7,6 +7,8 @@ package com.newrelic.agent.config; +import com.newrelic.agent.DebugFlag; + import java.io.File; import java.text.MessageFormat; @@ -20,7 +22,6 @@ public class ConfigFileHelper { private static final String CONFIG_FILE_PROPERTY = "newrelic.config.file"; private static final String NEW_RELIC_HOME_DIRECTORY_PROPERTY = "newrelic.home"; private static final String NEW_RELIC_HOME_DIRECTORY_ENVIRONMENT_VARIABLE = "NEWRELIC_HOME"; - private static final String NEW_RELIC_DEBUG_PROPERTY = "newrelic.debug"; private static final String[] SEARCH_DIRECTORIES = { ".", "conf", "config", "etc" }; /** @@ -36,7 +37,7 @@ public static File findConfigFile() { File parentDir = getNewRelicDirectory(); if (parentDir != null) { - if (Boolean.getBoolean(NEW_RELIC_DEBUG_PROPERTY)) { + if (DebugFlag.DEBUG) { System.err.println(MessageFormat.format("New Relic home directory: {0}", parentDir)); } } @@ -149,7 +150,7 @@ private static File findHomeDirectoryFromEnvironmentVariable() { private static File findConfigFile(File parentDirectory) { for (String searchDir : SEARCH_DIRECTORIES) { File configDir = new File(parentDirectory, searchDir); - if (Boolean.getBoolean(NEW_RELIC_DEBUG_PROPERTY)) { + if (DebugFlag.DEBUG) { System.err.println(MessageFormat.format("Searching for New Relic configuration in directory {0}", configDir)); } if (configDir.exists()) {