Skip to content

Commit

Permalink
NR-66564 JFR toggle Config Listener (#1124)
Browse files Browse the repository at this point in the history
* monitor config changes for JFR

* additional tests

* NR-66564 Add JFR Stopped supportability metric
  • Loading branch information
jtduffy authored Jan 19, 2023
1 parent 4994b85 commit 1d94787
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ public class MetricNames {

// JFR Service
public static final String SUPPORTABILITY_JFR_SERVICE_STARTED_SUCCESS = "Supportability/JfrService/Started/Success";
public static final String SUPPORTABILITY_JFR_SERVICE_STOPPED_SUCCESS = "Supportability/JfrService/Stopped/Success";
public static final String SUPPORTABILITY_JFR_SERVICE_STARTED_FAIL = "Supportability/JfrService/Started/Fail";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public interface JfrConfig {
*/
boolean isEnabled();

/**
* Set the JFR service enabled flag
*
* @param enabled the desired enabled flag value
*/
void setEnabled(boolean enabled);

/**
* Check if audit_logging is enabled for the JFR service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JfrConfigImpl extends BaseConfig implements JfrConfig {
public static final Boolean AUDIT_LOGGING_DEFAULT = Boolean.FALSE;
public static final Boolean USE_LICENSE_KEY_DEFAULT = Boolean.TRUE;

private final boolean isEnabled;
private boolean isEnabled;

public JfrConfigImpl(Map<String, Object> pProps) {
super(pProps, SYSTEM_PROPERTY_ROOT);
Expand All @@ -38,6 +38,11 @@ public boolean isEnabled() {
return isEnabled;
}

@Override
public void setEnabled(boolean enabled) {
this.isEnabled = enabled;
}

@Override
public boolean auditLoggingEnabled() {
return getProperty(AUDIT_LOGGING, AUDIT_LOGGING_DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.newrelic.agent.MetricNames;
import com.newrelic.agent.ThreadService;
import com.newrelic.agent.config.AgentConfig;
import com.newrelic.agent.config.AgentConfigListener;
import com.newrelic.agent.config.JfrConfig;
import com.newrelic.agent.service.AbstractService;
import com.newrelic.agent.service.ServiceFactory;
Expand All @@ -29,7 +30,7 @@
import static com.newrelic.jfr.daemon.SetupUtils.buildCommonAttributes;
import static com.newrelic.jfr.daemon.SetupUtils.buildUploader;

public class JfrService extends AbstractService {
public class JfrService extends AbstractService implements AgentConfigListener {

private final JfrConfig jfrConfig;
private final AgentConfig defaultAgentConfig;
Expand All @@ -39,6 +40,7 @@ public JfrService(JfrConfig jfrConfig, AgentConfig defaultAgentConfig) {
super(JfrService.class.getSimpleName());
this.jfrConfig = jfrConfig;
this.defaultAgentConfig = defaultAgentConfig;
ServiceFactory.getConfigService().addIAgentConfigListener(this);
}

@Override
Expand Down Expand Up @@ -94,6 +96,8 @@ public final boolean isEnabled() {

@Override
protected void doStop() {
NewRelic.getAgent().getMetricAggregator().incrementCounter(MetricNames.SUPPORTABILITY_JFR_SERVICE_STOPPED_SUCCESS);

if (jfrController != null) {
jfrController.shutdown();
}
Expand Down Expand Up @@ -128,4 +132,20 @@ DaemonConfig buildDaemonConfig() {
.proxyPassword(defaultAgentConfig.getProxyPassword());
return builder.build();
}

@Override
public void configChanged(String appName, AgentConfig agentConfig) {
boolean newJfrEnabledVal = agentConfig.getJfrConfig().isEnabled();

if (newJfrEnabledVal != jfrConfig.isEnabled()) {
Agent.LOG.log(Level.INFO, "JFR enabled flag changed to {0}", newJfrEnabledVal);
jfrConfig.setEnabled(newJfrEnabledVal);

if (newJfrEnabledVal) {
doStart();
} else {
doStop();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import com.newrelic.agent.RPMServiceManager;
import com.newrelic.agent.ThreadService;
import com.newrelic.agent.config.AgentConfig;
import com.newrelic.agent.config.ConfigService;
import com.newrelic.agent.config.JfrConfig;
import com.newrelic.agent.service.ServiceFactory;
import com.newrelic.agent.service.ServiceManager;
import com.newrelic.agent.service.ServiceManagerImpl;
import com.newrelic.jfr.ThreadNameNormalizer;
import com.newrelic.jfr.daemon.DaemonConfig;
import com.newrelic.jfr.daemon.JfrRecorderException;
Expand All @@ -15,6 +18,7 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;

import static com.newrelic.agent.config.AgentConfigImpl.DEFAULT_EVENT_INGEST_URI;
Expand All @@ -26,6 +30,7 @@
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
Expand All @@ -43,6 +48,10 @@ public class JfrServiceTest {
@Before
public void before() {
MockitoAnnotations.openMocks(this);

MockServiceManager manager = new MockServiceManager();
ServiceFactory.setServiceManager(manager);

when(jfrConfig.useLicenseKey()).thenReturn(true);
when(agentConfig.getApplicationName()).thenReturn("test_app_name");
when(agentConfig.getMetricIngestUri()).thenReturn(DEFAULT_METRIC_INGEST_URI);
Expand All @@ -52,7 +61,6 @@ public void before() {
.thenReturn(ThreadNameNormalizer.DEFAULT_PATTERN);
}


@Test
public void daemonConfigBuiltCorrect() {
JfrService jfrService = new JfrService(jfrConfig, agentConfig);
Expand Down Expand Up @@ -122,4 +130,19 @@ public void jfrLoopDoesStart() {
fail("Should not have thrown any exception");
}
}

@Test
public void configChanged_RespectsChangedSetting() {
JfrService jfrService = new JfrService(jfrConfig, agentConfig);
JfrService spyJfr = spy(jfrService);
AgentConfig newAgentConfig = mock(AgentConfig.class);
JfrConfig newJfrConfig = mock(JfrConfig.class);

when(jfrConfig.isEnabled()).thenReturn(true);
when(newAgentConfig.getJfrConfig()).thenReturn(newJfrConfig);
when(newJfrConfig.isEnabled()).thenReturn(false);

spyJfr.configChanged("my-app", newAgentConfig);
verify(spyJfr).doStop();
}
}

0 comments on commit 1d94787

Please sign in to comment.