Search in sources :

Example 1 with ServiceManager

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

the class Agent method continuePremain.

/**
 * Called by the "real" premain() in the BootstrapAgent.
 */
@SuppressWarnings("unused")
public static void continuePremain(String agentArgs, Instrumentation inst, long startTime) {
    final LifecycleObserver lifecycleObserver = LifecycleObserver.createLifecycleObserver(agentArgs);
    if (!lifecycleObserver.isAgentSafe()) {
        return;
    }
    // This *MUST* be done first thing in the premain
    addMixinInterfacesToBootstrap(inst);
    if (ServiceFactory.getServiceManager() != null) {
        LOG.warning("New Relic Agent is already running! Check if more than one -javaagent switch is used on the command line.");
        lifecycleObserver.agentAlreadyRunning();
        return;
    }
    String enabled = System.getProperty(AGENT_ENABLED_PROPERTY);
    if (enabled != null && !Boolean.parseBoolean(enabled)) {
        LOG.warning("New Relic Agent is disabled by a system property.");
        return;
    }
    String jvmName = System.getProperty("java.vm.name");
    if (jvmName.contains("Oracle JRockit")) {
        String msg = MessageFormat.format("New Relic Agent {0} does not support the Oracle JRockit JVM (\"{1}\").", Agent.getVersion(), jvmName);
        LOG.warning(msg);
    }
    if (!tryToInitializeServiceManager(inst)) {
        return;
    }
    try {
        ServiceManager serviceManager = ServiceFactory.getServiceManager();
        // The following method will immediately configure the log so that the rest of our initialization sequence
        // is written to the newrelic_agent.log rather than to the console. Configuring the log also applies the
        // log_level setting from the newrelic.yml so debugging levels become available here, if so configured.
        serviceManager.start();
        lifecycleObserver.serviceManagerStarted(serviceManager);
        LOG.info(MessageFormat.format("New Relic Agent v{0} has started", Agent.getVersion()));
        if (System.getProperty("newrelic.bootstrap_classpath") != null) {
            // This is an obsolete system property that caused the entire Agent to be loaded on the bootstrap.
            LOG.info("The \"newrelic.bootstrap_classpath\" property is no longer used. Please remove it from your configuration.");
        }
        LOG.info("Agent class loader: " + AgentBridge.getAgent().getClass().getClassLoader());
        logAnyFilesFoundInEndorsedDirs();
        if (serviceManager.getConfigService().getDefaultAgentConfig().isStartupTimingEnabled()) {
            recordPremainTime(serviceManager.getStatsService(), startTime);
        }
        recordAgentVersion(serviceManager.getStatsService());
    } catch (Throwable t) {
        // There's no way to gracefully pull the agent out due to our bytecode modification and class structure changes (pointcuts).
        // We're likely to throw an exception into the user's app if we try to continue.
        String msg = "Unable to start New Relic Agent. Please remove -javaagent from your startup arguments and contact New Relic support.";
        try {
            LOG.log(Level.SEVERE, t, msg);
        } catch (Throwable t2) {
        }
        System.err.println(msg);
        if (t instanceof NoClassDefFoundError) {
            String version = System.getProperty("java.version");
            if (version.startsWith("9") || version.startsWith("10")) {
                String message = "We currently do not support Java 9 or 10 in modular mode. If you are running with " + "it and want to use the agent, use command line flag '--add-modules' to add appropriate modules";
                System.err.println(message);
            } else if (version.startsWith("11") || version.startsWith("12")) {
                String message = "Applications that previously relied on the command line flag '--add-modules' will no longer work with Java EE " + "dependencies. You must add all Java EE dependencies to your build file manually, and then remove the --add-modules flag for them.";
                System.err.println(message);
            }
        }
        t.printStackTrace();
        System.exit(1);
    }
    lifecycleObserver.agentStarted();
}
Also used : ServiceManager(com.newrelic.agent.service.ServiceManager)

Example 2 with ServiceManager

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

the class ClassTransformerConfigImplTest method testPointcutGroupDisabled.

@Test
public void testPointcutGroupDisabled() {
    Map<String, Object> configuration = new HashMap<>();
    configuration.put("class_transformer:my_pointcut_group:enabled", true);
    configuration.put("class_transformer:instrumentation_default:enabled", 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);
    PointCutConfiguration pointCutConfiguration = new PointCutConfiguration("PointcutClassName", "my_pointcut_group", true, agentConfig.getClassTransformerConfig());
    assertTrue(pointCutConfiguration.isEnabled());
}
Also used : HashMap(java.util.HashMap) MockServiceManager(com.newrelic.agent.MockServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) MockServiceManager(com.newrelic.agent.MockServiceManager) PointCutConfiguration(com.newrelic.agent.instrumentation.PointCutConfiguration) Test(org.junit.Test)

Example 3 with ServiceManager

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

the class ClassTransformerConfigImplTest method testSpringPointcutEnabledDefaultDisabled.

@Test
public void testSpringPointcutEnabledDefaultDisabled() {
    Map<String, Object> configuration = new HashMap<>();
    configuration.put("class_transformer:instrumentation_default:enabled", false);
    configuration.put("enable_spring_tracing", true);
    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);
    assertTrue(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 4 with ServiceManager

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

the class ClassTransformerConfigImplTest method testSpringPointcutEnabled.

@Test
public void testSpringPointcutEnabled() {
    Map<String, Object> configuration = new HashMap<>();
    configuration.put("lite_mode", true);
    configuration.put("class_transformer:instrumentation_default:enabled", true);
    configuration.put("enable_spring_tracing", true);
    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);
    assertTrue(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 5 with ServiceManager

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

the class ClassTransformerConfigImplTest method testSpringPointcutDisabled.

@Test
public void testSpringPointcutDisabled() {
    Map<String, Object> classTransformerMap = new HashMap<>();
    Map<String, Object> defaultInstrumentationSettings = new HashMap<>();
    defaultInstrumentationSettings.put("enabled", false);
    classTransformerMap.put(ClassTransformerConfigImpl.DEFAULT_INSTRUMENTATION, defaultInstrumentationSettings);
    Map<String, Object> configSettings = new HashMap<>();
    configSettings.put(AgentConfigImpl.CLASS_TRANSFORMER, classTransformerMap);
    configSettings.put(AgentConfigImpl.APP_NAME, "Unit Test");
    AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(configSettings);
    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)

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