Skip to content

Commit

Permalink
feat: Handle Service Base URL Datadog "Body not empty" check failure
Browse files Browse the repository at this point in the history
OCD-4785
  • Loading branch information
tmy1313 authored Jan 17, 2025
1 parent dee649e commit 36e7424
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,6 @@
</Logger>
<Logger name="fixDatadogUrlUptimeAssertionsJobLogger" level="INFO" additivity="false">
<AppenderRef ref="fixDatadogUrlUptimeAssertionsJob" />
<AppenderRef ref="FixDatadogUrlUptimeAssertionsJobJson" />
<AppenderRef ref="fixDatadogUrlUptimeAssertionsJobJson" />
</Logger>
</Loggers>
2 changes: 1 addition & 1 deletion chpl/chpl-resources/src/main/resources/jobs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@
<job>
<name>fixDatadogUrlUptimeAssertionsJob</name>
<group>systemJobs</group>
<description>Fix Datadog Url Uptime Assertions Job (BEWARE - THIS JOB CAN ONLY BE RUN ONCE IN EACH DATADOG ENVITRONMENT!)</description>
<description>Fix Datadog Url Uptime Assertions Job (BEWARE - THIS JOB CAN ONLY BE RUN ONCE IN EACH DATADOG ENVIRONMENT!)</description>
<job-class>gov.healthit.chpl.scheduler.job.urluptime.FixDatadogUrlUptimeAssertionsJob</job-class>
<durability>true</durability>
<recover>false</recover>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@Log4j2(topic = "serviceBaseUrlListUptimeCreatorJobLogger")
@Component
public class DatadogSyntheticsTestService {
public static final String NOT_EMPTY_REGEX = "/[\\S]+/";
private static final Integer HTTP_STATUS_OK = 200;
private static final String HTTP_METHOD_GET = "GET";
private static final Long SECONDS_IN_A_MINUTE = 60L;
Expand Down Expand Up @@ -110,7 +111,7 @@ public SyntheticsAPITest createSyntheticsTest(String url, List<Long> developerId
.type(SyntheticsAssertionType.STATUS_CODE)),
new SyntheticsAssertion(new SyntheticsAssertionTarget()
.operator(SyntheticsAssertionOperator.MATCHES)
.target("/[\\S\s]+[\\S]+/")
.target(NOT_EMPTY_REGEX)
.type(SyntheticsAssertionType.BODY))))
.request(new SyntheticsTestRequest()
.url(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
Expand Down Expand Up @@ -61,85 +59,70 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
LOGGER.info("********* Starting the Fix Datadog Url Uptime Assertions Job *********");

datadogSyntheticsTestService.getAllSyntheticsTests().forEach(test -> {
Optional<SyntheticsAssertion> contentHeaderAssertion = getContentHeaderAssertionExist(test.getConfig().getAssertions());
String url = test.getConfig().getRequest().getUrl();

if (contentHeaderAssertion.isPresent()) {

SyntheticsAPITest body = new SyntheticsAPITest()
.config(new SyntheticsAPITestConfig()
.assertions(Arrays.asList(
new SyntheticsAssertion(new SyntheticsAssertionTarget()
.operator(SyntheticsAssertionOperator.LESS_THAN)
.target(datadogTestTimeout)
.type(SyntheticsAssertionType.RESPONSE_TIME)),
new SyntheticsAssertion(new SyntheticsAssertionTarget()
.operator(SyntheticsAssertionOperator.IS)
.target(HTTP_STATUS_OK)
.type(SyntheticsAssertionType.STATUS_CODE)),
new SyntheticsAssertion(new SyntheticsAssertionTarget()
.operator(SyntheticsAssertionOperator.MATCHES)
.target("/[\\S\s]+[\\S]+/")
.type(SyntheticsAssertionType.BODY))))
.request(new SyntheticsTestRequest()
.url(url)
.method(HTTP_METHOD_GET)))
.options(new SyntheticsTestOptions()
.httpVersion(SyntheticsTestOptionsHTTPVersion.ANY)
.minFailureDuration(0L)
.minLocationFailed(1L)
.scheduling(new SyntheticsTestOptionsScheduling()
.timezone(DateUtil.ET_ZONE_ID)
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.MONDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.TUESDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.WEDNESDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.THURSDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.FRIDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime)))
.tickEvery(convertMinutesToSeconds(datadogCheckEveryMinutes)))
.locations(Collections.singletonList(datadogTestLocation))
.message("Failed: " + url)
.type(SyntheticsAPITestType.API)
.name(url)
.tags(test.getTags());

try {
datadogSyntheticsTestService.getApiProvider().getApiInstance().updateAPITest(test.getPublicId(), body);
LOGGER.info("Test updated: {}", url);
} catch (ApiException e) {
LOGGER.error("Could not update test for URL: {}", url, e);
}
} else {
LOGGER.info("Test NOT updated: {}", url);
SyntheticsAPITest body = new SyntheticsAPITest()
.config(new SyntheticsAPITestConfig()
.assertions(Arrays.asList(
new SyntheticsAssertion(new SyntheticsAssertionTarget()
.operator(SyntheticsAssertionOperator.LESS_THAN)
.target(datadogTestTimeout)
.type(SyntheticsAssertionType.RESPONSE_TIME)),
new SyntheticsAssertion(new SyntheticsAssertionTarget()
.operator(SyntheticsAssertionOperator.IS)
.target(HTTP_STATUS_OK)
.type(SyntheticsAssertionType.STATUS_CODE)),
new SyntheticsAssertion(new SyntheticsAssertionTarget()
.operator(SyntheticsAssertionOperator.MATCHES)
.target(DatadogSyntheticsTestService.NOT_EMPTY_REGEX)
.type(SyntheticsAssertionType.BODY))))
.request(new SyntheticsTestRequest()
.url(url)
.method(HTTP_METHOD_GET)))
.options(new SyntheticsTestOptions()
.httpVersion(SyntheticsTestOptionsHTTPVersion.ANY)
.minFailureDuration(0L)
.minLocationFailed(1L)
.scheduling(new SyntheticsTestOptionsScheduling()
.timezone(DateUtil.ET_ZONE_ID)
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.MONDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.TUESDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.WEDNESDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.THURSDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime))
.addTimeframesItem(new SyntheticsTestOptionsSchedulingTimeframe()
.day(DatadogDayOfWeek.FRIDAY)
.from(datadogTestStartTime)
.to(datadogTestEndTime)))
.tickEvery(convertMinutesToSeconds(datadogCheckEveryMinutes)))
.locations(Collections.singletonList(datadogTestLocation))
.message("Failed: " + url)
.type(SyntheticsAPITestType.API)
.name(url)
.tags(test.getTags());

try {
datadogSyntheticsTestService.getApiProvider().getApiInstance().updateAPITest(test.getPublicId(), body);
LOGGER.info("Test updated: {}", url);
} catch (ApiException e) {
LOGGER.error("Could not update test for URL: {}", url, e);
}
});

LOGGER.info("********* Completed the Fix Datadog Url Uptime Assertions Job *********");
}


private Optional<SyntheticsAssertion> getContentHeaderAssertionExist(List<SyntheticsAssertion> assertions) {
return assertions.stream()
.filter(assertion -> assertion.getSyntheticsAssertionTarget().getType().equals(SyntheticsAssertionType.HEADER)
&& assertion.getSyntheticsAssertionTarget().getProperty().equals("content-length"))
.findAny();

}

private Long convertMinutesToSeconds(Long minutes) {
return minutes * SECONDS_IN_A_MINUTE;
}
Expand Down

0 comments on commit 36e7424

Please sign in to comment.