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);
}
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());
}
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");
}
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);
}
}
}
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");
}
}
Aggregations