RU Charge for Queries in DocumentDB

azure-cosmosdb

Solution

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>();

Problem

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"). ```

Original source

Related problems