Search in sources :

Example 11 with AgentConfiguration

use of com.wavefront.api.agent.AgentConfiguration in project java by wavefrontHQ.

the class ProxyCheckInSchedulerTest method testNetworkErrors.

@Test
public void testNetworkErrors() {
    ProxyConfig proxyConfig = EasyMock.createMock(ProxyConfig.class);
    ProxyV2API proxyV2API = EasyMock.createMock(ProxyV2API.class);
    APIContainer apiContainer = EasyMock.createMock(APIContainer.class);
    reset(proxyConfig, proxyV2API, proxyConfig);
    expect(proxyConfig.getServer()).andReturn("https://acme.corp/zzz").anyTimes();
    expect(proxyConfig.getToken()).andReturn("abcde12345").anyTimes();
    expect(proxyConfig.getHostname()).andReturn("proxyHost").anyTimes();
    expect(proxyConfig.isEphemeral()).andReturn(true).anyTimes();
    expect(proxyConfig.getAgentMetricsPointTags()).andReturn(Collections.emptyMap()).anyTimes();
    String authHeader = "Bearer abcde12345";
    AgentConfiguration returnConfig = new AgentConfiguration();
    returnConfig.setPointsPerBatch(1234567L);
    replay(proxyConfig);
    UUID proxyId = ProxyUtil.getOrCreateProxyId(proxyConfig);
    expect(apiContainer.getProxyV2API()).andReturn(proxyV2API).anyTimes();
    expect(proxyV2API.proxyCheckin(eq(proxyId), eq(authHeader), eq("proxyHost"), eq(getBuildVersion()), anyLong(), anyObject(), eq(true))).andThrow(new ProcessingException(new UnknownHostException())).once();
    expect(proxyV2API.proxyCheckin(eq(proxyId), eq(authHeader), eq("proxyHost"), eq(getBuildVersion()), anyLong(), anyObject(), eq(true))).andThrow(new ProcessingException(new SocketTimeoutException())).once();
    expect(proxyV2API.proxyCheckin(eq(proxyId), eq(authHeader), eq("proxyHost"), eq(getBuildVersion()), anyLong(), anyObject(), eq(true))).andThrow(new ProcessingException(new ConnectException())).once();
    expect(proxyV2API.proxyCheckin(eq(proxyId), eq(authHeader), eq("proxyHost"), eq(getBuildVersion()), anyLong(), anyObject(), eq(true))).andThrow(new ProcessingException(new NullPointerException())).once();
    expect(proxyV2API.proxyCheckin(eq(proxyId), eq(authHeader), eq("proxyHost"), eq(getBuildVersion()), anyLong(), anyObject(), eq(true))).andThrow(new NullPointerException()).once();
    replay(proxyV2API, apiContainer);
    ProxyCheckInScheduler scheduler = new ProxyCheckInScheduler(proxyId, proxyConfig, apiContainer, x -> fail("We are not supposed to get here"), () -> {
    }, () -> {
    });
    scheduler.updateConfiguration();
    scheduler.updateConfiguration();
    scheduler.updateConfiguration();
    scheduler.updateConfiguration();
    verify(proxyConfig, proxyV2API, apiContainer);
}
Also used : APIContainer(com.wavefront.agent.api.APIContainer) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) ProxyV2API(com.wavefront.api.ProxyV2API) AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) UUID(java.util.UUID) ProcessingException(javax.ws.rs.ProcessingException) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 12 with AgentConfiguration

use of com.wavefront.api.agent.AgentConfiguration in project java by wavefrontHQ.

the class ProxyCheckInSchedulerTest method testNormalCheckinWithBadConsumer.

@Test
public void testNormalCheckinWithBadConsumer() {
    ProxyConfig proxyConfig = EasyMock.createMock(ProxyConfig.class);
    ProxyV2API proxyV2API = EasyMock.createMock(ProxyV2API.class);
    APIContainer apiContainer = EasyMock.createMock(APIContainer.class);
    reset(proxyConfig, proxyV2API, proxyConfig);
    expect(proxyConfig.getServer()).andReturn("https://acme.corp/api/").anyTimes();
    expect(proxyConfig.getToken()).andReturn("abcde12345").anyTimes();
    expect(proxyConfig.getHostname()).andReturn("proxyHost").anyTimes();
    expect(proxyConfig.isEphemeral()).andReturn(true).anyTimes();
    expect(proxyConfig.getAgentMetricsPointTags()).andReturn(Collections.emptyMap()).anyTimes();
    String authHeader = "Bearer abcde12345";
    AgentConfiguration returnConfig = new AgentConfiguration();
    replay(proxyConfig);
    UUID proxyId = ProxyUtil.getOrCreateProxyId(proxyConfig);
    expect(proxyV2API.proxyCheckin(eq(proxyId), eq(authHeader), eq("proxyHost"), eq(getBuildVersion()), anyLong(), anyObject(), eq(true))).andReturn(returnConfig).anyTimes();
    expect(apiContainer.getProxyV2API()).andReturn(proxyV2API).anyTimes();
    replay(proxyV2API, apiContainer);
    try {
        ProxyCheckInScheduler scheduler = new ProxyCheckInScheduler(proxyId, proxyConfig, apiContainer, x -> {
            throw new NullPointerException("gotcha!");
        }, () -> {
        }, () -> {
        });
        scheduler.updateProxyMetrics();
        ;
        scheduler.updateConfiguration();
        verify(proxyConfig, proxyV2API, apiContainer);
        fail("We're not supposed to get here");
    } catch (NullPointerException e) {
    // NPE caught, we're good
    }
}
Also used : APIContainer(com.wavefront.agent.api.APIContainer) ProxyV2API(com.wavefront.api.ProxyV2API) AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) UUID(java.util.UUID) Test(org.junit.Test)

Example 13 with AgentConfiguration

use of com.wavefront.api.agent.AgentConfiguration in project java by wavefrontHQ.

the class ProxyCheckInScheduler method checkin.

/**
 * Perform agent check-in and fetch configuration of the daemon from remote server.
 *
 * @return Fetched configuration. {@code null} if the configuration is invalid.
 */
private AgentConfiguration checkin() {
    AgentConfiguration newConfig;
    JsonNode agentMetricsWorkingCopy;
    synchronized (executor) {
        if (agentMetrics == null)
            return null;
        agentMetricsWorkingCopy = agentMetrics;
        agentMetrics = null;
        if (retries.incrementAndGet() > MAX_CHECKIN_ATTEMPTS)
            return null;
    }
    logger.info("Checking in: " + firstNonNull(serverEndpointUrl, proxyConfig.getServer()));
    try {
        newConfig = apiContainer.getProxyV2API().proxyCheckin(proxyId, "Bearer " + proxyConfig.getToken(), proxyConfig.getHostname(), getBuildVersion(), System.currentTimeMillis(), agentMetricsWorkingCopy, proxyConfig.isEphemeral());
        agentMetricsWorkingCopy = null;
    } catch (ClientErrorException ex) {
        agentMetricsWorkingCopy = null;
        switch(ex.getResponse().getStatus()) {
            case 401:
                checkinError("HTTP 401 Unauthorized: Please verify that your server and token settings" + " are correct and that the token has Proxy Management permission!");
                if (successfulCheckIns.get() == 0) {
                    throw new RuntimeException("Aborting start-up");
                }
                break;
            case 403:
                checkinError("HTTP 403 Forbidden: Please verify that your token has Proxy Management " + "permission!");
                if (successfulCheckIns.get() == 0) {
                    throw new RuntimeException("Aborting start-up");
                }
                break;
            case 404:
            case 405:
                String serverUrl = proxyConfig.getServer().replaceAll("/$", "");
                if (successfulCheckIns.get() == 0 && !retryImmediately && !serverUrl.endsWith("/api")) {
                    this.serverEndpointUrl = serverUrl + "/api/";
                    checkinError("Possible server endpoint misconfiguration detected, attempting to use " + serverEndpointUrl);
                    apiContainer.updateServerEndpointURL(serverEndpointUrl);
                    retryImmediately = true;
                    return null;
                }
                String secondaryMessage = serverUrl.endsWith("/api") ? "Current setting: " + proxyConfig.getServer() : "Server endpoint URLs normally end with '/api/'. Current setting: " + proxyConfig.getServer();
                checkinError("HTTP " + ex.getResponse().getStatus() + ": Misconfiguration detected, " + "please verify that your server setting is correct. " + secondaryMessage);
                if (successfulCheckIns.get() == 0) {
                    throw new RuntimeException("Aborting start-up");
                }
                break;
            case 407:
                checkinError("HTTP 407 Proxy Authentication Required: Please verify that " + "proxyUser and proxyPassword settings are correct and make sure your HTTP proxy" + " is not rate limiting!");
                if (successfulCheckIns.get() == 0) {
                    throw new RuntimeException("Aborting start-up");
                }
                break;
            case 429:
                // 429s are retried silently.
                return null;
            default:
                checkinError("HTTP " + ex.getResponse().getStatus() + " error: Unable to check in with Wavefront! " + proxyConfig.getServer() + ": " + Throwables.getRootCause(ex).getMessage());
        }
        // return empty configuration to prevent checking in every 1s
        return new AgentConfiguration();
    } catch (ProcessingException ex) {
        Throwable rootCause = Throwables.getRootCause(ex);
        if (rootCause instanceof UnknownHostException) {
            checkinError("Unknown host: " + proxyConfig.getServer() + ". Please verify your DNS and network settings!");
            return null;
        }
        if (rootCause instanceof ConnectException) {
            checkinError("Unable to connect to " + proxyConfig.getServer() + ": " + rootCause.getMessage() + " Please verify your network/firewall settings!");
            return null;
        }
        if (rootCause instanceof SocketTimeoutException) {
            checkinError("Unable to check in with " + proxyConfig.getServer() + ": " + rootCause.getMessage() + " Please verify your network/firewall settings!");
            return null;
        }
        checkinError("Request processing error: Unable to retrieve proxy configuration! " + proxyConfig.getServer() + ": " + rootCause);
        return null;
    } catch (Exception ex) {
        checkinError("Unable to retrieve proxy configuration from remote server! " + proxyConfig.getServer() + ": " + Throwables.getRootCause(ex));
        return null;
    } finally {
        synchronized (executor) {
            // not been updated yet, restore last known set of agent metrics to be retried
            if (agentMetricsWorkingCopy != null && agentMetrics == null) {
                agentMetrics = agentMetricsWorkingCopy;
            }
        }
    }
    if (newConfig.currentTime != null) {
        Clock.set(newConfig.currentTime);
    }
    return newConfig;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) ClientErrorException(javax.ws.rs.ClientErrorException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ClientErrorException(javax.ws.rs.ClientErrorException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) UnknownHostException(java.net.UnknownHostException) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException) ConnectException(java.net.ConnectException)

Aggregations

AgentConfiguration (com.wavefront.api.agent.AgentConfiguration)13 Test (org.junit.Test)10 APIContainer (com.wavefront.agent.api.APIContainer)9 ProxyV2API (com.wavefront.api.ProxyV2API)9 UUID (java.util.UUID)9 ClientErrorException (javax.ws.rs.ClientErrorException)8 ConnectException (java.net.ConnectException)4 SocketTimeoutException (java.net.SocketTimeoutException)4 UnknownHostException (java.net.UnknownHostException)4 ProcessingException (javax.ws.rs.ProcessingException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)1 ServerErrorException (javax.ws.rs.ServerErrorException)1