Search in sources :

Example 6 with Environment

use of com.newrelic.agent.environment.Environment in project newrelic-java-agent by newrelic.

the class RPMConnectionServiceTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    AgentHelper.initializeConfig();
    MockServiceManager serviceManager = new MockServiceManager();
    ServiceFactory.setServiceManager(serviceManager);
    serviceManager.start();
    Map<String, Object> settings = AgentConfigFactoryTest.createStagingMap();
    AgentConfig config = AgentConfigFactory.createAgentConfig(settings, null, null);
    Environment env = new Environment(config, "c:\\test\\log");
    EnvironmentService envService = Mockito.mock(EnvironmentService.class, new Returns(env));
    serviceManager.setEnvironmentService(envService);
    ConfigService configService = ConfigServiceFactory.createConfigServiceUsingSettings(settings);
    serviceManager.setConfigService(configService);
    ThreadService threadService = new ThreadService();
    serviceManager.setThreadService(threadService);
    rpmConnectionService = new TestRPMConnectionService();
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) Returns(org.mockito.internal.stubbing.answers.Returns) ThreadService(com.newrelic.agent.ThreadService) ConfigService(com.newrelic.agent.config.ConfigService) MockServiceManager(com.newrelic.agent.MockServiceManager) Environment(com.newrelic.agent.environment.Environment) EnvironmentService(com.newrelic.agent.environment.EnvironmentService) BeforeClass(org.junit.BeforeClass)

Example 7 with Environment

use of com.newrelic.agent.environment.Environment in project newrelic-java-agent by newrelic.

the class RPMService method getStartOptions.

/**
 * Returns a map of startup options to be sent to RPM when the RPM service connects.
 */
protected Map<String, Object> getStartOptions() {
    AgentConfig agentConfig = ServiceFactory.getConfigService().getAgentConfig(appName);
    int pid = ServiceFactory.getEnvironmentService().getProcessPID();
    Map<String, Object> options = new HashMap<>();
    // options.add(System.getProperty("user.dir"));
    options.put("pid", pid);
    String language = agentConfig.getLanguage();
    options.put("language", language);
    String defaultHost = Hostname.getHostname(agentConfig, true);
    options.put("host", defaultHost);
    String displayHost = Hostname.getDisplayHostname(agentConfig, defaultHost);
    options.put("display_host", displayHost);
    Agent.LOG.log(Level.INFO, "Host name is {0}, display host {1} for application {2}", defaultHost, displayHost, appName);
    options.put("high_security", getAndLogHighSecurity(agentConfig));
    Environment environment = ServiceFactory.getEnvironmentService().getEnvironment();
    options.put("environment", environment);
    options.put("settings", getSettings(agentConfig.getProperty("send_environment_info", true)));
    UtilizationData utilizationData = ServiceFactory.getUtilizationService().updateUtilizationData();
    options.put("utilization", utilizationData.map());
    options.put(AgentConfigFactory.EVENT_HARVEST_CONFIG, ServiceFactory.getHarvestService().getEventDataHarvestLimits());
    String instanceName = environment.getAgentIdentity().getInstanceName();
    if (instanceName != null) {
        options.put("instance_name", instanceName);
    }
    // options.put("framework", "java"); // this belongs in the environment
    // options.put("launch_time",
    // JSON.serializeNumber(TimeConversion.convertMillisToSeconds(System.currentTimeMillis())));
    options.put("agent_version", Agent.getVersion());
    options.put("app_name", appNames);
    StringBuilder identifier = new StringBuilder(language);
    identifier.append(':').append(appName);
    Integer serverPort = environment.getAgentIdentity().getServerPort();
    if (serverPort != null) {
        identifier.append(':').append(serverPort);
    }
    options.put("identifier", identifier.toString());
    options.put("labels", agentConfig.getLabelsConfig());
    return options;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AgentConfig(com.newrelic.agent.config.AgentConfig) HashMap(java.util.HashMap) Environment(com.newrelic.agent.environment.Environment) UtilizationData(com.newrelic.agent.utilization.UtilizationData)

Example 8 with Environment

use of com.newrelic.agent.environment.Environment in project newrelic-java-agent by newrelic.

the class HarvestServiceTest method multipleRPMServices.

@Test
public void multipleRPMServices() throws Exception {
    Environment environment = ServiceFactory.getEnvironmentService().getEnvironment();
    environment.setServerPort(null);
    final CountDownLatch latch = new CountDownLatch(2);
    MyRPMService rpmService = new MyRPMService() {

        @Override
        public void harvest(StatsEngine statsEngine) {
            latch.countDown();
        }
    };
    final CountDownLatch latch2 = new CountDownLatch(2);
    MyRPMService rpmService2 = new MyRPMService() {

        @Override
        public void harvest(StatsEngine statsEngine) {
            latch2.countDown();
        }
    };
    TestHarvestService harvestService = new TestHarvestService();
    harvestService.setReportingPeriod(500L);
    harvestService.start();
    harvestService.startHarvest(rpmService);
    harvestService.startHarvest(rpmService2);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
    harvestService.stop();
}
Also used : Environment(com.newrelic.agent.environment.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test) AgentConfigFactoryTest(com.newrelic.agent.config.AgentConfigFactoryTest)

Example 9 with Environment

use of com.newrelic.agent.environment.Environment in project newrelic-java-agent by newrelic.

the class HarvestServiceTest method harvestNowWhenHarvestNotRunning.

@Test
public void harvestNowWhenHarvestNotRunning() throws Exception {
    Environment environment = ServiceFactory.getEnvironmentService().getEnvironment();
    environment.setServerPort(null);
    final AtomicInteger harvestCount = new AtomicInteger();
    MyRPMService rpmService = new MyRPMService() {

        @Override
        public void harvest(StatsEngine statsEngine) {
            harvestCount.incrementAndGet();
        }
    };
    TestHarvestService harvestService = new TestHarvestService(666000L);
    harvestService.setReportingPeriod(1000000L);
    harvestService.start();
    harvestService.startHarvest(rpmService);
    // try to do an immediate harvest
    harvestService.harvestNow();
    // should run harvest
    Assert.assertEquals(1, harvestCount.get());
    harvestService.stop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Environment(com.newrelic.agent.environment.Environment) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test) AgentConfigFactoryTest(com.newrelic.agent.config.AgentConfigFactoryTest)

Example 10 with Environment

use of com.newrelic.agent.environment.Environment in project newrelic-java-agent by newrelic.

the class HarvestServiceTest method harvestListener.

@Test
public void harvestListener() throws Exception {
    Environment environment = ServiceFactory.getEnvironmentService().getEnvironment();
    environment.setServerPort(null);
    final CountDownLatch latch = new CountDownLatch(1);
    MyRPMService rpmService = new MyRPMService() {

        @Override
        public void harvest(StatsEngine statsEngine) {
            latch.countDown();
        }
    };
    final CountDownLatch latch2 = new CountDownLatch(1);
    HarvestListener harvestListener = new HarvestListener() {

        @Override
        public void beforeHarvest(String appName, StatsEngine statsEngine) {
            latch2.countDown();
        }

        @Override
        public void afterHarvest(String appName) {
        }
    };
    TestHarvestService harvestService = new TestHarvestService();
    harvestService.setReportingPeriod(500L);
    harvestService.addHarvestListener(harvestListener);
    harvestService.start();
    harvestService.startHarvest(rpmService);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
    harvestService.stop();
}
Also used : Environment(com.newrelic.agent.environment.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) StatsEngine(com.newrelic.agent.stats.StatsEngine) Test(org.junit.Test) AgentConfigFactoryTest(com.newrelic.agent.config.AgentConfigFactoryTest)

Aggregations

Environment (com.newrelic.agent.environment.Environment)10 Test (org.junit.Test)6 AgentConfigFactoryTest (com.newrelic.agent.config.AgentConfigFactoryTest)5 StatsEngine (com.newrelic.agent.stats.StatsEngine)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 AgentConfig (com.newrelic.agent.config.AgentConfig)3 EnvironmentService (com.newrelic.agent.environment.EnvironmentService)3 ConfigService (com.newrelic.agent.config.ConfigService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Before (org.junit.Before)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 MockServiceManager (com.newrelic.agent.MockServiceManager)1 ThreadService (com.newrelic.agent.ThreadService)1 TransactionData (com.newrelic.agent.TransactionData)1 AgentIdentity (com.newrelic.agent.environment.AgentIdentity)1 SpanError (com.newrelic.agent.model.SpanError)1 Stats (com.newrelic.agent.stats.Stats)1 StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)1 StatsService (com.newrelic.agent.stats.StatsService)1 StatsServiceImpl (com.newrelic.agent.stats.StatsServiceImpl)1