Skip to content

Commit

Permalink
Adding basic request diagnostics for V3 (#615)
Browse files Browse the repository at this point in the history
* inital commit

* making cosmosDiagnostic public

* bumping direct version

* removing formatting of json

* removing extra line

* updating per new changes with clientsiderequestdiagnostics

* using to string on CLientSideRequestDiagnostics

* updating DOtNetSDKAPI

* updating DotNetSDKAPI.json

* moving cosmosDiagnostic as abstract clas

* adding abstract tostring

* updating change log

* removing query metrics from header

* updating contract

* upgrading direct to 3.1.6 to leverage IClientSideRequestDiagnostics

* changing name of cosmosDiagnostics to diagnostics

* moving diagnostics to Diagnostics
  • Loading branch information
simplynaveen20 authored and kirankumarkolli committed Aug 30, 2019
1 parent 1993f5d commit 76e76dd
Show file tree
Hide file tree
Showing 32 changed files with 634 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/ClientRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed class ClientRetryPolicy : IDocumentClientRetryPolicy
private Uri locationEndpoint;
private RetryContext retryContext;

private ClientSideRequestStatistics sharedStatistics;
private IClientSideRequestStatistics sharedStatistics;

public ClientRetryPolicy(
GlobalEndpointManager globalEndpointManager,
Expand All @@ -50,7 +50,7 @@ public ClientRetryPolicy(
this.sessionTokenRetryCount = 0;
this.canUseMultipleWriteLocations = false;

this.sharedStatistics = new ClientSideRequestStatistics();
this.sharedStatistics = new CosmosClientSideRequestStatistics();
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions Microsoft.Azure.Cosmos/src/DocumentFeedResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal DocumentFeedResponse(
INameValueCollection responseHeaders,
bool useETagAsContinuation = false,
IReadOnlyDictionary<string, QueryMetrics> queryMetrics = null,
ClientSideRequestStatistics requestStats = null,
IClientSideRequestStatistics requestStats = null,
string disallowContinuationTokenMessage = null,
long responseLengthBytes = 0)
: this(result)
Expand All @@ -79,7 +79,7 @@ internal DocumentFeedResponse(
IEnumerable<T> result,
int count,
INameValueCollection responseHeaders,
ClientSideRequestStatistics requestStats,
IClientSideRequestStatistics requestStats,
long responseLengthBytes)
: this(result, count, responseHeaders, false, null, requestStats, responseLengthBytes: responseLengthBytes)
{
Expand All @@ -91,7 +91,7 @@ internal DocumentFeedResponse(
/// <remarks>
/// This value is currently used for tracking replica Uris.
/// </remarks>
internal ClientSideRequestStatistics RequestStatistics { get; private set; }
internal IClientSideRequestStatistics RequestStatistics { get; private set; }

/// <summary>
/// Gets the response length in bytes
Expand Down
5 changes: 5 additions & 0 deletions Microsoft.Azure.Cosmos/src/Handler/ResponseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public virtual Stream Content
/// </summary>
public virtual RequestMessage RequestMessage { get; internal set; }

/// <summary>
/// Gets the cosmos diagnostic information for the current request to Azure Cosmos DB service
/// </summary>
public CosmosDiagnostics Diagnostics { get; set; }

/// <summary>
/// Gets the internal error object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ internal CosmosQueryResponseMessageHeaders CloneKnownProperties(
RetryAfterLiteral = this.RetryAfterLiteral,
SubStatusCodeLiteral = this.SubStatusCodeLiteral,
ContentType = this.ContentType,
QueryMetricsText = QueryMetricsText
};
}

Expand Down Expand Up @@ -106,6 +107,7 @@ internal static CosmosQueryResponseMessageHeaders ConvertToQueryHeaders(
RetryAfterLiteral = sourceHeaders.RetryAfterLiteral,
SubStatusCodeLiteral = sourceHeaders.SubStatusCodeLiteral,
ContentType = sourceHeaders.ContentType,
QueryMetricsText = sourceHeaders.QueryMetricsText
};
}
}
Expand Down
3 changes: 3 additions & 0 deletions Microsoft.Azure.Cosmos/src/Headers/Headers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ internal string RetryAfterLiteral
[CosmosKnownHeaderAttribute(HeaderName = HttpConstants.HttpHeaders.PageSize)]
internal string PageSize { get; set; }

[CosmosKnownHeaderAttribute(HeaderName = HttpConstants.HttpHeaders.QueryMetrics)]
internal string QueryMetricsText { get; set; }

/// <summary>
/// Creates a new instance of <see cref="Headers"/>.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Microsoft.Azure.Cosmos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<ClientVersion>3.1.1</ClientVersion>
<DirectVersion>3.1.5</DirectVersion>
<DirectVersion>3.1.6</DirectVersion>
<HybridRowVersion>1.0.0-preview</HybridRowVersion>
<DirectPackageName Condition=" '$(SignAssembly)' == 'true' ">Microsoft.Azure.Cosmos.Direct</DirectPackageName>
<DirectPackageName Condition=" '$(SignAssembly)' != 'true' ">Microsoft.Azure.Cosmos.Direct.MyGet</DirectPackageName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,6 @@ public CosmosQueryResponseMessageHeaders GetResponseHeaders()

this.SetQueryMetrics();

IReadOnlyDictionary<string, QueryMetrics> groupedQueryMetrics = this.GetQueryMetrics();
if (groupedQueryMetrics != null && groupedQueryMetrics.Count != 0)
{
responseHeaders[HttpConstants.HttpHeaders.QueryMetrics] = QueryMetrics
.CreateFromIEnumerable(groupedQueryMetrics.Values)
.ToDelimitedString();
}

responseHeaders.RequestCharge = this.requestChargeTracker.GetAndResetCharge();

return responseHeaders;
Expand Down Expand Up @@ -392,6 +384,7 @@ public override async Task<QueryResponse> DrainAsync(int maxElements, Cancellati
result: results,
count: results.Count,
responseHeaders: this.GetResponseHeaders(),
queryMetrics: this.GetQueryMetrics(),
responseLengthBytes: this.GetAndResetResponseLengthBytes());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ private async Task<ResponseMessage> ExecuteNextHelperAsync(CancellationToken can
}
}

if (response?.queryMetrics != null && response?.queryMetrics.Count > 0)
{
response.Diagnostics = new QueryOperationStatistics(response.queryMetrics);
}

return response;
}

Expand Down Expand Up @@ -307,7 +312,7 @@ private async Task<CosmosQueryExecutionContext> CreateItemQueryExecutionContextA
rewrittenComosQueryContext = this.cosmosQueryContext;
}

return await CreateSpecializedDocumentQueryExecutionContextAsync(
return await this.CreateSpecializedDocumentQueryExecutionContextAsync(
rewrittenComosQueryContext,
partitionedQueryExecutionInfo,
targetRanges,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ public override async Task<QueryResponse> DrainAsync(int maxElements, Cancellati
count: finalResult.Count,
responseLengthBytes: responseLengthBytes,
responseHeaders: new CosmosQueryResponseMessageHeaders(
continauationToken: null,
disallowContinuationTokenMessage: null,
resourceType: resourceType,
continauationToken: null,
disallowContinuationTokenMessage: null,
resourceType: resourceType,
containerRid: containerRid)
{
RequestCharge = requestCharge
});
},
queryMetrics: this.GetQueryMetrics());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public override async Task<QueryResponse> DrainAsync(int maxElements, Cancellati
distinctResults,
distinctResults.Count,
cosmosQueryResponse.ResponseLengthBytes,
cosmosQueryResponse.QueryHeaders.CloneKnownProperties(updatedContinuationToken, disallowContinuationTokenMessage));
cosmosQueryResponse.QueryHeaders.CloneKnownProperties(updatedContinuationToken, disallowContinuationTokenMessage),
cosmosQueryResponse.queryMetrics);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public override async Task<QueryResponse> DrainAsync(int maxElements, Cancellati
result: documentsAfterSkip,
count: documentsAfterSkip.Count(),
responseHeaders: sourcePage.QueryHeaders.CloneKnownProperties(updatedContinuationToken, sourcePage.QueryHeaders.DisallowContinuationTokenMessage),
queryMetrics: sourcePage.queryMetrics,
responseLengthBytes: sourcePage.ResponseLengthBytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public override async Task<QueryResponse> DrainAsync(int maxElements, Cancellati
takedDocuments,
takedDocuments.Count,
results.ResponseLengthBytes,
results.QueryHeaders.CloneKnownProperties(updatedContinuationToken, results.QueryHeaders.DisallowContinuationTokenMessage));
results.QueryHeaders.CloneKnownProperties(updatedContinuationToken, results.QueryHeaders.DisallowContinuationTokenMessage),
results.queryMetrics);
}

private enum TakeEnum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ public override async Task<QueryResponse> ExecuteNextAsync(CancellationToken tok
dynamics,
queryResponse.Count,
queryResponse.ResponseLengthBytes,
queryResponse.QueryHeaders.CloneKnownProperties());
queryResponse.QueryHeaders.CloneKnownProperties(),
queryMetrics: queryResponse.queryMetrics);
}
catch (Exception)
{
Expand Down
18 changes: 9 additions & 9 deletions Microsoft.Azure.Cosmos/src/Query/QueryMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public long OutputDocumentCount
/// <summary>
/// Gets the size of documents outputted in bytes during query in the Azure DocumentDB database service.
/// </summary>
internal long OutputDocumentSize
public long OutputDocumentSize
{
get
{
Expand All @@ -168,7 +168,7 @@ internal long OutputDocumentSize
/// <summary>
/// Gets the total query time in the Azure DocumentDB database service.
/// </summary>
internal TimeSpan TotalQueryExecutionTime
public TimeSpan TotalQueryExecutionTime
{
get
{
Expand Down Expand Up @@ -212,7 +212,7 @@ public long Retries
/// <summary>
/// Gets the query index lookup time in the Azure DocumentDB database service.
/// </summary>
internal TimeSpan IndexLookupTime
public TimeSpan IndexLookupTime
{
get
{
Expand All @@ -223,7 +223,7 @@ internal TimeSpan IndexLookupTime
/// <summary>
/// Gets the document loading time during query in the Azure DocumentDB database service.
/// </summary>
internal TimeSpan DocumentLoadTime
public TimeSpan DocumentLoadTime
{
get
{
Expand All @@ -234,7 +234,7 @@ internal TimeSpan DocumentLoadTime
/// <summary>
/// Gets the query runtime execution times during query in the Azure DocumentDB database service.
/// </summary>
internal RuntimeExecutionTimes RuntimeExecutionTimes
public RuntimeExecutionTimes RuntimeExecutionTimes
{
get
{
Expand All @@ -245,7 +245,7 @@ internal RuntimeExecutionTimes RuntimeExecutionTimes
/// <summary>
/// Gets the output writing/serializing time during query in the Azure DocumentDB database service.
/// </summary>
internal TimeSpan DocumentWriteTime
public TimeSpan DocumentWriteTime
{
get
{
Expand Down Expand Up @@ -281,7 +281,7 @@ public double IndexHitRatio
/// <summary>
/// Gets the Index Hit Document Count.
/// </summary>
internal long IndexHitDocumentCount
public long IndexHitDocumentCount
{
get
{
Expand All @@ -292,7 +292,7 @@ internal long IndexHitDocumentCount
/// <summary>
/// Gets the VMExecution Time.
/// </summary>
internal TimeSpan VMExecutionTime
public TimeSpan VMExecutionTime
{
get
{
Expand Down Expand Up @@ -560,7 +560,7 @@ public TimeSpan WriteOutputTime
/// <summary>
/// Gets the query runtime execution times during query in the Azure DocumentDB database service.
/// </summary>
internal RuntimeExecutionTimes RuntimeExecutionTimes
public RuntimeExecutionTimes RuntimeExecutionTimes
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ internal override void PopulateRequestOptions(RequestMessage request)
request.Headers.Add(HttpConstants.HttpHeaders.ContentSerializationFormat, this.CosmosSerializationOptions.ContentSerializationFormat);
}

request.Headers.Add(HttpConstants.HttpHeaders.PopulateQueryMetrics, bool.TrueString);

base.PopulateRequestOptions(request);
}

Expand All @@ -220,7 +222,7 @@ internal QueryRequestOptions Clone()
EnableCrossPartitionSkipTake = this.EnableCrossPartitionSkipTake,
EnableGroupBy = this.EnableGroupBy,
Properties = this.Properties,
IsEffectivePartitionKeyRouting = this.IsEffectivePartitionKeyRouting
IsEffectivePartitionKeyRouting = this.IsEffectivePartitionKeyRouting,
};

return queryRequestOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ protected ItemResponse()
internal ItemResponse(
HttpStatusCode httpStatusCode,
Headers headers,
T item)
T item,
CosmosDiagnostics diagnostics)
{
this.StatusCode = httpStatusCode;
this.Headers = headers;
this.Resource = item;
this.Diagnostics = diagnostics;
}

/// <inheritdoc/>
Expand Down
5 changes: 3 additions & 2 deletions Microsoft.Azure.Cosmos/src/Resource/CosmosResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal FeedResponse<T> CreateQueryFeedResponse<T>(
if (queryResponse != null)
{
return QueryResponse<T>.CreateResponse<T>(
responseMessage: queryResponse,
cosmosQueryResponse: queryResponse,
jsonSerializer: this.cosmosSerializer);
}

Expand All @@ -56,7 +56,8 @@ internal Task<ItemResponse<T>> CreateItemResponseAsync<T>(
return new ItemResponse<T>(
cosmosResponseMessage.StatusCode,
cosmosResponseMessage.Headers,
item);
item,
cosmosResponseMessage.Diagnostics);
});
}

Expand Down
Loading

0 comments on commit 76e76dd

Please sign in to comment.