Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include debug as env #890

Merged
merged 4 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions newrelic-agent/src/main/java/com/newrelic/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -73,7 +84,7 @@ private static String initVersion() {
}

public static boolean isDebugEnabled() {
return DEBUG;
return DebugFlag.DEBUG;
}

private static volatile boolean canFastPath = true;
Expand Down Expand Up @@ -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<String, Object> environmentInfo = ImmutableMap.<String, Object>builder()
.put("Duration", agentPremainTime)
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -293,7 +293,7 @@ private AgentConfigImpl(Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.newrelic.agent.config;

import com.newrelic.agent.DebugFlag;

import java.io.File;
import java.text.MessageFormat;

Expand All @@ -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" };

/**
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -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()) {
Expand Down