Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RU Charge for Queries in DocumentDB

With the .NET library how do I determine the RU charge for a query. It returns IQueryable and I'm not sure how to log that. Bonus points for how to log all request's RU.

Simple code but doesn't return RU's:

 var docs = DocumentDBRepository<CampaignMessage>.client.
         CreateDocumentQuery<CampaignMessage>(UriFactoryExtensions.CreateCollectionUri(), new FeedOptions() { MaxItemCount = -1, MaxDegreeOfParallelism = 5 }).Where(x => x.BlastKey == "abc-796").
like image 361
lucuma Avatar asked Apr 28 '17 16:04

lucuma


1 Answers

With the .NET library how do I determine the RU charge for a query. It returns IQueryable and I'm not sure how to log that.

As the link provided by Mikhail, you need to invoke docs.AsDocumentQuery().ExecuteNextAsync to retrieve the result from DocumentDB service, you could get the RU charge for a query from FeedResponse<T>.RequestCharge.

For log all request's RU, I have checked the client logs when using .NET client SDK for logging operation logs, but only the error operation has the log for response headers. I assumed that you need to write code to log the RU charge for each requests, here is the code snippet, you could refer to it:

public static class DocumentDBExtension
{
    public static async Task<IEnumerable<TSource>> QueryWithRuLog<TSource>(this IQueryable<TSource> source)
    {
        List<TSource> items = new List<TSource>();
        double totalRuCost = 0;
        var query = source.AsDocumentQuery();
        while (query.HasMoreResults)
        {
            var result =await query.ExecuteNextAsync<TSource>();
            items.AddRange(result);
            //log RU
            totalRuCost += result.RequestCharge;
        }
        //log totalRuCost
        Console.WriteLine($"RUs cost:{totalRuCost}");
        return items;
    }
}

//Usage
var result=docs.QueryWithRuLog<CampaignMessage>();
like image 99
Bruce Chen Avatar answered Nov 21 '22 16:11

Bruce Chen