Search in sources :

Example 1 with HttpError

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());
        }
    }
}
Also used : Normalizer(com.newrelic.agent.normalization.Normalizer) HostConnectException(com.newrelic.agent.transport.HostConnectException) ConnectException(java.net.ConnectException) UnexpectedException(java.rmi.UnexpectedException) HostConnectException(com.newrelic.agent.transport.HostConnectException) HttpError(com.newrelic.agent.transport.HttpError)

Example 2 with HttpError

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());
}
Also used : ConfigService(com.newrelic.agent.config.ConfigService) ReservoirManager(com.newrelic.agent.interfaces.ReservoirManager) HttpError(com.newrelic.agent.transport.HttpError) Logger(com.newrelic.api.agent.Logger) Test(org.junit.Test)

Example 3 with HttpError

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);
        }
    }
}
Also used : LogEvent(com.newrelic.agent.model.LogEvent) StatsEngine(com.newrelic.agent.stats.StatsEngine) StatsWork(com.newrelic.agent.stats.StatsWork) HttpError(com.newrelic.agent.transport.HttpError) DistributedSamplingPriorityQueue(com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)

Example 4 with HttpError

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);
        }
    }
}
Also used : StatsWork(com.newrelic.agent.stats.StatsWork) HttpError(com.newrelic.agent.transport.HttpError) StatsEngine(com.newrelic.agent.stats.StatsEngine) CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent)

Example 5 with HttpError

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());
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) HttpError(com.newrelic.agent.transport.HttpError) EventTestHelper.generateTransactionData(com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Aggregations

HttpError (com.newrelic.agent.transport.HttpError)11 StatsEngine (com.newrelic.agent.stats.StatsEngine)4 StatsWork (com.newrelic.agent.stats.StatsWork)4 Test (org.junit.Test)4 TransactionData (com.newrelic.agent.TransactionData)2 ConfigService (com.newrelic.agent.config.ConfigService)2 ReservoirManager (com.newrelic.agent.interfaces.ReservoirManager)2 DistributedSamplingPriorityQueue (com.newrelic.agent.service.analytics.DistributedSamplingPriorityQueue)2 EventTestHelper.generateTransactionData (com.newrelic.agent.service.analytics.EventTestHelper.generateTransactionData)2 TransactionStats (com.newrelic.agent.stats.TransactionStats)2 Logger (com.newrelic.api.agent.Logger)2 IRPMService (com.newrelic.agent.IRPMService)1 SpanEventsConfig (com.newrelic.agent.config.SpanEventsConfig)1 CustomInsightsEvent (com.newrelic.agent.model.CustomInsightsEvent)1 ErrorEvent (com.newrelic.agent.model.ErrorEvent)1 LogEvent (com.newrelic.agent.model.LogEvent)1 SpanEvent (com.newrelic.agent.model.SpanEvent)1 Normalizer (com.newrelic.agent.normalization.Normalizer)1 HostConnectException (com.newrelic.agent.transport.HostConnectException)1 JSONException (com.newrelic.agent.util.JSONException)1