Search in sources :

Example 1 with HostConnectException

use of com.newrelic.agent.transport.HostConnectException 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 HostConnectException

use of com.newrelic.agent.transport.HostConnectException in project newrelic-java-agent by newrelic.

the class ApacheHttpClientWrapper method execute.

@Override
public ReadResult execute(HttpClientWrapper.Request request, ExecuteEventHandler handler) throws Exception {
    HttpUriRequest apacheRequest = mapRequestToApacheRequest(request);
    if (handler != null) {
        handler.requestStarted();
    }
    logConnectionPoolStatus(apacheRequest);
    try (CloseableHttpResponse response = httpClient.execute(apacheRequest, createContext())) {
        if (handler != null) {
            handler.requestEnded();
        }
        logConnectionPoolStatus(apacheRequest);
        return mapResponseToResult(response);
    } catch (HttpHostConnectException hostConnectException) {
        throw new HostConnectException(hostConnectException.getHost().toString(), hostConnectException);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HostConnectException(com.newrelic.agent.transport.HostConnectException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException)

Example 3 with HostConnectException

use of com.newrelic.agent.transport.HostConnectException in project newrelic-java-agent by newrelic.

the class HostConnectExceptionTest method ensureCauseIsSet.

@Test
public void ensureCauseIsSet() {
    HttpHostConnectException innerException = new HttpHostConnectException(new IOException("really inner cause"), HttpHost.create("https://nothing.example.com"));
    HostConnectException target = new HostConnectException(innerException.toString(), innerException);
    assertSame(innerException, target.getCause());
}
Also used : HostConnectException(com.newrelic.agent.transport.HostConnectException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HostConnectException (com.newrelic.agent.transport.HostConnectException)3 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)2 Normalizer (com.newrelic.agent.normalization.Normalizer)1 HttpError (com.newrelic.agent.transport.HttpError)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 UnexpectedException (java.rmi.UnexpectedException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 Test (org.junit.Test)1