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);
}
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
}
}
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;
}
Aggregations