cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Http Monitor - Pre Execution Script - Using custom JavaScript

BhanuSarabu
Visitor

How to use the below javascript statement in a http monitor pre-execution script?

 

var reandomId = (["DDDDDDDD"]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));

7 REPLIES 7

sia_h
Dynatrace Pro
Dynatrace Pro

Hi,

 

What do you want to use the variable for? You can have a look at the documentation see what use cases we have for pre-execution scripts.

 

Sia

Sia, I want to feed this variable to the request body.

I've referred to the documentation and couldn't figure out how to use this.

Script execution is getting failed.

Here is the link to the Synthetic HTTP Monitor script.

<Edited to remove the link>

Please check and let me know how to fix this. 

We don't recommend providing links to your monitors/ tenant in the public community. If you would like monitor specific advice I recommend that you open a chat or create a ticket. 

That said, you can see the cause of the script failure by checking the vuc-http-execution.log of the ActiveGate that you ran the monitor on. I can see that the failure is due to "Pre script execution failed due to: ReferenceError: \"crypto\" is not defined. "

Synthetic SME and community advocate.

Hi @BhanuSarabu , for security reasons, we discourage posting direct links to Dynatrace environments, as this is a public forum. You can read more about it in our Community guidelines.

Could you post the screenshot of the script for the Synthetic HTTP Monitor instead?

If you have any questions about the Community, you can contact me at maciej.neumann@dynatrace.com

Hi, Please find the details below:

1. I tested the script by passing the API request body in the pre-execution script and it worked fine.

var reqBody = "<api_req_body_json>";

request.setBody(reqBody);

 

2. Now I want to feed an auto generated Id to the request body. As part of that, I've modified the pre-execution script as below and it's failing.

Note: At this point, I'm not passing the Id to the "reqBody" variable. I want to do that once this executes fine.

var reandomId = (["DDDDDDDD"]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));

var reqBody = "<api_req_body_json>";

request.setBody(reqBody);

 

Synthetic Monitor Script Name: Auto Feed ID Testing

 

Please let me know for more details.

 

Hello, @HannahM! Is this some workaround that would be helpful for this case? Thank you for your feedback in advance!

If you would like to use the methods from the crypto library, you will need to add them to the pre-execution script, as it has no knowledge of it. You will need to paste all the code you need from the library into the pre/post execution script. There is a 50kb size limit for each pre/post-execution script, so be sure to use only the code you need and do a "minimize" on it.

Synthetic SME and community advocate.

Featured Posts