In node.js, how do I get Request Unit Charges from call to DocumentClient.queryDocuments()?

642 views Asked by At

I'm using the queryDocuments() method from the node.js client for the first time. I've previously used readDocument(), executeStoredProcedure(), replaceStoredProcedure(), etc. and all of those return a header object that allows you to inspect the 'x-ms-request-charge' header to discern the request unit charge for the operation. However, when using the queryDocuments() or readDocuments() methods, it returns an QueryIterator and I don't see a way to inspect it to see the RUs for the operation.

I suspect that the reason for this is that it's not one operation. It's a series of operations and you didn't implement a way to aggregate the total RUs for the entire thing. I'm willing to hit the REST API directly to get this myself, but I wanted to ask if there was a way to get it with the current library before I went through the trouble.

1

There are 1 answers

1
Andrew Liu On BEST ANSWER

You can inspect response headers (e.g. x-ms-request-charge) by inspecting the third input parameter in the callback for queryIterator.executeNext().

For example, here is a code sample that uses the response header (x-ms-retry-after-ms) to implement retry logic on throttles (error 429).

var queryIterator = documentClient.queryDocuments(collection._self, query);
executeNextWithRetry(yourCallback);

function executeNextWithRetry(callback) {
    queryIterator.executeNext(function(err, results, responseHeaders) {
        if(err && err.code === 429 && responseHeaders['x-ms-retry-after-ms']) {

            console.log("Retrying after " + responseHeaders['x-ms-retry-after-ms']);

            setTimeout(function() {
                executeNextWithRetry(callback);
            }, responseHeaders['x-ms-retry-after-ms']);

        } else {
            callback(err, results, responseHeaders);
        }
    });
}