1

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.

OmG
  • 18,337
  • 10
  • 57
  • 90
Larry Maccherone
  • 9,393
  • 3
  • 27
  • 43

1 Answers1

2

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);
        }
    });
}
Andrew Liu
  • 8,045
  • 38
  • 47
  • Thanks. I submitted a pull request to update the docs for executeNext(). It currently says, "takes two parameters error, resourcesList". I noticed that my last doc fix pull request never got merged so I added it to this latest. – Larry Maccherone Jul 11 '15 at 16:01