Return the actual request object inside the result to the statusCallback function
phaakma opened this issue · 2 comments
My use case here is to use the statusCallback function to log info about each request to a csv file for further analysis. I am using a custom requestGenerator to generate requests with random values, and also want to be able to 'tag' or otherwise label each request so they can be grouped in the analysis.
Currently, the statusCallback function receives three objects: error, result and latency. The result object is what returns information on the current request. It currently provides access to info such as the host, path, method, elapsed time, etc.
My challenge was how to access information generated within the custom requestGenerator from the statusCallback function.
My current solution is as follows. I found where the result object is created, around line 250 in the file httpClient.js. It looks like this:
const result = {
host: client._host,
path: connection.req.path,
method: connection.req.method,
statusCode: connection.statusCode,
body: body,
headers: connection.headers
};
This object has specific attributes, but I wanted to pass adhoc ones. So I added one attribute to this object, the actual request itself, like so:
const result = {
host: client._host,
path: connection.req.path,
method: connection.req.method,
statusCode: connection.statusCode,
body: body,
headers: connection.headers,
request: connection.req,
};
In this way I can do these sorts of things in the requestGenerator function:
request.pathParameters = message;
const labels = ["Bananas", "Grapes", "Peaches"]
request.label = labels[Math.floor(Math.random() * labels.length)]
Then in the statusCallback function I can access these like this:
console.log(result.request.pathParameters)
console.log(result.request.label)
...and can of course log them to file.
This works fine in my particular use case. I'm interested to know if anyone can think of a better way, or can see any potential problems, and/or might adding the actual request object to the result be worth submitting as a pull request to incorporate into the main code?
cheers,
-Paul
Your approach looks fine, please send a pull request! 😉
Solved in #203.