Skip to content

Commit

Permalink
fix: report agent status exception (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3 authored Aug 4, 2023
1 parent 0ec6f84 commit 31ceccc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ private void timedReportStatus() {
return;
}
reportStatusTask = TimerService.scheduleAtFixedRate(() -> {
ConfigService.INSTANCE.reportStatus();
// Load agent config according to last modified time
if (ConfigService.INSTANCE.reloadConfig()) {
install();
try {
ConfigService.INSTANCE.reportStatus();
// Load agent config according to last modified time
if (ConfigService.INSTANCE.reloadConfig()) {
install();
}
} catch (Exception e) {
LOGGER.error("[AREX] Report status error.", e);
}
}, 1, 1, TimeUnit.MINUTES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public void loadAgentConfig() {

HttpClientResponse clientResponse = AsyncHttpClientUtil.postAsyncWithJson(CONFIG_LOAD_URI, requestJson, null).join();
if (clientResponse == null) {
LOGGER.warn("[arex] Load agent config, response is null, pause recording");
LOGGER.warn("[AREX] Load agent config, response is null, pause recording");
ConfigManager.INSTANCE.setConfigInvalid();
return;
}

LOGGER.info("[arex] Load agent config\nrequest: {}\nresponse: {}", requestJson, clientResponse.getBody());
LOGGER.info("[AREX] Load agent config\nrequest: {}\nresponse: {}", requestJson, clientResponse.getBody());

if (StringUtil.isEmpty(clientResponse.getBody()) || "{}".equals(clientResponse.getBody())) {
LOGGER.warn("[arex] Load agent config, response is null, pause recording");
LOGGER.warn("[AREX] Load agent config, response is null, pause recording");
ConfigManager.INSTANCE.setConfigInvalid();
return;
}
Expand All @@ -90,12 +90,13 @@ public void loadAgentConfig() {
if (configResponse == null || configResponse.getBody() == null ||
configResponse.getBody().getServiceCollectConfiguration() == null) {
ConfigManager.INSTANCE.setConfigInvalid();
LOGGER.warn("[arex] Load agent config, deserialize response is null, pause recording");
LOGGER.warn("[AREX] Load agent config, deserialize response is null, pause recording");
return;
}
ConfigManager.INSTANCE.updateConfigFromService(configResponse.getBody());
} catch (Throwable e) {
LOGGER.warn("[arex] Load agent config error", e);
LOGGER.warn("[AREX] Load agent config error, pause recording. exception message: {}", e.getMessage());
ConfigManager.INSTANCE.setConfigInvalid();
}
}

Expand Down Expand Up @@ -190,17 +191,25 @@ public void report() {
Map<String, String> requestHeaders = MapUtils.newHashMapWithExpectedSize(1);
requestHeaders.put("If-Modified-Since", prevLastModified);

HttpClientResponse response = AsyncHttpClientUtil.postAsyncWithJson(AGENT_STATUS_URI, requestJson,
requestHeaders).join();
HttpClientResponse response;

try {
response = AsyncHttpClientUtil.postAsyncWithJson(AGENT_STATUS_URI, requestJson, requestHeaders).join();
} catch (Exception e) {
LOGGER.warn("[AREX] Report agent status error, {}", e.getMessage());
return;
}

if (response == null || MapUtils.isEmpty(response.getHeaders())) {
LOGGER.info("[arex] Report agent status response is null.\nrequest: {}", requestJson);
LOGGER.info("[AREX] Report agent status response is null. request: {}", requestJson);
return;
}

// Tue, 15 Nov 1994 12:45:26 GMT, see https://datatracker.ietf.org/doc/html/rfc7232#section-3.3
String lastModified = response.getHeaders().get("Last-Modified");
LOGGER.info("[arex] Report agent status lastModified: {}.\nrequest: {}", lastModified, requestJson);
String lastModified = response.getHeaders()
.getOrDefault("Last-Modified", response.getHeaders().get("last-modified"));
LOGGER.info("[AREX] Report agent status, previous lastModified: {}, lastModified: {}. request: {}",
prevLastModified, lastModified, requestJson);
if (StringUtil.isEmpty(lastModified)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void completed(HttpResponse response) {
try {
clientResponse.setBody(responseHandler.handle(response.getEntity()));
} catch (Exception e) {
LogManager.warn("completed", e);
LogManager.warn("ahc.completed", e.getMessage());
}
}

Expand All @@ -55,14 +55,14 @@ public void completed(HttpResponse response) {
public void failed(Exception e) {
LogManager.setContextMap(contextMap);
responseFuture.completeExceptionally(e);
LogManager.warn("failed", e);
LogManager.warn("ahc.failed", e.getMessage());
}

@Override
public void cancelled() {
LogManager.setContextMap(contextMap);
responseFuture.completeExceptionally(new InterruptedException("Request has been cancelled."));
LogManager.warn("cancelled", "Request has been cancelled.");
LogManager.warn("ahc.cancelled", "Request has been cancelled.");
}

private Map<String, String> convertResponseHeaders(Header[] headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ void loadAgentConfig() {
ConfigManager.INSTANCE.setStorageServiceMode("mock-not-local");
try (MockedStatic<AsyncHttpClientUtil> ahc = mockStatic(AsyncHttpClientUtil.class);
MockedStatic<NetUtils> netUtils = mockStatic(NetUtils.class)){
// response has error
CompletableFuture<HttpClientResponse> errorFuture = new CompletableFuture<>();
errorFuture.completeExceptionally(new RuntimeException("mock error"));
ahc.when(() -> AsyncHttpClientUtil.postAsyncWithJson(anyString(), anyString(), anyMap())).thenReturn(errorFuture);
assertEquals(DELAY_MINUTES, ConfigService.INSTANCE.loadAgentConfig(null));
assertEquals(0, ConfigManager.INSTANCE.getRecordRate());
assertEquals(EnumSet.noneOf(DayOfWeek.class), ConfigManager.INSTANCE.getAllowDayOfWeeks());
assertEquals(AgentStatusEnum.UN_START, ConfigService.INSTANCE.getAgentStatus());

// clientResponse is null
ahc.when(() -> AsyncHttpClientUtil.postAsyncWithJson(anyString(), anyString(), eq(null))).thenReturn(CompletableFuture.completedFuture(null));
assertEquals(DELAY_MINUTES, ConfigService.INSTANCE.loadAgentConfig(null));
Expand Down Expand Up @@ -138,6 +147,12 @@ void reportStatus() {
try (MockedStatic<AsyncHttpClientUtil> ahc = mockStatic(AsyncHttpClientUtil.class);
MockedStatic<NetUtils> netUtils = mockStatic(NetUtils.class)){
netUtils.when(NetUtils::getIpAddress).thenReturn("127.0.0.1");
// response has error
CompletableFuture<HttpClientResponse> errorFuture = new CompletableFuture<>();
errorFuture.completeExceptionally(new RuntimeException("mock error"));
ahc.when(() -> AsyncHttpClientUtil.postAsyncWithJson(anyString(), anyString(), anyMap())).thenReturn(errorFuture);
ConfigService.INSTANCE.reportStatus();
assertFalse(ConfigService.INSTANCE.reloadConfig());

// response header is empty
ahc.when(() -> AsyncHttpClientUtil.postAsyncWithJson(anyString(), anyString(), anyMap())).thenReturn(
Expand Down

0 comments on commit 31ceccc

Please sign in to comment.