Table of Contents

Class CosmosDiagnostics

Namespace
Microsoft.Azure.Cosmos
Assembly
Microsoft.Azure.Cosmos.Client.dll

Contains the cosmos diagnostic information for the current request to Azure Cosmos DB service.

public abstract class CosmosDiagnostics
Inheritance
CosmosDiagnostics
Inherited Members
Extension Methods

Constructors

CosmosDiagnostics()

protected CosmosDiagnostics()

Methods

GetClientElapsedTime()

This represent the end to end elapsed time of the request. If the request is still in progress it will return the current elapsed time since the start of the request.

public virtual TimeSpan GetClientElapsedTime()

Returns

TimeSpan

The clients end to end elapsed time of the request.

GetContactedRegions()

Gets the list of all regions that were contacted for a request

public abstract IReadOnlyList<(string regionName, Uri uri)> GetContactedRegions()

Returns

IReadOnlyList<(string regionName, Uri uri)>

The list of tuples containing the Region name and the URI

Remarks

The returned list contains unique regions and doesn't guarantee ordering of the regions contacted from the first to the last

GetFailedRequestCount()

This represents the count of failed requests.

public virtual int GetFailedRequestCount()

Returns

int

The count of failed requests with cosmos service.

GetQueryMetrics()

This represents the backend query metrics for the request.

public virtual ServerSideCumulativeMetrics GetQueryMetrics()

Returns

ServerSideCumulativeMetrics

The accumulated backend metrics for the request.

Remarks

This is only applicable for query operations. For all other operations this will return null.

GetStartTimeUtc()

This represents the start time of the request.

public virtual DateTime? GetStartTimeUtc()

Returns

DateTime?

This returns the start time of the request.

ToString()

Gets the string field CosmosDiagnostics instance in the Azure Cosmos DB database service.

public override abstract string ToString()

Returns

string

The string field CosmosDiagnostics instance in the Azure Cosmos DB database service.

Examples

Do not eagerly materialize the diagnostics until the moment of consumption to avoid unnecessary allocations, let the ToString be called only when needed. You can capture diagnostics conditionally, based on latency or errors:

try
{
    ItemResponse<Book> response = await container.CreateItemAsync<Book>(item: testItem);
    if (response.Diagnostics.GetClientElapsedTime() > ConfigurableSlowRequestTimeSpan)
    {
        // Log the diagnostics and add any additional info necessary to correlate to other logs 
        logger.LogInformation("Operation took longer than expected, Diagnostics: {Diagnostics}");
    }
}
catch (CosmosException cosmosException)
{
    // Log the full exception including the stack trace 
    logger.LogError(cosmosException);
    // The Diagnostics can be logged separately if required.
    logger.LogError("Cosmos DB call failed with {StatusCode}, {SubStatusCode}, Diagnostics: {Diagnostics}", cosmosException.StatusCode, cosmosException.SubStatusCode, cosmosException.Diagnostics);
}

Remarks

CosmosDiagnostics implements lazy materialization and is only materialized when ToString() is called.