Skip to content

Commit

Permalink
feat: allow BOSY_TOO_LARGE_TO_PROCCESS to be treated as a successful …
Browse files Browse the repository at this point in the history
…test

OCD-4736
  • Loading branch information
tmy1313 committed Nov 13, 2024
1 parent 80d2dd5 commit a5b7c64
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.datadog.api.client.ApiException;
import com.datadog.api.client.v1.api.SyntheticsApi.GetAPITestLatestResultsOptionalParameters;
import com.datadog.api.client.v1.model.SyntheticsAPITestResultFull;
import com.datadog.api.client.v1.model.SyntheticsAPITestResultShort;
import com.datadog.api.client.v1.model.SyntheticsGetAPITestLatestResultsResponse;

Expand All @@ -23,6 +24,7 @@ public class DatadogSyntheticsTestResultService {

private DatadogSyntheticsTestApiProvider apiProvider;


@Autowired
public DatadogSyntheticsTestResultService(DatadogSyntheticsTestApiProvider apiProvider) {
this.apiProvider = apiProvider;
Expand Down Expand Up @@ -56,6 +58,15 @@ public List<SyntheticsAPITestResultShort> getSyntheticsTestResults(String public
return testResults;
}

public SyntheticsAPITestResultFull getDetailedTestResult(String publicTestKey, String resultId) {
try {
return apiProvider.getApiInstance().getAPITestResult(publicTestKey, resultId);
} catch (ApiException e) {
LOGGER.error("Could not retrieve test result details for: {} | {}", publicTestKey, resultId);
return null;
}
}

private Long getMostRecentTimestamp(List<SyntheticsAPITestResultShort> testResults) {
return testResults.stream()
.mapToLong(result -> Math.round(result.getCheckTime()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.datadog.api.client.v1.model.SyntheticsAPITestResultFull;
import com.datadog.api.client.v1.model.SyntheticsAPITestResultShort;
import com.datadog.api.client.v1.model.SyntheticsApiTestFailureCode;
import com.datadog.api.client.v1.model.SyntheticsTestDetails;

import gov.healthit.chpl.domain.Developer;
Expand All @@ -38,6 +41,8 @@ public class DatadogUrlUptimeSynchonizer {
private UrlUptimeMonitorDAO urlUptimeMonitorDAO;
private UrlUptimeMonitorTestDAO urlUptimeMonitorTestDAO;

private List<String> errorsToIgnore;


public DatadogUrlUptimeSynchonizer(DatadogSyntheticsTestService datadogSyntheticsTestService, DatadogSyntheticsTestResultService datadogSyntheticsTestResultService,
ServiceBaseUrlListService serviceBaseUrlListGatherer, UrlUptimeMonitorDAO urlUptimeMonitorDAO, UrlUptimeMonitorTestDAO urlUptimeMonitorTestDAO) {
Expand All @@ -46,6 +51,8 @@ public DatadogUrlUptimeSynchonizer(DatadogSyntheticsTestService datadogSynthetic
this.serviceBaseUrlListService = serviceBaseUrlListGatherer;
this.urlUptimeMonitorDAO = urlUptimeMonitorDAO;
this.urlUptimeMonitorTestDAO = urlUptimeMonitorTestDAO;

errorsToIgnore = List.of("BODY_TOO_LARGE_TO_PROCESS");
}

@Transactional
Expand All @@ -62,16 +69,36 @@ private void synchronizeUrlUptimeMonitorTestsWithDatadogSyntheticsTestResults()

getDatesToRetrieveResultsFor().stream()
.peek(testDate -> LOGGER.info("**************** Retrieving test results for: {} ****************", testDate))
.forEach(testDate -> urlUptimeMonitorDAO.getAll()
.forEach(urlUptimeMonitor -> datadogSyntheticsTestResultService.getSyntheticsTestResults(
getDatadogPublicId(syntheticsTestDetails, urlUptimeMonitor.getUrl(), urlUptimeMonitor.getDeveloper().getId()),
testDate)
.forEach(syntheticsTestResult -> urlUptimeMonitorTestDAO.create(UrlUptimeMonitorTest.builder()
.urlUptimeMonitorId(urlUptimeMonitor.getId())
.datadogTestKey(syntheticsTestResult.getResultId())
.checkTime(toLocalDateTime(syntheticsTestResult.getCheckTime().longValue()))
.passed(syntheticsTestResult.getResult().getPassed())
.build()))));
.forEach(testDate -> urlUptimeMonitorDAO.getAll().forEach(urlUptimeMonitor -> {
String publicId = getDatadogPublicId(syntheticsTestDetails, urlUptimeMonitor.getUrl(), urlUptimeMonitor.getDeveloper().getId());

datadogSyntheticsTestResultService.getSyntheticsTestResults(publicId, testDate).forEach(syntheticsTestResult -> {
urlUptimeMonitorTestDAO.create(UrlUptimeMonitorTest.builder()
.urlUptimeMonitorId(urlUptimeMonitor.getId())
.datadogTestKey(syntheticsTestResult.getResultId())
.checkTime(toLocalDateTime(syntheticsTestResult.getCheckTime().longValue()))
.passed(calculatePassed(syntheticsTestResult, publicId))
.build());
});
}));

}

private boolean calculatePassed(SyntheticsAPITestResultShort result, String publicId) {
if (result.getResult().getPassed()) {
return true;
} else {
SyntheticsAPITestResultFull detailedResult = datadogSyntheticsTestResultService.getDetailedTestResult(publicId, result.getResultId());
return isErrorIgnorable(detailedResult.getResult().getFailure().getCode());
}

}

private boolean isErrorIgnorable(SyntheticsApiTestFailureCode errorCode) {
return errorsToIgnore.stream()
.filter(code -> code.equals(errorCode.getValue()))
.findAny()
.isPresent();
}

private void synchronizeDatadogSyntheticsTestsWithServiceBaseUrlLists() {
Expand Down Expand Up @@ -189,7 +216,8 @@ private void addUrlUptimeMonitor(UrlUptimeMonitor urlUptimeMonitor) {

private List<LocalDate> getDatesToRetrieveResultsFor() {
List<LocalDate> datesToRetrieveResultsFor = new ArrayList<LocalDate>();
for (Long i = 1L; i <= DAYS_TO_LOOK_BACK_FOR_RESULTS; ++i) {
//for (Long i = 1L; i <= DAYS_TO_LOOK_BACK_FOR_RESULTS; ++i) {
for (Long i = 0L; i <= DAYS_TO_LOOK_BACK_FOR_RESULTS; ++i) {
if (!LocalDate.now().minusDays(i).getDayOfWeek().equals(DayOfWeek.SATURDAY)
&& !LocalDate.now().minusDays(i).getDayOfWeek().equals(DayOfWeek.SUNDAY)
&& !doUrlUptimeMonitorTestsExistInDbForDate(LocalDate.now().minusDays(i))) {
Expand Down

0 comments on commit a5b7c64

Please sign in to comment.