Search in sources :

Example 6 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class ApiTest method testNoExceptionRecordMetricNoTransaction.

@Test
public void testNoExceptionRecordMetricNoTransaction() throws Exception {
    final MetricName name = MetricName.create("roger");
    StatsWork statsWork = new StatsWork() {

        @Override
        public void doWork(StatsEngine statsEngine) {
            statsEngine.getApdexStats(name);
            try {
                statsEngine.getStats(name);
                Assert.fail("expected java.lang.RuntimeException");
            } catch (RuntimeException e) {
            // expected
            }
        }

        @Override
        public String getAppName() {
            return null;
        }
    };
    ServiceFactory.getStatsService().doStatsWork(statsWork, "statsWorkName");
    NewRelic.recordMetric(name.getName(), 1.0f);
}
Also used : MetricName(com.newrelic.agent.metric.MetricName) StatsWork(com.newrelic.agent.stats.StatsWork) StatsEngine(com.newrelic.agent.stats.StatsEngine) BrowserConfigTest(com.newrelic.agent.browser.BrowserConfigTest) Test(org.junit.Test)

Example 7 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class CloudUtilityTest method recordsMetric.

@Test
public void recordsMetric() {
    ArgumentCaptor<StatsWork> captor = ArgumentCaptor.forClass(StatsWork.class);
    doNothing().when(mockStatsService).doStatsWork(captor.capture(), anyString());
    new CloudUtility().recordError("some error");
    verify(mockStatsService, times(1)).doStatsWork(any(StatsWork.class), anyString());
    StatsWork argument = captor.getValue();
    assertTrue(argument instanceof IncrementCounter);
    assertEquals("some error", ((IncrementCounter) argument).getName());
}
Also used : IncrementCounter(com.newrelic.agent.stats.IncrementCounter) StatsWork(com.newrelic.agent.stats.StatsWork) Test(org.junit.Test)

Example 8 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class Harvestable method recordIntervalMetric.

private void recordIntervalMetric() {
    long startTimeInNanos = System.nanoTime();
    final long harvestIntervalInNanos = startTimeInNanos - lastHarvest;
    lastHarvest = startTimeInNanos;
    ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

        @Override
        public void doWork(StatsEngine statsEngine) {
            if (harvestIntervalInNanos > 0) {
                statsEngine.getResponseTimeStats(service.getEventHarvestIntervalMetric()).recordResponseTime(harvestIntervalInNanos, TimeUnit.NANOSECONDS);
            }
        }

        @Override
        public String getAppName() {
            return appName;
        }
    }, "HarvestInterval");
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Example 9 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class ErrorServiceImpl method harvestEvents.

public void harvestEvents(final String appName) {
    boolean eventsEnabled = isEventsEnabledForApp(appName);
    if (!eventsEnabled) {
        reservoirForApp.remove(appName);
        return;
    }
    if (maxSamplesStored <= 0) {
        clearReservoir(appName);
        return;
    }
    long startTimeInNanos = System.nanoTime();
    final DistributedSamplingPriorityQueue<ErrorEvent> reservoir = reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<ErrorEvent>(appName, "Error Service", maxSamplesStored));
    if (reservoir != null && reservoir.size() > 0) {
        try {
            ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendErrorEvents(maxSamplesStored, reservoir.getNumberOfTries(), Collections.unmodifiableList(reservoir.asList()));
            final long durationInNanos = System.nanoTime() - startTimeInNanos;
            ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

                @Override
                public void doWork(StatsEngine statsEngine) {
                    recordSupportabilityMetrics(statsEngine, durationInNanos, reservoir);
                }

                @Override
                public String getAppName() {
                    return appName;
                }
            }, reservoir.getServiceName());
            if (reservoir.size() < reservoir.getNumberOfTries()) {
                int dropped = reservoir.getNumberOfTries() - reservoir.size();
                Agent.LOG.log(Level.FINE, "Dropped {0} error events out of {1}.", dropped, reservoir.getNumberOfTries());
            }
        } catch (HttpError e) {
            if (!e.discardHarvestData()) {
                Agent.LOG.log(Level.FINE, "Unable to send error events. Unsent events will be included in the next harvest.", e);
                // Save unsent data by merging it with current data using reservoir algorithm
                DistributedSamplingPriorityQueue<ErrorEvent> currentReservoir = reservoirForApp.get(appName);
                currentReservoir.retryAll(reservoir);
            } else {
                // discard harvest data
                reservoir.clear();
                Agent.LOG.log(Level.FINE, "Unable to send error events. Unsent events will be dropped.", e);
            }
        } catch (Exception e) {
            // discard harvest data
            reservoir.clear();
            Agent.LOG.log(Level.FINE, "Unable to send error events. Unsent events will be dropped.", e);
        }
    }
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) ErrorEvent(com.newrelic.agent.model.ErrorEvent) HttpError(com.newrelic.agent.transport.HttpError) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Example 10 with StatsWork

use of com.newrelic.agent.stats.StatsWork in project newrelic-java-agent by newrelic.

the class SpanEventsServiceImpl method harvestEvents.

@Override
public void harvestEvents(final String appName) {
    if (!spanEventsConfig.isEnabled() || reservoirManager.getMaxSamplesStored() <= 0) {
        clearReservoir();
        return;
    }
    long startTimeInNanos = System.nanoTime();
    final ReservoirManager.HarvestResult result = reservoirManager.attemptToSendReservoir(appName, collectorSender, logger);
    if (result != null) {
        final long durationInNanos = System.nanoTime() - startTimeInNanos;
        ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

            @Override
            public void doWork(StatsEngine statsEngine) {
                recordSupportabilityMetrics(statsEngine, durationInNanos, result.sent, result.seen);
            }

            @Override
            public String getAppName() {
                return appName;
            }
        }, "HarvestResult");
    }
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) ReservoirManager(com.newrelic.agent.interfaces.ReservoirManager) StatsEngine(com.newrelic.agent.stats.StatsEngine)

Aggregations

StatsWork (com.newrelic.agent.stats.StatsWork)11 StatsEngine (com.newrelic.agent.stats.StatsEngine)8 HttpError (com.newrelic.agent.transport.HttpError)4 Test (org.junit.Test)3 MetricName (com.newrelic.agent.metric.MetricName)2 DistributedSamplingPriorityQueue (com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)2 TransactionActivity (com.newrelic.agent.TransactionActivity)1 BrowserConfigTest (com.newrelic.agent.browser.BrowserConfigTest)1 ReservoirManager (com.newrelic.agent.interfaces.ReservoirManager)1 CustomInsightsEvent (com.newrelic.agent.model.CustomInsightsEvent)1 ErrorEvent (com.newrelic.agent.model.ErrorEvent)1 LogEvent (com.newrelic.agent.model.LogEvent)1 IncrementCounter (com.newrelic.agent.stats.IncrementCounter)1 MergeStatsEngine (com.newrelic.agent.stats.MergeStatsEngine)1 StatsService (com.newrelic.agent.stats.StatsService)1 BasicThreadInfo (com.newrelic.agent.threads.BasicThreadInfo)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 Tracer (com.newrelic.agent.tracers.Tracer)1 ThreadInfo (java.lang.management.ThreadInfo)1 ArrayList (java.util.ArrayList)1