Search in sources :

Example 1 with AgentConfiguration

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

the class AbstractAgent method fetchConfig.

/**
 * Fetch configuration of the daemon from remote server.
 *
 * @return Fetched configuration. {@code null} if the configuration is invalid.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
private AgentConfiguration fetchConfig() {
    AgentConfiguration newConfig = null;
    JsonNode agentMetricsWorkingCopy;
    long agentMetricsCaptureTsWorkingCopy;
    synchronized (agentConfigurationExecutor) {
        if (agentMetrics == null)
            return null;
        agentMetricsWorkingCopy = agentMetrics;
        agentMetricsCaptureTsWorkingCopy = agentMetricsCaptureTs;
        agentMetrics = null;
    }
    logger.info("fetching configuration from server at: " + server);
    try {
        newConfig = agentAPI.checkin(agentId, hostname, token, props.getString("build.version"), agentMetricsCaptureTsWorkingCopy, localAgent, agentMetricsWorkingCopy, pushAgent, ephemeral);
        agentMetricsWorkingCopy = null;
    } catch (NotAuthorizedException ex) {
        fetchConfigError("HTTP 401 Unauthorized: Please verify that your server and token settings", "are correct and that the token has Proxy Management permission!");
        return null;
    } catch (ClientErrorException ex) {
        if (ex.getResponse().getStatus() == 407) {
            fetchConfigError("HTTP 407 Proxy Authentication Required: Please verify that proxyUser and proxyPassword", "settings are correct and make sure your HTTP proxy is not rate limiting!");
            return null;
        }
        if (ex.getResponse().getStatus() == 404) {
            fetchConfigError("HTTP 404 Not Found: Please verify that your server setting is correct: " + server, null);
            return null;
        }
        fetchConfigError("HTTP " + ex.getResponse().getStatus() + " error: Unable to retrieve proxy configuration!", server + ": " + Throwables.getRootCause(ex).getMessage());
        return null;
    } catch (ProcessingException ex) {
        Throwable rootCause = Throwables.getRootCause(ex);
        if (rootCause instanceof UnknownHostException) {
            fetchConfigError("Unknown host: " + server + ". Please verify your DNS and network settings!", null);
            return null;
        }
        if (rootCause instanceof ConnectException || rootCause instanceof SocketTimeoutException) {
            fetchConfigError("Unable to connect to " + server + ": " + rootCause.getMessage(), "Please verify your network/firewall settings!");
            return null;
        }
        fetchConfigError("Request processing error: Unable to retrieve proxy configuration!", server + ": " + rootCause);
        return null;
    } catch (Exception ex) {
        fetchConfigError("Unable to retrieve proxy configuration from remote server!", server + ": " + Throwables.getRootCause(ex));
        return null;
    } finally {
        synchronized (agentConfigurationExecutor) {
            // not been updated yet, restore last known set of agent metrics to be retried
            if (agentMetricsWorkingCopy != null && agentMetrics == null) {
                agentMetrics = agentMetricsWorkingCopy;
            }
        }
    }
    try {
        if (newConfig.currentTime != null) {
            Clock.set(newConfig.currentTime);
        }
        newConfig.validate(localAgent);
    } catch (Exception ex) {
        logger.log(Level.WARNING, "configuration file read from server is invalid", ex);
        try {
            agentAPI.agentError(agentId, "Configuration file is invalid: " + ex.toString());
        } catch (Exception e) {
            logger.log(Level.WARNING, "cannot report error to collector", e);
        }
        return null;
    }
    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) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) ClientErrorException(javax.ws.rs.ClientErrorException) FileNotFoundException(java.io.FileNotFoundException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) ProcessingException(javax.ws.rs.ProcessingException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ProcessingException(javax.ws.rs.ProcessingException) ConnectException(java.net.ConnectException)

Example 2 with AgentConfiguration

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

the class PushAgentTest method testIgnoreBackendSpanHeadSamplingPercent.

@Test
public void testIgnoreBackendSpanHeadSamplingPercent() {
    proxy.proxyConfig.backendSpanHeadSamplingPercentIgnored = true;
    proxy.proxyConfig.traceSamplingRate = 1.0;
    AgentConfiguration agentConfiguration = new AgentConfiguration();
    agentConfiguration.setSpanSamplingRate(0.5);
    proxy.processConfiguration(agentConfiguration);
    assertEquals(1.0, proxy.entityProps.getGlobalProperties().getTraceSamplingRate(), 1e-3);
    proxy.proxyConfig.backendSpanHeadSamplingPercentIgnored = false;
    proxy.processConfiguration(agentConfiguration);
    assertEquals(0.5, proxy.entityProps.getGlobalProperties().getTraceSamplingRate(), 1e-3);
}
Also used : AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) Test(org.junit.Test)

Example 3 with AgentConfiguration

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

the class ProxyCheckInSchedulerTest method testDontRetryCheckinOnBadCredentials.

@Test
public void testDontRetryCheckinOnBadCredentials() {
    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();
    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 ClientErrorException(Response.status(401).build())).once();
    replay(proxyV2API, apiContainer);
    try {
        ProxyCheckInScheduler scheduler = new ProxyCheckInScheduler(proxyId, proxyConfig, apiContainer, x -> fail("We are not supposed to get here"), () -> {
        }, () -> {
        });
        fail("We're not supposed to get here");
    } catch (RuntimeException e) {
    // 
    }
    verify(proxyConfig, proxyV2API, apiContainer);
}
Also used : APIContainer(com.wavefront.agent.api.APIContainer) ProxyV2API(com.wavefront.api.ProxyV2API) AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) ClientErrorException(javax.ws.rs.ClientErrorException) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with AgentConfiguration

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

the class ProxyCheckInSchedulerTest method testDontRetryCheckinOnMisconfiguredUrlThatEndsWithApi.

@Test
public void testDontRetryCheckinOnMisconfiguredUrlThatEndsWithApi() {
    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();
    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 ClientErrorException(Response.status(404).build())).once();
    replay(proxyV2API, apiContainer);
    try {
        ProxyCheckInScheduler scheduler = new ProxyCheckInScheduler(proxyId, proxyConfig, apiContainer, x -> fail("We are not supposed to get here"), () -> {
        }, () -> {
        });
        fail();
    } catch (RuntimeException e) {
    // 
    }
    verify(proxyConfig, proxyV2API, apiContainer);
}
Also used : APIContainer(com.wavefront.agent.api.APIContainer) ProxyV2API(com.wavefront.api.ProxyV2API) AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) ClientErrorException(javax.ws.rs.ClientErrorException) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with AgentConfiguration

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

the class ProxyCheckInSchedulerTest method testNormalCheckinWithRemoteShutdown.

@Test
public void testNormalCheckinWithRemoteShutdown() {
    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();
    returnConfig.setShutOffAgents(true);
    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);
    AtomicBoolean shutdown = new AtomicBoolean(false);
    ProxyCheckInScheduler scheduler = new ProxyCheckInScheduler(proxyId, proxyConfig, apiContainer, x -> {
    }, () -> shutdown.set(true), () -> {
    });
    scheduler.updateProxyMetrics();
    scheduler.updateConfiguration();
    verify(proxyConfig, proxyV2API, apiContainer);
    assertTrue(shutdown.get());
}
Also used : APIContainer(com.wavefront.agent.api.APIContainer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProxyV2API(com.wavefront.api.ProxyV2API) AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) UUID(java.util.UUID) Test(org.junit.Test)

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