Search in sources :

Example 1 with ClassTransformerService

use of com.newrelic.agent.instrumentation.ClassTransformerService 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 2 with ClassTransformerService

use of com.newrelic.agent.instrumentation.ClassTransformerService in project newrelic-java-agent by newrelic.

the class ExtensionServiceTest method setUpAgent.

@Before
public void setUpAgent() throws Exception {
    Map<String, Object> configMap = new HashMap<>();
    configMap.put(AgentConfigImpl.APP_NAME, EXTENSION_NAME);
    AgentConfig config = AgentConfigImpl.createAgentConfig(configMap);
    configService = ConfigServiceFactory.createConfigService(config, Collections.<String, Object>emptyMap());
    serviceManager = new MockServiceManager(configService);
    ServiceFactory.setServiceManager(serviceManager);
    final InstrumentationProxy instrumentationProxy = Mockito.mock(InstrumentationProxy.class);
    serviceManager.setCoreService(new MockCoreService() {

        @Override
        public InstrumentationProxy getInstrumentation() {
            return instrumentationProxy;
        }
    });
    Mockito.when(instrumentationProxy.isRetransformClassesSupported()).thenReturn(true);
    Mockito.when(instrumentationProxy.getAllLoadedClasses()).thenReturn(new Class[] {});
    extensionService = new ExtensionService(configService, ExtensionsLoadedListener.NOOP);
    serviceManager.setExtensionService(extensionService);
    serviceManager.setJmxService(Mockito.mock(JmxService.class));
    ClassTransformerService classTransformerService = serviceManager.getClassTransformerService();
    ClassRetransformer mockRetransformer = Mockito.mock(ClassRetransformer.class);
    Mockito.when(classTransformerService.getLocalRetransformer()).thenReturn(mockRetransformer);
    InstrumentationContextManager mockContextManager = Mockito.mock(InstrumentationContextManager.class);
    Mockito.when(classTransformerService.getContextManager()).thenReturn(mockContextManager);
    ClassWeaverService mockWeaverService = Mockito.mock(ClassWeaverService.class);
    Mockito.when(mockContextManager.getClassWeaverService()).thenReturn(mockWeaverService);
    Mockito.when(mockWeaverService.reloadExternalWeavePackages(Mockito.<File>anyCollection(), Mockito.<File>anyCollection())).thenReturn(new Runnable() {

        @Override
        public void run() {
        }
    });
}
Also used : ClassRetransformer(com.newrelic.agent.instrumentation.custom.ClassRetransformer) HashMap(java.util.HashMap) InstrumentationProxy(com.newrelic.agent.InstrumentationProxy) JmxService(com.newrelic.agent.jmx.JmxService) InstrumentationContextManager(com.newrelic.agent.instrumentation.context.InstrumentationContextManager) AgentConfig(com.newrelic.agent.config.AgentConfig) MockServiceManager(com.newrelic.agent.MockServiceManager) ClassTransformerService(com.newrelic.agent.instrumentation.ClassTransformerService) MockCoreService(com.newrelic.agent.MockCoreService) ClassWeaverService(com.newrelic.agent.instrumentation.weaver.ClassWeaverService) Before(org.junit.Before)

Example 3 with ClassTransformerService

use of com.newrelic.agent.instrumentation.ClassTransformerService in project newrelic-java-agent by newrelic.

the class JarExtensionTest method javaagentExtension.

@Test
public void javaagentExtension() throws IOException {
    ServiceFactory.setServiceManager(Mockito.mock(ServiceManager.class));
    ClassTransformerService classTransformerService = Mockito.mock(ClassTransformerService.class);
    Mockito.when(ServiceFactory.getServiceManager().getClassTransformerService()).thenReturn(classTransformerService);
    Instrumentation instrumentation = Mockito.mock(Instrumentation.class);
    Mockito.when(classTransformerService.getExtensionInstrumentation()).thenReturn(instrumentation);
    AgentBridge.agent = Mockito.mock(Agent.class);
    Logger logger = Mockito.mock(Logger.class);
    Mockito.when(AgentBridge.agent.getLogger()).thenReturn(logger);
    JarExtension.create(Mockito.mock(IAgentLogger.class), Mockito.mock(ExtensionParsers.class), createJavaAgentExtension());
    // Verify that our premain class was invoked
    Mockito.verify(logger, Mockito.times(1)).log(Level.INFO, "My cool extension started! {0}", instrumentation);
}
Also used : Agent(com.newrelic.agent.bridge.Agent) ServiceManager(com.newrelic.agent.service.ServiceManager) ClassTransformerService(com.newrelic.agent.instrumentation.ClassTransformerService) Instrumentation(java.lang.instrument.Instrumentation) IAgentLogger(com.newrelic.agent.logging.IAgentLogger) IAgentLogger(com.newrelic.agent.logging.IAgentLogger) Logger(com.newrelic.api.agent.Logger) Test(org.junit.Test)

Example 4 with ClassTransformerService

use of com.newrelic.agent.instrumentation.ClassTransformerService in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method createServiceManager.

private MockServiceManager createServiceManager(Map<String, Object> configMap) throws Exception {
    AgentConfig config = AgentConfigImpl.createAgentConfig(configMap);
    MockServiceManager serviceManager = new MockServiceManager();
    ServiceFactory.setServiceManager(serviceManager);
    ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap);
    serviceManager.setConfigService(configService);
    ThreadService threadService = new ThreadService();
    serviceManager.setThreadService(threadService);
    ClassTransformerService classTransformerService = Mockito.mock(ClassTransformerService.class);
    serviceManager.setClassTransformerService(classTransformerService);
    HarvestService harvestService = new MockHarvestService();
    serviceManager.setHarvestService(harvestService);
    TransactionService transactionService = new TransactionService();
    serviceManager.setTransactionService(transactionService);
    StatsService statsService = new StatsServiceImpl();
    serviceManager.setStatsService(statsService);
    DatabaseService dbService = new DatabaseService();
    serviceManager.setDatabaseService(dbService);
    EnvironmentService envService = new EnvironmentServiceImpl();
    serviceManager.setEnvironmentService(envService);
    SqlTraceService sqlTraceService = new SqlTraceServiceImpl();
    serviceManager.setSqlTraceService(sqlTraceService);
    MockCoreService agent = new MockCoreService();
    serviceManager.setCoreService(agent);
    serviceManager.setAttributesService(new AttributesService());
    TransactionTraceService transactionTraceService = new TransactionTraceService();
    serviceManager.setTransactionTraceService(transactionTraceService);
    DistributedTraceServiceImpl distributedTraceService = new DistributedTraceServiceImpl();
    serviceManager.setDistributedTraceService(distributedTraceService);
    TransactionDataToDistributedTraceIntrinsics transactionDataToDistributedTraceIntrinsics = new TransactionDataToDistributedTraceIntrinsics(distributedTraceService);
    serviceManager.setTransactionEventsService(new TransactionEventsService(transactionDataToDistributedTraceIntrinsics));
    MockRPMServiceManager rpmServiceManager = new MockRPMServiceManager();
    serviceManager.setRPMServiceManager(rpmServiceManager);
    MockRPMService rpmService = new MockRPMService();
    rpmService.setApplicationName(APP_NAME);
    rpmService.setEverConnected(true);
    rpmService.setErrorService(new ErrorServiceImpl(APP_NAME));
    rpmServiceManager.setRPMService(rpmService);
    configService.start();
    serviceManager.start();
    sqlTraceService.start();
    return serviceManager;
}
Also used : MockHarvestService(com.newrelic.agent.MockHarvestService) HarvestService(com.newrelic.agent.HarvestService) TransactionService(com.newrelic.agent.TransactionService) ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) StatsService(com.newrelic.agent.stats.StatsService) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) AttributesService(com.newrelic.agent.attributes.AttributesService) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) DatabaseService(com.newrelic.agent.database.DatabaseService) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) TransactionDataToDistributedTraceIntrinsics(com.newrelic.agent.service.analytics.TransactionDataToDistributedTraceIntrinsics) AgentConfig(com.newrelic.agent.config.AgentConfig) ThreadService(com.newrelic.agent.ThreadService) ConfigService(com.newrelic.agent.config.ConfigService) EnvironmentServiceImpl(com.newrelic.agent.environment.EnvironmentServiceImpl) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) ClassTransformerService(com.newrelic.agent.instrumentation.ClassTransformerService) TransactionEventsService(com.newrelic.agent.service.analytics.TransactionEventsService) MockHarvestService(com.newrelic.agent.MockHarvestService) MockCoreService(com.newrelic.agent.MockCoreService) EnvironmentService(com.newrelic.agent.environment.EnvironmentService) MockRPMService(com.newrelic.agent.MockRPMService)

Aggregations

ClassTransformerService (com.newrelic.agent.instrumentation.ClassTransformerService)4 MockServiceManager (com.newrelic.agent.MockServiceManager)3 MockCoreService (com.newrelic.agent.MockCoreService)2 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)2 AgentConfig (com.newrelic.agent.config.AgentConfig)2 ServiceManager (com.newrelic.agent.service.ServiceManager)2 Test (org.junit.Test)2 HarvestService (com.newrelic.agent.HarvestService)1 IRPMService (com.newrelic.agent.IRPMService)1 InstrumentationProxy (com.newrelic.agent.InstrumentationProxy)1 MockHarvestService (com.newrelic.agent.MockHarvestService)1 MockRPMService (com.newrelic.agent.MockRPMService)1 ThreadService (com.newrelic.agent.ThreadService)1 TransactionService (com.newrelic.agent.TransactionService)1 AttributesService (com.newrelic.agent.attributes.AttributesService)1 Agent (com.newrelic.agent.bridge.Agent)1 ConfigService (com.newrelic.agent.config.ConfigService)1 DatabaseService (com.newrelic.agent.database.DatabaseService)1 EnvironmentService (com.newrelic.agent.environment.EnvironmentService)1 EnvironmentServiceImpl (com.newrelic.agent.environment.EnvironmentServiceImpl)1