Web applications and services are often hosted in multiple data centers (primary and backup). If performance degrades at the primary source, a failover may be necessary, shifting traffic to the backup location. There is a series of checks that must be done before the data center switch is triggered. You can use Catchpoint to detect performance degradation and automatically run the necessary checks with the help of Catchpoint's Transaction Test. We will also rely on a Ping Test, API test, and Catchpoint's REST API to accomplish the use case.

Ping test: Monitors the health and reachability of the primary data center.
Transaction test: Transaction test to automate the series of manual checks.
*Note: The ping test will run continuously to monitor data center health. The transaction test needs to be activated or deactivated based on the outcome of the ping test.
Implementation
To bridge the Ping and Transaction Test, we will leverage the API JavaScript test. This API test will use the REST API to check the health of the ping test and then activate or deactivate the Transaction Test.
How this works:
- A ping test monitors the health of the primary data center based on a set frequency.
- Results from this ping test, including the # Test Errors metric, are plotted in a favorite chart.
- A Transaction Test to perform a series of checks is configured and set as inactive.
- The JavaScript test checks the content in the favorite chart and triggers the execution of the Transaction Test if the # Test Errors metric exceeds a threshold. This is all done using Catchpoint's REST API.
Building JavaScript API script:
- Generate authentication token to use REST API.
setNavigatePostData("grant_type=client_credentials&client_id=<YOUR_KEY>&client_secret=<YOUR_SECRET>");
open("https://io.catchpoint.com/ui/api/token");
var res = Catchpoint.extract('resp-content','(?s).*');
obj = JSON.parse(res);
var token = obj.access_token;
- Next, use the REST API to collect favorite chart data for the last five minutes. The below code generates a dynamic date based on the current time. Replace the favorite charts id.
var d = new Date();
var myStartDate = new Date(d - 5 * 60000);
var start=""+(d.getUTCMonth()+1)+"-"+d.getUTCDate()+"-"+d.getUTCFullYear()+" "+d.getUTCHours()+":"+d.getUTCMinutes();
var end=""+(myStartDate.getUTCMonth()+1)+"-"+myStartDate.getUTCDate()+"-"+myStartDate.getUTCFullYear()+" "+myStartDate.getUTCHours()+":"+myStartDate.getUTCMinutes();
setHeader(`authorization`, `Bearer ${token}`); open(`https://io.catchpoint.com/ui/api/v1/performance/favoriteCharts/<favorite_Charts_id>/data?useUTC=true&startTime=${end}&endTime=${start}`);
// Extract #Test_Error metric from favourite chart response.
var res_Step2 = Catchpoint.extract('resp-content','(?s).*');
obj_Step2 = JSON.parse(res_Step2);
var Test_Error = obj_Step2.summary.items[0].synthetic_metrics[1];
- Once favorite chart data is fetched, add a condition to check the value of
#test_Error. If it’s more than or equal to 2 for the past five minutes, activate the Transaction Test. If#test_erroris less than two, deactivate the Transaction Test. Replace the Transaction Test ID.
// Activate or deactivate the test based on #test_Errors. [#Test_Error >= 2 - Activate ; #Test_Error < 2 - Inctivate ]
if(Test_Error<2){
setHeader(`content-type`, `application/json`)
setHeader(`authorization`, `Bearer ${token}`)
// Payload passed will be different based on the test setup and its configuration.
setNavigatePostData('{"status":{"id":1}}')
open("https://io.catchpoint.com/ui/api/v1/tests/updateStatus?tests=<transaction_test_id>")
}
else if(Test_Error>=2){
setHeader(`content-type`, `application/json`)
setHeader(`authorization`, `Bearer ${token}`)
// Payload passed will be different based on the test setup and its configuration.
setNavigatePostData('{"status":{"id":0}}')
open("https://io.catchpoint.com/ui/api/v1/tests/updateStatus?tests=<transaction_test_id>")
}