Search in sources :

Example 11 with IRPMService

use of com.newrelic.agent.IRPMService in project newrelic-java-agent by newrelic.

the class EnvironmentHolder method setupEnvironment.

public void setupEnvironment() throws Exception {
    rpmService.setIsConnected(true);
    IRPMService originalRpmService = originalServiceManager.getRPMServiceManager().getRPMService();
    rpmService.setApplicationName(originalRpmService.getApplicationName());
    AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(environmentHolderSettingsGenerator.generateSettings());
    Map<String, Object> properties = ImmutableMap.<String, Object>of("agent_config", ((BaseConfig) agentConfig).getProperties(), "collect_span_events", true);
    ((ConfigServiceImpl) ServiceFactory.getConfigService()).connected(rpmService, properties);
    if (agentConfig.getDistributedTracingConfig().isEnabled()) {
        ((DistributedTraceServiceImpl) ServiceFactory.getDistributedTraceService()).connected(rpmService, agentConfig);
    }
    config = agentConfig;
    // Set up now that new environment is in place
    ServiceFactory.getTransactionService().addTransactionListener(transactionList);
    transactionList.clear();
    // We have to actually start the harvest service, even though typically tests call harvestNow(). This means
    // the test will conflict with a real harvest if it takes more than about 30 seconds, possibly leading to
    // flickers. We should probably change the design of the harvest service. Better yet, we should implement a
    // better way to have custom configurations in functional tests and get rid of this messy class completely.
    ServiceFactory.getHarvestService().startHarvest(rpmService);
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) ConfigServiceImpl(com.newrelic.agent.config.ConfigServiceImpl) IRPMService(com.newrelic.agent.IRPMService)

Example 12 with IRPMService

use of com.newrelic.agent.IRPMService in project newrelic-java-agent by newrelic.

the class ConfigServiceTest method fileChangeAndThenConnectDoesActuallyChangeConfig.

@Test
public void fileChangeAndThenConnectDoesActuallyChangeConfig() throws IOException {
    ServiceManager mockServiceManager = mock(ServiceManager.class);
    ClassTransformerService mockClassTransformerService = mock(ClassTransformerService.class);
    when(mockServiceManager.getClassTransformerService()).thenReturn(mockClassTransformerService);
    ServiceFactory.setServiceManager(mockServiceManager);
    String appName = "Unit Test";
    Map<String, Object> originalMap = ImmutableMap.<String, Object>of("app_name", appName);
    File mockConfigFile = File.createTempFile("ConfigServiceTest", null);
    try (OutputStream os = new FileOutputStream(mockConfigFile)) {
        os.write(JSONObject.toJSONString(Collections.singletonMap("common", originalMap)).getBytes());
    }
    assertTrue(mockConfigFile.setLastModified(15L));
    AgentConfig originalConfig = AgentConfigImpl.createAgentConfig(originalMap);
    final Boolean[] circuitBreakerSetting = new Boolean[] { null };
    assertTrue("Default circuitbreaker was expected to be true; it was apparently not.", originalConfig.getCircuitBreakerConfig().isEnabled());
    ConfigServiceImpl target = new ConfigServiceImpl(originalConfig, mockConfigFile, originalMap, false);
    target.addIAgentConfigListener(new AgentConfigListener() {

        @Override
        public void configChanged(String appName, AgentConfig agentConfig) {
            circuitBreakerSetting[0] = agentConfig.getCircuitBreakerConfig().isEnabled();
        }
    });
    // step 1: modify the file.
    try (OutputStream os = new FileOutputStream(mockConfigFile)) {
        os.write(JSONObject.toJSONString(Collections.singletonMap("common", ImmutableMap.of("app_name", appName, "circuitbreaker", Collections.singletonMap("enabled", false)))).getBytes());
    }
    assertTrue("unable to set the last modified time on the mock config file.", mockConfigFile.setLastModified(System.currentTimeMillis()));
    target.afterHarvest(appName);
    assertNotNull("circuitbreaker setting should have been set; it was not", circuitBreakerSetting[0]);
    assertFalse("circuitbreaker setting has not changed from true to false; it should have!", circuitBreakerSetting[0]);
    circuitBreakerSetting[0] = null;
    // step 2: trigger connect.
    IRPMService mockRPMService = mock(IRPMService.class);
    when(mockRPMService.getApplicationName()).thenReturn(appName);
    target.connected(mockRPMService, Collections.<String, Object>emptyMap());
    // this should not have reverted to the original contents.
    assertNotNull("circuitbreaker setting should have been set; it was not", circuitBreakerSetting[0]);
    assertFalse("circuitbreaker setting has changed from false; it should not have!", circuitBreakerSetting[0]);
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) MockServiceManager(com.newrelic.agent.MockServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) ClassTransformerService(com.newrelic.agent.instrumentation.ClassTransformerService) FileOutputStream(java.io.FileOutputStream) IRPMService(com.newrelic.agent.IRPMService) JSONObject(org.json.simple.JSONObject) File(java.io.File) Test(org.junit.Test)

Example 13 with IRPMService

use of com.newrelic.agent.IRPMService in project newrelic-java-agent by newrelic.

the class StartProfilerCommandTest method validArgsThreadProfilerV2NullDefaults.

@Test
public void validArgsThreadProfilerV2NullDefaults() throws CommandException {
    IRPMService rpmService = null;
    final long profileId = 1L;
    final long duration = 60000L;
    final long samplePeriod = 500L;
    final boolean onlyRunnableThreads = true;
    final boolean onlyRequestThreads = true;
    final String profilerFormat = "v2";
    final Object profileAgentCode = null;
    Map<String, Object> args = new HashMap<String, Object>() {

        private static final long serialVersionUID = 1L;

        {
            put("profile_id", profileId);
            put("sample_period", samplePeriod);
            put("duration", duration);
            put("only_runnable_threads", onlyRunnableThreads);
            put("only_request_threads", onlyRequestThreads);
            put("profiler_format", profilerFormat);
            put("profile_agent_code", profileAgentCode);
        }
    };
    startProfilerCommand.process(rpmService, args);
    Assert.assertEquals(duration * 1000L, profilerParameters.getDurationInMillis().longValue());
    Assert.assertEquals(samplePeriod * 1000L, profilerParameters.getSamplePeriodInMillis().longValue());
    Assert.assertEquals(profileId, profilerParameters.getProfileId().longValue());
    Assert.assertEquals(onlyRunnableThreads, profilerParameters.isRunnablesOnly());
    Assert.assertEquals(onlyRequestThreads, profilerParameters.isOnlyRequestThreads());
    Assert.assertEquals(false, profilerParameters.isProfileAgentThreads());
    Assert.assertEquals(profilerFormat, profilerParameters.getProfilerFormat());
    Assert.assertNull(profilerParameters.getKeyTransaction());
}
Also used : HashMap(java.util.HashMap) IRPMService(com.newrelic.agent.IRPMService) Test(org.junit.Test)

Example 14 with IRPMService

use of com.newrelic.agent.IRPMService in project newrelic-java-agent by newrelic.

the class StartProfilerCommandTest method badSamplePeriod.

@Test(expected = CommandException.class)
public void badSamplePeriod() throws CommandException {
    IRPMService rpmService = null;
    Map<String, Object> args = new HashMap<String, Object>() {

        private static final long serialVersionUID = 1L;

        {
            put("profile_id", 1L);
            put("duration", 120.0f);
            put("sample_period", "dog");
        }
    };
    startProfilerCommand.process(rpmService, args);
}
Also used : HashMap(java.util.HashMap) IRPMService(com.newrelic.agent.IRPMService) Test(org.junit.Test)

Example 15 with IRPMService

use of com.newrelic.agent.IRPMService in project newrelic-java-agent by newrelic.

the class StartProfilerCommandTest method samplePeriodGreaterThanDuration.

@Test(expected = CommandException.class)
public void samplePeriodGreaterThanDuration() throws CommandException {
    IRPMService rpmService = null;
    Map<String, Object> args = new HashMap<String, Object>() {

        private static final long serialVersionUID = 1L;

        {
            put("profile_id", 1L);
            put("duration", 1000L);
            put("sample_period", 2000L);
        }
    };
    startProfilerCommand.process(rpmService, args);
}
Also used : HashMap(java.util.HashMap) IRPMService(com.newrelic.agent.IRPMService) Test(org.junit.Test)

Aggregations

IRPMService (com.newrelic.agent.IRPMService)17 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 AgentConfig (com.newrelic.agent.config.AgentConfig)4 MockServiceManager (com.newrelic.agent.MockServiceManager)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)1 RPMServiceManager (com.newrelic.agent.RPMServiceManager)1 TransactionService (com.newrelic.agent.TransactionService)1 ConfigServiceImpl (com.newrelic.agent.config.ConfigServiceImpl)1 ErrorService (com.newrelic.agent.errors.ErrorService)1 ClassTransformerService (com.newrelic.agent.instrumentation.ClassTransformerService)1 ServiceManager (com.newrelic.agent.service.ServiceManager)1 DistributedTraceServiceImpl (com.newrelic.agent.tracing.DistributedTraceServiceImpl)1 HttpError (com.newrelic.agent.transport.HttpError)1 JSONException (com.newrelic.agent.util.JSONException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1