Skip to content

Commit

Permalink
Merge pull request #1755 from tmy1313/OCD-4767
Browse files Browse the repository at this point in the history
OCD-4767 - Reset URL in Datadog for Service Base URL Report
  • Loading branch information
kekey1 authored Dec 19, 2024
2 parents ba98f88 + 3f2e5d0 commit aef1fe8
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,11 @@
<KeyValuePair key="dd.span_id" value="%X{dd.span_id}" />
</JsonLayout>
</Console>
<Console name="fixDatadogUrlUptimeAssertionsJobJson" target="SYSTEM_OUT">
<JsonLayout compact="true" eventEol="true" properties="true" stacktraceAsString="true">
<KeyValuePair key="service" value="fixDatadogUrlUptimeAssertionsJob" />
<KeyValuePair key="dd.trace_id" value="%X{dd.trace_id}" />
<KeyValuePair key="dd.span_id" value="%X{dd.span_id}" />
</JsonLayout>
</Console>
</Appenders>
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,16 @@
interval="1" modulate="true" />
</Policies>
</RollingFile>
<RollingFile name="fixDatadogUrlUptimeAssertionsJob"
fileName="${logDir}/scheduler/fixDatadogUrlUptimeAssertionsJob.log"
filePattern="${logDir}/scheduler/history/fixDatadogUrlUptimeAssertionsJob-%d{yyyy-MM-dd}.log"
filePermissions="rw-rw-r--">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p (%t) [%C{1}(%M:%L)] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy
interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,7 @@
<Logger name="updatedSedFriendlyIdsJobLogger" level="INFO" additivity="false">
<AppenderRef ref="updatedSedFriendlyIdsJob" />
</Logger>
<Logger name="fixDatadogUrlUptimeAssertionsJobLogger" level="INFO" additivity="false">
<AppenderRef ref="fixDatadogUrlUptimeAssertionsJob" />
</Logger>
</Loggers>
4 changes: 4 additions & 0 deletions chpl/chpl-api/src/main/resources/log4j2-xinclude-loggers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,8 @@
<AppenderRef ref="updatedSedFriendlyIdsJob" />
<AppenderRef ref="updatedSedFriendlyIdsJobJson" />
</Logger>
<Logger name="fixDatadogUrlUptimeAssertionsJobLogger" level="INFO" additivity="false">
<AppenderRef ref="fixDatadogUrlUptimeAssertionsJob" />
<AppenderRef ref="FixDatadogUrlUptimeAssertionsJobJson" />
</Logger>
</Loggers>
9 changes: 9 additions & 0 deletions chpl/chpl-resources/src/main/resources/jobs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -921,5 +921,14 @@
<durability>true</durability>
<recover>false</recover>
</job>

<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>
<job-class>gov.healthit.chpl.scheduler.job.urluptime.FixDatadogUrlUptimeAssertionsJob</job-class>
<durability>true</durability>
<recover>false</recover>
</job>
</schedule>
</job-scheduling-data>
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public DatadogSyntheticsTestService(DatadogSyntheticsTestApiProvider apiProvider
this.datadogTestLocation = datadogTestLocation;
}

protected DatadogSyntheticsTestApiProvider getApiProvider() {
return apiProvider;
}

public List<SyntheticsTestDetails> getAllSyntheticsTests() {
try {
return apiProvider.getApiInstance().listTests().getTests();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package gov.healthit.chpl.scheduler.job.urluptime;

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

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.datadog.api.client.ApiException;
import com.datadog.api.client.v1.model.SyntheticsAPITest;
import com.datadog.api.client.v1.model.SyntheticsAPITestConfig;
import com.datadog.api.client.v1.model.SyntheticsAPITestType;
import com.datadog.api.client.v1.model.SyntheticsAssertion;
import com.datadog.api.client.v1.model.SyntheticsAssertionOperator;
import com.datadog.api.client.v1.model.SyntheticsAssertionTarget;
import com.datadog.api.client.v1.model.SyntheticsAssertionType;
import com.datadog.api.client.v1.model.SyntheticsTestOptions;
import com.datadog.api.client.v1.model.SyntheticsTestOptionsHTTPVersion;
import com.datadog.api.client.v1.model.SyntheticsTestOptionsScheduling;
import com.datadog.api.client.v1.model.SyntheticsTestOptionsSchedulingTimeframe;
import com.datadog.api.client.v1.model.SyntheticsTestRequest;

import gov.healthit.chpl.scheduler.job.QuartzJob;
import gov.healthit.chpl.util.DateUtil;
import lombok.extern.log4j.Log4j2;

@Log4j2(topic = "fixDatadogUrlUptimeAssertionsJobLogger")
public class FixDatadogUrlUptimeAssertionsJob extends QuartzJob {

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;


@Autowired
private DatadogSyntheticsTestService datadogSyntheticsTestService;

@Value("${datadog.syntheticsTest.startTime}")
private String datadogTestStartTime;

@Value("${datadog.syntheticsTest.endTime}")
private String datadogTestEndTime;

@Value("${datadog.syntheticsTest.checkEveryMinutes}")
private Long datadogCheckEveryMinutes;

@Value("${datadog.syntheticsTest.timeout}")
private Integer datadogTestTimeout;

@Value("${datadog.syntheticsTest.location}")
private String datadogTestLocation;

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
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);
}
});

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

}

0 comments on commit aef1fe8

Please sign in to comment.