Synthetic Monitoring
Browser monitors, HTTP monitors, synthetic locations.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is it possible to make a synchroniuos HTTP call from Synthetic monitor javascript execution step?

deni
Pro

Hi,

This is a follow-up to my earlier post:
https://community.dynatrace.com/t5/Synthetic-Monitoring/Synthetic-monitor-counting-api-fail-by-messa...

Could someone please confirm whether it is possible or not possible to make a synchronous HTTP call from a Synthetic Monitor JavaScript execution step?
If it is not possible, could you please point me to the official documentation where this behavior is described?

Thanks!

Regards, Deni 

Dynatrace Integration Engineer at CodeAttest
2 REPLIES 2

Julius_Loman
DynaMight Legend
DynaMight Legend

@deni  , for HTTP synthetic tests, this is not possible, see this: https://docs.dynatrace.com/docs/shortlink/http-monitor-pre-post-script#define-pre-and-post-execution...


Pre- and post-execution scripts have direct access only to the data retrieved as the result of the current request. In other words, the response details are available only within the same request’s post-execution script. To pass the information to a different request within the same monitor, use a variable instead.

For Browser synthetic tests, this is certainly possible, for example, this one taken from the docs:

api.startAsyncSyntheticEvent();
fetch('https://randomuser.me/api/').then((resp) => resp.json()).then(function(data) {
    document.getElementById('firstName').value = data.results[0].name.first;
    document.getElementById('lastName').value = data.results[0].name.lastname;
    api.finish();
}).catch(function(error) {
    api.fail('Fetch request to randomuser.me failed');
});

Here you need to call api.startAsyncSyntheticEvent() so that the synthetic engine waits for api.fail() or api.finish() call. See docs.

Dynatrace Ambassador | Alanata a.s., Slovakia, Dynatrace Master Partner

Thanks @Julius_Loman 

My question was related only to Browser Synthetic Monitors.

The issue was caused by an incorrect placement of api.startAsyncSyntheticEvent().

In my original script, I had synchronous logic and an early return before calling api.startAsyncSyntheticEvent():

// Some synchronous code
var text = document.documentElement.innerText || '';
var someCheck = ...;

if (!someCheck) {
    return;
}

api.startAsyncSyntheticEvent(); // ❌ too late

fetch(...)
    .then(function(res) {
        if (!res.ok) {
            api.fail("FAIL_1");
            return;
        }
        api.fail("FAIL_2");
    })
    .catch(function(err) {
        api.fail("FAIL_3");
    });

 

Because api.startAsyncSyntheticEvent() was not called at the beginning of the JavaScript execution step, the Synthetic engine treated the step as synchronous and immediately moved on to the next step.
As a result, the asynchronous fetch() completed after the step had already finished, and api.fail() was ignored, which made it look like synchronous execution was not supported.

Once I moved api.startAsyncSyntheticEvent() to the very beginning of the JavaScript execution step and ensured that all execution paths end with either api.finish() or api.fail(), the monitor behaved as expected and failed on the JavaScript step with the correct failure message.

 

 
api.startAsyncSyntheticEvent();

// logic ...

fetch(...)
    .then(...)
    .catch(...);

 

So async HTTP calls are fully supported in Browser Synthetic Monitors, as long as api.startAsyncSyntheticEvent() is invoked before any possible return path.

Dynatrace Integration Engineer at CodeAttest

Featured Posts