13 Jun 2025
08:11 PM
- last edited on
16 Jun 2025
07:38 AM
by
MaciejNeumann
I have a synthetic click path monitor.
step 1 navigates to the url. with basic auth.
Nabs a value in the response body.
this is the actual response:
"responseBody": "{\"id\":\"activeCallsIn\",\"value\":119}",
I have Step 2 event to do "JavaScript"
Here is the script.
// Get the response body from the prior API call (assumes response is a valid object)
var responseBody = response.getResponseBody();
// "{\"id\":\"activeCallsIn\",\"value\":148}"; (if I put this data into above variable I get a metric into Dynatace)
// Parse the response JSON to extract metric data
var metricData = JSON.parse(responseBody);
// Construct the Dynatrace metric payload using the id and value from the response
// Example: "company.activeCallsIn 119"
var payload = "company." + metricData.id + " " + metricData.value;
// Dynatrace API endpoint for metric ingestion
var url = "https://mytenant.dynatrace.com/api/v2/metrics/ingest";
// Log the processed metric for debugging
api.info("Metric Processed: " + payload);
api.startAsyncSyntheticEvent();
// Send the metric to Dynatrace
fetch(url, {
method: "POST",
headers: {
'Content-Type': 'text/plain',
'Authorization': 'Api-Token ' + api.getCredential('CREDENTIALS_VAULT-MyCrEDS', 'token')
},
body: payload
}).then(response => {
if (!response.ok) throw response.statusText;
api.finish();
}).catch(error => {
api.fail("Fetch failed: " + error);
});
My issue is that it states, response is not defined. When looking at the executions each step clearly shows my response from the original api. The screenshot shows all the details of the response but I am unable to get that value into those variables. Anyone have any ideas?
13 Jun 2025 11:26 PM
I am pretty sure the issue here is that trying to set the responseBody variable in the context of the second script step is blowing things up because storing that value would have to happen as part of the first step. I am not sure if that's possible within a Clickpath monitor though..
Based on what you indicated in your original message, it sounds like a multi-step HTTP monitor may be a better fit. In HTTP monitors, you have the option to use a combination of post-execution and pre-execution scripts to pass variable data between steps. The documentation has a pretty good example of passing variables.
I hope this helps.
16 Jun 2025 05:26 PM
I am still not able to get anywhere.
I have one http request (get) with post script here:
var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);
// Construct the Dynatrace metric payload using the id and value from the response
//Example ResponseBody = "{\"id\":\"activeCallsIn\",\"value\":148}";
// Example: "company.activeCallsIn 119"
var payload = "company." + metricData.id + " " + metricData.value;
// Dynatrace API endpoint for metric ingestion
var url = "https://{environmentid}.live.dynatrace.com/api/v2/metrics/ingest";
// Log the processed metric for debugging
api.info("Metric Processed: " + payload);
api.startAsyncSyntheticEvent();
// Send the metric to Dynatrace
fetch(url, {
method: "POST",
headers: {
'Content-Type': 'text/plain',
'Authorization': 'Api-Token ' + api.getCredential("CREDENTIALS_VAULT-DAJFKHJIGREJK8R4", "token")
},
body: payload
}).then(response => {
if (!response.ok) throw response.statusText;
api.finish();
}).catch(error => {
api.fail("Fetch failed: " + error);
});
I am trying to do this now as a post script in 1 step but still fails at post script. No code issues so far??? Need assistance.
16 Jun 2025 06:35 PM
Just a question: Is this an actual synthetic test that has success/failure criteria, or more of a method for getting metric data from an external source into Dynatrace?