Search in sources :

Example 21 with TransactionService

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

the class CoreServiceImpl method jvmShutdown.

private void jvmShutdown(long startTime) {
    getLogger().fine("Agent JVM shutdown hook: enter.");
    AgentConfig config = ServiceFactory.getConfigService().getDefaultAgentConfig();
    // Only add the "transaction wait" shutdown hook if one has been configured
    if (config.waitForTransactionsInMillis() > 0) {
        getLogger().fine("Agent JVM shutdown hook: waiting for transactions to finish");
        // While there are still transactions in progress and we haven't hit the configured timeout keep
        // waiting and checking for the transactions to finish before allowing the shutdown to continue.
        long finishTime = System.currentTimeMillis() + config.waitForTransactionsInMillis();
        TransactionService txService = ServiceFactory.getTransactionService();
        while (txService.getTransactionsInProgress() > 0 && System.currentTimeMillis() < finishTime) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        }
        getLogger().fine("Agent JVM shutdown hook: transactions finished");
    }
    if (config.isSendDataOnExit() && ((System.currentTimeMillis() - startTime) >= config.getSendDataOnExitThresholdInMillis())) {
        // Grab all RPMService instances (may be multiple with auto_app_naming enabled) and harvest them
        List<IRPMService> rpmServices = ServiceFactory.getRPMServiceManager().getRPMServices();
        for (IRPMService rpmService : rpmServices) {
            rpmService.harvestNow();
        }
    }
    getLogger().info("JVM is shutting down");
    shutdown();
    getLogger().fine("Agent JVM shutdown hook: done.");
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) TransactionService(com.newrelic.agent.TransactionService) IRPMService(com.newrelic.agent.IRPMService)

Example 22 with TransactionService

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

the class ServiceManagerImpl method doStart.

@Override
protected synchronized void doStart() throws Exception {
    // The ConfigService has been created, but not started. This means it
    // is safe to consult the config, but it will be the local config only.
    coreService.start();
    threadService = new ThreadService();
    circuitBreakerService = new CircuitBreakerService();
    classTransformerService = new ClassTransformerServiceImpl(coreService.getInstrumentation());
    AgentConfig config = configService.getDefaultAgentConfig();
    JmxConfig jmxConfig = config.getJmxConfig();
    jmxService = new JmxService(jmxConfig);
    Logger jarCollectorLogger = Agent.LOG.getChildLogger("com.newrelic.jar_collector");
    boolean jarCollectorEnabled = configService.getDefaultAgentConfig().getJarCollectorConfig().isEnabled();
    AtomicBoolean shouldSendAllJars = new AtomicBoolean(true);
    TrackedAddSet<JarData> analyzedJars = new TrackedAddSet<>();
    Function<URL, JarData> processor = new JarCollectorServiceProcessor(jarCollectorLogger, configService.getDefaultAgentConfig());
    JarAnalystFactory jarAnalystFactory = new JarAnalystFactory(processor, analyzedJars, jarCollectorLogger);
    ExecutorService executorService = Executors.newSingleThreadExecutor(new DefaultThreadFactory("New Relic Jar Analysis Thread", true));
    JarCollectorInputs jarCollectorInputs = JarCollectorInputs.build(jarCollectorEnabled, jarAnalystFactory, executorService, jarCollectorLogger);
    jarCollectorService = new JarCollectorServiceImpl(jarCollectorLogger, jarCollectorEnabled, shouldSendAllJars, analyzedJars, jarCollectorInputs.getClassNoticingFactory());
    extensionService = new ExtensionService(configService, jarCollectorInputs.getExtensionAnalysisProducer());
    String defaultAppName = configService.getDefaultAgentConfig().getApplicationName();
    JarCollectorConnectionListener jarCollectorConnectionListener = new JarCollectorConnectionListener(defaultAppName, shouldSendAllJars);
    JarCollectorHarvestListener jarCollectorHarvestListener = new JarCollectorHarvestListener(defaultAppName, jarCollectorService);
    sourceLanguageService = new SourceLanguageService();
    expirationService = new ExpirationService();
    tracerService = new TracerService();
    // this allows async parts of transaction to be registered
    // it must be created before the first class transformation occurs
    asyncTxService = new AsyncTransactionService();
    // this is called in a transaction finish - it needs to be created before the first class transformation
    environmentService = new EnvironmentServiceImpl();
    /*
         * Before this point the ClassTransformer is not initialized, so be careful not to load classes that should be
         * instrumented.
         */
    cacheService = new CacheService();
    extensionService.start();
    classTransformerService.start();
    boolean realAgent = coreService.getInstrumentation() != null;
    statsService = new StatsServiceImpl();
    replayStartupStatsWork();
    utilizationService = new UtilizationService();
    // Start as early as possible.
    if (realAgent) {
        utilizationService.start();
    }
    rpmConnectionService = new RPMConnectionServiceImpl();
    transactionService = new TransactionService();
    InfiniteTracing infiniteTracing = buildInfiniteTracing(configService);
    InfiniteTracingEnabledCheck infiniteTracingEnabledCheck = new InfiniteTracingEnabledCheck(configService);
    SpanEventCreationDecider spanEventCreationDecider = new SpanEventCreationDecider(configService);
    AgentConnectionEstablishedListener agentConnectionEstablishedListener = new UpdateInfiniteTracingAfterConnect(infiniteTracingEnabledCheck, infiniteTracing);
    JfrConfig jfrConfig = config.getJfrConfig();
    jfrService = new JfrService(jfrConfig, configService.getDefaultAgentConfig());
    AgentConnectionEstablishedListener jfrServiceConnectionListener = new JfrServiceConnectionListener(jfrService);
    distributedTraceService = new DistributedTraceServiceImpl();
    TransactionDataToDistributedTraceIntrinsics transactionDataToDistributedTraceIntrinsics = new TransactionDataToDistributedTraceIntrinsics(distributedTraceService);
    rpmServiceManager = new RPMServiceManagerImpl(agentConnectionEstablishedListener, jarCollectorConnectionListener, jfrServiceConnectionListener);
    normalizationService = new NormalizationServiceImpl();
    harvestService = new HarvestServiceImpl();
    gcService = realAgent ? new GCService() : new NoopService("GC Service");
    transactionTraceService = new TransactionTraceService();
    transactionEventsService = new TransactionEventsService(transactionDataToDistributedTraceIntrinsics);
    profilerService = new ProfilerService();
    commandParser = new CommandParser();
    cpuSamplerService = realAgent ? new CPUSamplerService() : new NoopService("CPU Sampler");
    deadlockDetectorService = new DeadlockDetectorService();
    samplerService = realAgent ? new SamplerServiceImpl() : new NoopSamplerService();
    sqlTraceService = new SqlTraceServiceImpl();
    databaseService = new DatabaseService();
    browserService = new BrowserServiceImpl();
    remoteInstrumentationService = new RemoteInstrumentationServiceImpl();
    attsService = new AttributesService();
    insightsService = new InsightsServiceImpl();
    logSenderService = new LogSenderServiceImpl();
    spanEventsService = SpanEventsServiceFactory.builder().configService(configService).rpmServiceManager(rpmServiceManager).transactionService(transactionService).infiniteTracingConsumer(infiniteTracing).spanEventCreationDecider(spanEventCreationDecider).environmentService(environmentService).transactionDataToDistributedTraceIntrinsics(transactionDataToDistributedTraceIntrinsics).build();
    // Register harvest listeners that started before harvest service was created.
    harvestService.addHarvestListener(extensionService);
    harvestService.addHarvestListener(jarCollectorHarvestListener);
    asyncTxService.start();
    threadService.start();
    statsService.start();
    environmentService.start();
    rpmConnectionService.start();
    tracerService.start();
    jarCollectorService.start();
    sourceLanguageService.start();
    harvestService.start();
    gcService.start();
    transactionService.start();
    transactionTraceService.start();
    transactionEventsService.start();
    profilerService.start();
    commandParser.start();
    jmxService.start();
    cpuSamplerService.start();
    deadlockDetectorService.start();
    samplerService.start();
    sqlTraceService.start();
    browserService.start();
    cacheService.start();
    normalizationService.start();
    databaseService.start();
    configService.start();
    remoteInstrumentationService.start();
    attsService.start();
    insightsService.start();
    logSenderService.start();
    circuitBreakerService.start();
    distributedTraceService.start();
    spanEventsService.start();
    startServices();
    // start last so other services can add connection listeners
    rpmServiceManager.start();
    // used for debugging purposes to quickly determine slow service startups
    ServiceTiming.setEndTime();
    ServiceTiming.logServiceTimings(getLogger());
}
Also used : ClassTransformerServiceImpl(com.newrelic.agent.instrumentation.ClassTransformerServiceImpl) TrackedAddSet(com.newrelic.agent.service.module.TrackedAddSet) RPMServiceManagerImpl(com.newrelic.agent.RPMServiceManagerImpl) TransactionDataToDistributedTraceIntrinsics(com.newrelic.agent.service.analytics.TransactionDataToDistributedTraceIntrinsics) AgentConnectionEstablishedListener(com.newrelic.agent.AgentConnectionEstablishedListener) CommandParser(com.newrelic.agent.commands.CommandParser) DefaultThreadFactory(com.newrelic.agent.util.DefaultThreadFactory) InsightsServiceImpl(com.newrelic.agent.service.analytics.InsightsServiceImpl) RPMConnectionServiceImpl(com.newrelic.agent.rpm.RPMConnectionServiceImpl) JarCollectorServiceImpl(com.newrelic.agent.service.module.JarCollectorServiceImpl) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) ExpirationService(com.newrelic.agent.ExpirationService) ProfilerService(com.newrelic.agent.profile.ProfilerService) CircuitBreakerService(com.newrelic.agent.circuitbreaker.CircuitBreakerService) JarAnalystFactory(com.newrelic.agent.service.module.JarAnalystFactory) RemoteInstrumentationServiceImpl(com.newrelic.agent.reinstrument.RemoteInstrumentationServiceImpl) SqlTraceServiceImpl(com.newrelic.agent.sql.SqlTraceServiceImpl) TransactionService(com.newrelic.agent.TransactionService) AsyncTransactionService(com.newrelic.agent.service.async.AsyncTransactionService) HarvestServiceImpl(com.newrelic.agent.HarvestServiceImpl) AttributesService(com.newrelic.agent.attributes.AttributesService) InfiniteTracing(com.newrelic.InfiniteTracing) DeadlockDetectorService(com.newrelic.agent.deadlock.DeadlockDetectorService) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) BrowserServiceImpl(com.newrelic.agent.browser.BrowserServiceImpl) JmxConfig(com.newrelic.agent.config.JmxConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EnvironmentServiceImpl(com.newrelic.agent.environment.EnvironmentServiceImpl) JarCollectorInputs(com.newrelic.agent.service.module.JarCollectorInputs) InfiniteTracingEnabledCheck(com.newrelic.agent.service.analytics.InfiniteTracingEnabledCheck) TransactionEventsService(com.newrelic.agent.service.analytics.TransactionEventsService) JarData(com.newrelic.agent.service.module.JarData) Logger(com.newrelic.api.agent.Logger) URL(java.net.URL) AgentConfig(com.newrelic.agent.config.AgentConfig) SpanEventCreationDecider(com.newrelic.agent.service.analytics.SpanEventCreationDecider) JfrConfig(com.newrelic.agent.config.JfrConfig) LogSenderServiceImpl(com.newrelic.agent.service.logging.LogSenderServiceImpl) JarCollectorServiceProcessor(com.newrelic.agent.service.module.JarCollectorServiceProcessor) CPUSamplerService(com.newrelic.agent.samplers.CPUSamplerService) JarCollectorConnectionListener(com.newrelic.agent.service.module.JarCollectorConnectionListener) GCService(com.newrelic.agent.GCService) CacheService(com.newrelic.agent.cache.CacheService) NormalizationServiceImpl(com.newrelic.agent.normalization.NormalizationServiceImpl) ExtensionService(com.newrelic.agent.extension.ExtensionService) TracerService(com.newrelic.agent.TracerService) UtilizationService(com.newrelic.agent.utilization.UtilizationService) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) JmxService(com.newrelic.agent.jmx.JmxService) AsyncTransactionService(com.newrelic.agent.service.async.AsyncTransactionService) DatabaseService(com.newrelic.agent.database.DatabaseService) JarCollectorHarvestListener(com.newrelic.agent.service.module.JarCollectorHarvestListener) ThreadService(com.newrelic.agent.ThreadService) ExecutorService(java.util.concurrent.ExecutorService) SamplerServiceImpl(com.newrelic.agent.samplers.SamplerServiceImpl) NoopSamplerService(com.newrelic.agent.samplers.NoopSamplerService) SourceLanguageService(com.newrelic.agent.language.SourceLanguageService) JfrService(com.newrelic.agent.jfr.JfrService)

Example 23 with TransactionService

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

the class ErrorServiceTest method unexpectedStringInTransaction.

@Test
public void unexpectedStringInTransaction() {
    TransactionData transactionData = createTransactionData(200, new ReportableError("Surprising error"), false);
    TransactionService txService = ServiceFactory.getTransactionService();
    TransactionStats transactionStats = new TransactionStats();
    txService.transactionFinished(transactionData, transactionStats);
    List<TracedError> tracedErrors = ServiceFactory.getRPMService().getErrorService().getAndClearTracedErrors();
    TracedError tracedError = tracedErrors.get(0);
    Assert.assertEquals("Surprising error", tracedError.getMessage());
    Assert.assertFalse(tracedError.expected);
    Assert.assertTrue(tracedError.incrementsErrorMetric());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) TransactionService(com.newrelic.agent.TransactionService) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 24 with TransactionService

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

the class ErrorServiceTest method expectedStringInTransaction.

@Test
public void expectedStringInTransaction() {
    TransactionData transactionData = createTransactionData(200, new ReportableError("I am expected"), true);
    TransactionService txService = ServiceFactory.getTransactionService();
    TransactionStats transactionStats = new TransactionStats();
    txService.transactionFinished(transactionData, transactionStats);
    List<TracedError> tracedErrors = ServiceFactory.getRPMService().getErrorService().getAndClearTracedErrors();
    TracedError tracedError = tracedErrors.get(0);
    Assert.assertEquals("I am expected", tracedError.getMessage());
    Assert.assertTrue(tracedError.expected);
    Assert.assertFalse(tracedError.incrementsErrorMetric());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) TransactionService(com.newrelic.agent.TransactionService) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 25 with TransactionService

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

the class ErrorServiceTest method errorCountMetrics.

@Test
public void errorCountMetrics() throws Exception {
    Map<String, Object> config = createConfig("java.lang.Exception");
    EventTestHelper.createServiceManager(config);
    MockRPMService rpmService = (MockRPMService) ServiceFactory.getRPMService();
    rpmService.setIsConnected(true);
    TransactionService txService = ServiceFactory.getTransactionService();
    Throwable error = new ArrayIndexOutOfBoundsException();
    TransactionData data = createTransactionData(true, 0, error, false);
    TransactionStats transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats("Errors/WebTransaction/Uri/dude").getCallCount());
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats(MetricNames.WEB_TRANSACTION_ERRORS_ALL).getCallCount());
    data = createTransactionData(true, 0, null, false);
    transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats("Errors/WebTransaction/Uri/dude").getCallCount());
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats(MetricNames.WEB_TRANSACTION_ERRORS_ALL).getCallCount());
    data = createTransactionData(false, 0, error, false);
    transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats("Errors/OtherTransaction/Custom/dude").getCallCount());
    Assert.assertEquals(1, transactionStats.getUnscopedStats().getStats(MetricNames.OTHER_TRANSACTION_ERRORS_ALL).getCallCount());
    data = createTransactionData(false, 0, null, false);
    transactionStats = new TransactionStats();
    txService.transactionFinished(data, transactionStats);
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats("Errors/OtherTransaction/Custom/dude").getCallCount());
    Assert.assertEquals(0, transactionStats.getUnscopedStats().getStats(MetricNames.WEB_TRANSACTION_ERRORS_ALL).getCallCount());
    StatsService spy = spy(new StatsServiceImpl());
    ((MockServiceManager) ServiceFactory.getServiceManager()).setStatsService(spy);
    ErrorServiceImpl errorService = (ErrorServiceImpl) ServiceFactory.getRPMService().getErrorService();
    StatsEngine statsEngine = spy.getStatsEngineForHarvest(APP_NAME);
    List<TracedError> actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(2, actualErrors.size());
    Assert.assertEquals(2, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
    spy = spy(new StatsServiceImpl());
    ((MockServiceManager) ServiceFactory.getServiceManager()).setStatsService(spy);
    statsEngine = spy.getStatsEngineForHarvest(APP_NAME);
    actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(0, actualErrors.size());
    Assert.assertEquals(0, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
    data = createTransactionData(true, 0, error, false);
    txService.transactionFinished(data, new TransactionStats());
    spy = spy(new StatsServiceImpl());
    ((MockServiceManager) ServiceFactory.getServiceManager()).setStatsService(spy);
    statsEngine = spy.getStatsEngineForHarvest(APP_NAME);
    actualErrors = errorService.getAndClearTracedErrors(APP_NAME, statsEngine);
    Assert.assertEquals(1, actualErrors.size());
    Assert.assertEquals(1, statsEngine.getStats(MetricNames.ERRORS_ALL).getCallCount());
}
Also used : TransactionService(com.newrelic.agent.TransactionService) StatsService(com.newrelic.agent.stats.StatsService) StatsEngine(com.newrelic.agent.stats.StatsEngine) TransactionStats(com.newrelic.agent.stats.TransactionStats) StatsServiceImpl(com.newrelic.agent.stats.StatsServiceImpl) MockServiceManager(com.newrelic.agent.MockServiceManager) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Aggregations

TransactionService (com.newrelic.agent.TransactionService)66 MockServiceManager (com.newrelic.agent.MockServiceManager)42 TransactionTraceService (com.newrelic.agent.trace.TransactionTraceService)39 ConfigService (com.newrelic.agent.config.ConfigService)38 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)30 ThreadService (com.newrelic.agent.ThreadService)30 HashMap (java.util.HashMap)29 HarvestService (com.newrelic.agent.HarvestService)26 MockRPMService (com.newrelic.agent.MockRPMService)26 Test (org.junit.Test)24 AttributesService (com.newrelic.agent.attributes.AttributesService)23 MockHarvestService (com.newrelic.agent.MockHarvestService)22 MockCoreService (com.newrelic.agent.MockCoreService)19 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)19 AgentConfig (com.newrelic.agent.config.AgentConfig)17 StatsServiceImpl (com.newrelic.agent.stats.StatsServiceImpl)17 SqlTraceService (com.newrelic.agent.sql.SqlTraceService)16 SqlTraceServiceImpl (com.newrelic.agent.sql.SqlTraceServiceImpl)16 StatsService (com.newrelic.agent.stats.StatsService)15 Transaction (com.newrelic.agent.Transaction)13