use of com.newrelic.agent.transport.HttpError in project newrelic-java-agent by newrelic.
the class RPMService method harvest.
@Override
public void harvest(StatsEngine statsEngine) {
if (!isConnected()) {
// Assumption: failed re-connect should be a result of a temporary condition, and we need to retry
try {
Agent.LOG.fine("Trying to re-establish connection to New Relic.");
this.launch();
} catch (Exception e) {
Agent.LOG.fine("Problem trying to re-establish connection to New Relic: " + e.getMessage());
}
}
if (isConnected()) {
boolean retry = false;
Normalizer metricNormalizer = ServiceFactory.getNormalizationService().getMetricNormalizer(appName);
List<MetricData> data = statsEngine.getMetricData(metricNormalizer);
long startTime = System.nanoTime();
long reportInterval = 0;
try {
long now = System.currentTimeMillis();
sendMetricDataSyncRestart(lastReportTime, now, data);
reportInterval = now - lastReportTime;
lastReportTime = now;
last503Error.set(0);
// log at info level whenever we successfully send metric data following a failure
if (retryCount.get() > 0) {
Agent.LOG.log(Level.INFO, "Successfully reconnected to the New Relic data service.");
}
Agent.LOG.log(Level.FINE, "Reported {0} timeslices for {1}", data.size(), getApplicationName());
} catch (InternalLimitExceeded e) {
Agent.LOG.log(Level.SEVERE, "The metric data post was too large. {0} timeslices will not be resent", data.size());
} catch (MetricDataException e) {
Agent.LOG.log(Level.SEVERE, "An invalid response was received while sending metric data. This data will not be resent.");
Agent.LOG.log(Level.FINEST, e, e.toString());
} catch (HttpError e) {
// HttpError handled here
retry = e.isRetryableError();
if (HttpResponseCode.SERVICE_UNAVAILABLE == e.getStatusCode()) {
// 503s are unfortunately common for the collector, so we log these at a lower level until we see
// many consecutive failures
handle503Error(e);
} else if (retry) {
// otherwise if we're going to retry later things aren't so bad
Agent.LOG.log(Level.INFO, "An error occurred posting metric data - {0}. This data will be resent later.", e.getMessage());
} else {
// but let's call out when we're dropping data. Check out HttpError.isRetryableError()
Agent.LOG.log(Level.SEVERE, "An error occurred posting metric data - {0}. {1} timeslices will not be resent.", e.getMessage(), data.size());
}
} catch (ForceRestartException e) {
logForceRestartException(e);
reconnectAsync();
retry = true;
} catch (ForceDisconnectException e) {
logForceDisconnectException(e);
shutdownAsync();
} catch (HostConnectException e) {
retry = true;
Agent.LOG.log(Level.INFO, "A connection error occurred contacting {0}. Please check your network / proxy settings.", e.getHostName());
Agent.LOG.log(Level.FINEST, e, e.toString());
} catch (Exception e) {
// LicenseException handled here
logMetricDataError(e);
retry = true;
if (e.getMessage() != null) {
String message = e.getMessage().toLowerCase();
// (web transaction maybe?). clear out the metrics
if (message.contains("json") && message.contains("parse")) {
retry = false;
}
}
}
long duration = System.nanoTime() - startTime;
if (retry) {
retryCount.getAndIncrement();
} else {
retryCount.set(0);
statsEngine.clear();
recordSupportabilityMetrics(statsEngine, reportInterval, duration, data.size());
}
}
}
use of com.newrelic.agent.transport.HttpError in project newrelic-java-agent by newrelic.
the class CollectorSpanEventReservoirManagerTest method retryOnHttpErrorWithNoDiscard.
@Test
public void retryOnHttpErrorWithNoDiscard() {
ConfigService mockConfigService = mock21Samples();
CollectorSpanEventReservoirManager target = initWith25Tries(mockConfigService);
ReservoirManager.HarvestResult harvestResult = target.attemptToSendReservoir(APP_NAME, (appName, reservoirSize, eventsSeen, events) -> {
throw new HttpError("don't discard", 429, 1234);
}, mock(Logger.class));
assertNull(harvestResult);
assertEquals(21, target.getOrCreateReservoir(APP_NAME).size());
assertEquals(21, target.getOrCreateReservoir(APP_NAME).getNumberOfTries());
}
use of com.newrelic.agent.transport.HttpError in project newrelic-java-agent by newrelic.
the class LogSenderServiceImpl method harvestEvents.
/**
* Harvest and send the LogEvents
*
* @param appName the application to harvest for
*/
public void harvestEvents(final String appName) {
if (!getIsEnabledForApp(ServiceFactory.getConfigService().getAgentConfig(appName), appName)) {
reservoirForApp.remove(appName);
return;
}
if (maxSamplesStored <= 0) {
clearReservoir(appName);
return;
}
long startTimeInNanos = System.nanoTime();
final DistributedSamplingPriorityQueue<LogEvent> reservoir = this.reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<>(appName, LOG_SENDER_SERVICE, maxSamplesStored));
if (reservoir != null && reservoir.size() > 0) {
try {
// Send LogEvents
ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendLogEvents(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;
}
}, LogSenderServiceImpl.class.getName());
if (reservoir.size() < reservoir.getNumberOfTries()) {
int dropped = reservoir.getNumberOfTries() - reservoir.size();
Agent.LOG.log(Level.FINE, "Dropped {0} log events out of {1}.", dropped, reservoir.getNumberOfTries());
}
} catch (HttpError e) {
if (!e.discardHarvestData()) {
Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be included in the next harvest.", e);
// Save unsent data by merging it with current data using reservoir algorithm
DistributedSamplingPriorityQueue<LogEvent> currentReservoir = reservoirForApp.get(appName);
currentReservoir.retryAll(reservoir);
} else {
// discard harvest data
reservoir.clear();
Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be dropped.", e);
}
} catch (Exception e) {
// discard harvest data
reservoir.clear();
Agent.LOG.log(Level.FINE, "Unable to send log events. Unsent events will be dropped.", e);
}
}
}
use of com.newrelic.agent.transport.HttpError in project newrelic-java-agent by newrelic.
the class InsightsServiceImpl method harvestEvents.
public void harvestEvents(final String appName) {
if (!getIsEnabledForApp(ServiceFactory.getConfigService().getAgentConfig(appName), appName)) {
reservoirForApp.remove(appName);
return;
}
if (maxSamplesStored <= 0) {
clearReservoir(appName);
return;
}
long startTimeInNanos = System.nanoTime();
final DistributedSamplingPriorityQueue<CustomInsightsEvent> reservoir = this.reservoirForApp.put(appName, new DistributedSamplingPriorityQueue<CustomInsightsEvent>(appName, "Insights Service", maxSamplesStored));
if (reservoir != null && reservoir.size() > 0) {
try {
ServiceFactory.getRPMServiceManager().getOrCreateRPMService(appName).sendCustomAnalyticsEvents(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} custom events out of {1}.", dropped, reservoir.getNumberOfTries());
}
} catch (HttpError e) {
if (!e.discardHarvestData()) {
Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be included in the next harvest.", e);
// Save unsent data by merging it with current data using reservoir algorithm
DistributedSamplingPriorityQueue<CustomInsightsEvent> currentReservoir = reservoirForApp.get(appName);
currentReservoir.retryAll(reservoir);
} else {
// discard harvest data
reservoir.clear();
Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be dropped.", e);
}
} catch (Exception e) {
// discard harvest data
reservoir.clear();
Agent.LOG.log(Level.FINE, "Unable to send custom events. Unsent events will be dropped.", e);
}
}
}
use of com.newrelic.agent.transport.HttpError in project newrelic-java-agent by newrelic.
the class TransactionEventsServiceTest method testSyntheticsBuffering1.
@Test
public void testSyntheticsBuffering1() throws Exception {
setup(true, true, TEST_RESERVOIR_SIZE);
this.rpmService.setSendAnalyticsEventsException(new HttpError("", HttpResponseCode.REQUEST_TIMEOUT, 0));
TransactionData transactionData = generateSyntheticTransactionData();
TransactionStats transactionStats = new TransactionStats();
service.dispatcherTransactionFinished(transactionData, transactionStats);
service.harvestEvents(APP_NAME);
assertEquals(1, service.pendingSyntheticsHeaps.size());
assertEquals(1, service.pendingSyntheticsHeaps.getFirst().size());
}
Aggregations