Search in sources :

Example 21 with ServiceManager

use of com.newrelic.agent.service.ServiceManager in project newrelic-java-agent by newrelic.

the class ClassTransformerConfigImplTest method testSpringPointcutDisabledDefaultEnabled.

@Test
public void testSpringPointcutDisabledDefaultEnabled() {
    Map<String, Object> configuration = new HashMap<>();
    configuration.put("class_transformer:instrumentation_default:enabled", true);
    configuration.put("enable_spring_tracing", false);
    configuration.put("app_name", "Unit Test");
    configuration = buildConfigMap(configuration);
    AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(configuration);
    ConfigService configService = ConfigServiceFactory.createConfigService(agentConfig, Collections.<String, Object>emptyMap());
    ServiceManager serviceManager = new MockServiceManager(configService);
    ServiceFactory.setServiceManager(serviceManager);
    SpringPointCut springPointCut = new SpringPointCut(null, agentConfig);
    assertFalse(springPointCut.isEnabled());
}
Also used : SpringPointCut(com.newrelic.agent.instrumentation.pointcuts.frameworks.spring.SpringPointCut) HashMap(java.util.HashMap) MockServiceManager(com.newrelic.agent.MockServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) MockServiceManager(com.newrelic.agent.MockServiceManager) Test(org.junit.Test)

Example 22 with ServiceManager

use of com.newrelic.agent.service.ServiceManager 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 23 with ServiceManager

use of com.newrelic.agent.service.ServiceManager in project newrelic-java-agent by newrelic.

the class TransactionEventsServiceTest method testTransactionEventFasterHarvest.

@Test
public void testTransactionEventFasterHarvest() throws Exception {
    Map<String, Object> config = new HashMap<>();
    config.put(AgentConfigImpl.APP_NAME, APP_NAME);
    EventTestHelper.setAppName(APP_NAME);
    EventTestHelper.createServiceManager(config);
    service = new TransactionEventsService(mock(TransactionDataToDistributedTraceIntrinsics.class));
    ((MockServiceManager) ServiceFactory.getServiceManager()).setTransactionEventsService(service);
    environmentService = new EnvironmentServiceImpl();
    ((MockServiceManager) ServiceFactory.getServiceManager()).setEnvironmentService(environmentService);
    ServiceManager serviceManager = spy(ServiceFactory.getServiceManager());
    ServiceFactory.setServiceManager(serviceManager);
    HarvestServiceImpl harvestService = spy(new HarvestServiceImpl());
    doReturn(harvestService).when(serviceManager).getHarvestService();
    doReturn(0L).when(harvestService).getInitialDelay();
    service.addHarvestableToService(APP_NAME);
    service.configureHarvestables(60, 3);
    assertEquals(3, service.getMaxSamplesStored());
    service.doStart();
    Map<String, Object> connectionInfo = new HashMap<>();
    Map<String, Object> eventHarvest = new HashMap<>();
    Map<String, Object> harvestLimits = new HashMap<>();
    // 5 is the lowest allowable value
    eventHarvest.put("report_period_ms", 5000L);
    eventHarvest.put("harvest_limits", harvestLimits);
    harvestLimits.put("analytic_event_data", 100L);
    connectionInfo.put("event_harvest_config", eventHarvest);
    harvestService.startHarvestables(ServiceFactory.getRPMService(), AgentConfigImpl.createAgentConfig(connectionInfo));
    getEventData(APP_NAME);
    Thread.sleep(500);
    DistributedSamplingPriorityQueue<TransactionEvent> currentEventData = createAndSendTransaction();
    assertEquals(1, currentEventData.size());
    Thread.sleep(6000);
    checkForEvent();
    assertEquals(1, currentEventData.size());
    createAndSendTransaction();
    Thread.sleep(6000);
    checkForEvent();
}
Also used : HashMap(java.util.HashMap) EnvironmentServiceImpl(com.newrelic.agent.environment.EnvironmentServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) MockServiceManager(com.newrelic.agent.MockServiceManager) HarvestServiceImpl(com.newrelic.agent.HarvestServiceImpl) Test(org.junit.Test)

Example 24 with ServiceManager

use of com.newrelic.agent.service.ServiceManager in project newrelic-java-agent by newrelic.

the class LogSenderServiceImplTest method createService.

private static LogSenderServiceImpl createService(Map<String, Object> config) throws Exception {
    config = new HashMap<>(config);
    ServiceManager serviceManager = mock(ServiceManager.class);
    when(serviceManager.getHarvestService()).thenReturn(harvestService);
    when(serviceManager.getStatsService()).thenReturn(statsService);
    when(serviceManager.getTransactionService()).thenReturn(txService);
    when(serviceManager.getRPMServiceManager()).thenReturn(Mockito.mock(RPMServiceManager.class));
    when(serviceManager.getRPMServiceManager().getRPMService()).thenReturn(Mockito.mock(RPMService.class));
    when(serviceManager.getConfigService()).thenReturn(Mockito.mock(ConfigService.class));
    when(serviceManager.getConfigService().getDefaultAgentConfig()).thenReturn(AgentConfigImpl.createAgentConfig(config));
    when(serviceManager.getRPMServiceManager().getRPMService().getApplicationName()).thenReturn(appName);
    ServiceFactory.setServiceManager(serviceManager);
    Transaction transaction = Mockito.mock(Transaction.class);
    when(txService.getTransaction(false)).thenReturn(transaction);
    LogSenderServiceImpl logSenderService = new LogSenderServiceImpl();
    when(ServiceFactory.getServiceManager().getLogSenderService()).thenReturn(logSenderService);
    logSenderService.start();
    return logSenderService;
}
Also used : ConfigService(com.newrelic.agent.config.ConfigService) Transaction(com.newrelic.agent.Transaction) RPMServiceManager(com.newrelic.agent.RPMServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) MockRPMService(com.newrelic.agent.MockRPMService) RPMService(com.newrelic.agent.RPMService) RPMServiceManager(com.newrelic.agent.RPMServiceManager)

Example 25 with ServiceManager

use of com.newrelic.agent.service.ServiceManager in project newrelic-java-agent by newrelic.

the class Agent method tryToInitializeServiceManager.

private static boolean tryToInitializeServiceManager(Instrumentation inst) {
    try {
        CoreService coreService = new CoreServiceImpl(inst);
        ConfigService configService = ConfigServiceFactory.createConfigService(Agent.LOG, System.getProperty("newrelic.checkconfig") != null);
        ServiceManager serviceManager = new ServiceManagerImpl(coreService, configService);
        ServiceFactory.setServiceManager(serviceManager);
        if (isLicenseKeyEmpty(serviceManager.getConfigService().getDefaultAgentConfig().getLicenseKey())) {
            LOG.error("license_key is empty in the config. Not starting New Relic Agent.");
            return false;
        }
        if (!serviceManager.getConfigService().getDefaultAgentConfig().isAgentEnabled()) {
            LOG.warning("agent_enabled is false in the config. Not starting New Relic Agent.");
            return false;
        }
        // Now that we know the agent is enabled, add the ApiClassTransformer
        BootstrapLoader.forceCorrectNewRelicApi(inst);
        // init problem classes before class transformer service is active
        InitProblemClasses.loadInitialClasses();
    } catch (ForceDisconnectException e) {
        /* Note: Our use of ForceDisconnectException is a bit misleading here as we haven't even tried to connect
             * to RPM at this point (that happens a few lines down when we call serviceManager.start()). This exception
             * comes from ConfigServiceFactory when it attempts to validate the local yml and finds that both HSM and
             * LASP are enabled. The LASP spec says in this scenario that "Shutdown will follow the behavior of the
             * ForceDisconnectException response from "New Relic." Not specifically that we should throw ForceDisconnectException.
             * Perhaps we should throw a different, more accurately named exception, that simply has the same behavior
             * as ForceDisconnectException as it will be replaced by a 410 response code in Protocol 17.
             */
        LOG.log(Level.SEVERE, e.getMessage());
        return false;
    } catch (Throwable t) {
        // this is the last point where we can stop the agent gracefully if something has gone wrong.
        LOG.log(Level.SEVERE, t, "Unable to start the New Relic Agent. Your application will continue to run but it will not be monitored.");
        return false;
    }
    return true;
}
Also used : ServiceManager(com.newrelic.agent.service.ServiceManager) ServiceManagerImpl(com.newrelic.agent.service.ServiceManagerImpl) CoreService(com.newrelic.agent.core.CoreService) CoreServiceImpl(com.newrelic.agent.core.CoreServiceImpl)

Aggregations

ServiceManager (com.newrelic.agent.service.ServiceManager)31 MockServiceManager (com.newrelic.agent.MockServiceManager)18 Test (org.junit.Test)17 HashMap (java.util.HashMap)12 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)5 ConfigService (com.newrelic.agent.config.ConfigService)5 MockRPMService (com.newrelic.agent.MockRPMService)4 AgentConfig (com.newrelic.agent.config.AgentConfig)4 PointCutConfiguration (com.newrelic.agent.instrumentation.PointCutConfiguration)4 SpringPointCut (com.newrelic.agent.instrumentation.pointcuts.frameworks.spring.SpringPointCut)4 HarvestServiceImpl (com.newrelic.agent.HarvestServiceImpl)3 RPMServiceManager (com.newrelic.agent.RPMServiceManager)3 ServiceManagerImpl (com.newrelic.agent.service.ServiceManagerImpl)2 JSONObject (org.json.simple.JSONObject)2 After (org.junit.After)2 ForceDisconnectException (com.newrelic.agent.ForceDisconnectException)1 IRPMService (com.newrelic.agent.IRPMService)1 RPMService (com.newrelic.agent.RPMService)1 Transaction (com.newrelic.agent.Transaction)1 BrowserServiceImpl (com.newrelic.agent.browser.BrowserServiceImpl)1