Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Query] Adds public backend metrics property to Diagnostics #4001

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f440f1b
initial commit
Maya-Painter Jul 21, 2023
b287e27
some pr comments, WIP
Maya-Painter Jul 21, 2023
82b9871
Refactor
Maya-Painter Jul 24, 2023
1770560
more
Maya-Painter Jul 24, 2023
6fa6e2a
Public constructors and modify accumulators
Maya-Painter Jul 26, 2023
451763b
accumulator updates and undo test changes
Maya-Painter Jul 27, 2023
d4f6c07
add test
Maya-Painter Jul 27, 2023
de863d9
PR comments
Maya-Painter Jul 27, 2023
b1a7823
bug fix
Maya-Painter Jul 27, 2023
b96203a
ToString() refactor
Maya-Painter Jul 28, 2023
f84e698
contract updates
Maya-Painter Jul 28, 2023
e2137d2
test updates
Maya-Painter Jul 28, 2023
e3f1876
Merge branch 'master' into users/mayapainter/querymetrics
Maya-Painter Jul 28, 2023
4c5152d
small fixes
Maya-Painter Jul 28, 2023
f18b585
text fix
Maya-Painter Jul 28, 2023
b25f872
Update accumulators
Maya-Painter Jul 31, 2023
7f8aa59
fix
Maya-Painter Aug 1, 2023
0ebb267
Merge branch 'master' into users/mayapainter/querymetrics
Maya-Painter Aug 1, 2023
f9af873
PR comments
Maya-Painter Aug 1, 2023
d320017
small fix
Maya-Painter Aug 1, 2023
edc5aab
Rename BE -> ServerSide
Maya-Painter Aug 2, 2023
b64d837
more renaming
Maya-Painter Aug 2, 2023
8d71294
Update API and tests
Maya-Painter Aug 2, 2023
95f8efb
separate public and internal classes
Maya-Painter Aug 3, 2023
4f9dd80
API update
Maya-Painter Aug 3, 2023
a1a3915
change namespace
Maya-Painter Aug 4, 2023
eccf865
Pr comments
Maya-Painter Aug 4, 2023
120bccf
public constructors and bug fix
Maya-Painter Aug 7, 2023
94f5612
API updates
Maya-Painter Aug 8, 2023
065d323
renaming and test updates
Maya-Painter Aug 11, 2023
99679c0
PR comments
Maya-Painter Aug 11, 2023
41af90a
more PR comments
Maya-Painter Aug 11, 2023
ba80d30
Merge branch 'master' into users/mayapainter/querymetrics
Maya-Painter Aug 11, 2023
b31e382
PR comments, test additions
Maya-Painter Aug 14, 2023
e6825c3
API updates and more tests
Maya-Painter Aug 14, 2023
2bb36c8
tests and pkrangeid update
Maya-Painter Aug 15, 2023
466be56
PR comments
Maya-Painter Aug 15, 2023
6abd122
more PR comments
Maya-Painter Aug 16, 2023
0b4fcba
smol test fix
Maya-Painter Aug 16, 2023
eca1292
PR comments - renaming properties and constructor rehash
Maya-Painter Aug 16, 2023
c273463
Merge branch 'master' into users/mayapainter/querymetrics
Maya-Painter Aug 16, 2023
f19bafc
contract update
Maya-Painter Aug 16, 2023
e08aff7
seal classes and private fields.
Maya-Painter Aug 23, 2023
2f0cf75
update indexHitRatio calc
Maya-Painter Aug 23, 2023
4a0b17f
mocking refactor to abstract classes
Maya-Painter Aug 25, 2023
6ecdd0c
Merge branch 'master' into users/mayapainter/querymetrics
Maya-Painter Aug 25, 2023
e30b8bc
contract updates
Maya-Painter Sep 5, 2023
3d9fc86
Merge branch 'master' into users/mayapainter/querymetrics
Maya-Painter Sep 5, 2023
806a160
PR comments - Update documentation
Maya-Painter Sep 8, 2023
255b10d
Merge branch 'master' into users/mayapainter/querymetrics
Maya-Painter Sep 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Microsoft.Azure.Cosmos/src/Diagnostics/CosmosDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public virtual int GetFailedRequestCount()
throw new NotImplementedException($"{nameof(CosmosDiagnostics)}.{nameof(GetFailedRequestCount)}");
}

/// <summary>
/// This represents the accumulated backend query metrics for the request.
/// </summary>
/// <returns>The string represemation of the query metrics dictionary.</returns>
public virtual BackendAccumulatedMetrics GetQueryMetrics()
{
// Default implementation avoids breaking change for users upgrading.
throw new NotImplementedException($"{nameof(CosmosDiagnostics)}.{nameof(GetQueryMetrics)}");
}

/// <summary>
/// Gets the string field <see cref="CosmosDiagnostics"/> instance in the Azure CosmosDB database service.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Azure.Cosmos.Diagnostics
using System.Linq;
using System.Text;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Query.Core.Metrics;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Cosmos.Tracing.TraceData;
using static Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum;
Expand Down Expand Up @@ -49,11 +50,42 @@ public override TimeSpan GetClientElapsedTime()
return this.Value?.Summary?.RegionsContacted;
}

public override BackendAccumulatedMetrics GetQueryMetrics()
{
QueryMetrics queryMetrics = QueryMetrics.Empty;
this.WalkTraceTreeForQueryMetrics(this.Value, ref queryMetrics);
return new BackendAccumulatedMetrics(queryMetrics);
}

internal bool IsGoneExceptionHit()
{
return this.WalkTraceTreeForGoneException(this.Value);
}

private bool WalkTraceTreeForQueryMetrics(ITrace currentTrace, ref QueryMetrics queryMetrics)
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
{
if (currentTrace == null)
{
return false;
}

foreach (object datums in currentTrace.Data.Values)
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
{
if (datums is QueryMetricsTraceDatum queryMetricsTraceDatum)
{
queryMetrics += queryMetricsTraceDatum.QueryMetrics;
return true;
}
}

foreach (ITrace childTrace in currentTrace.Children)
{
this.WalkTraceTreeForQueryMetrics(childTrace, ref queryMetrics);
}

return false;
}

private bool WalkTraceTreeForGoneException(ITrace currentTrace)
{
if (currentTrace == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using Microsoft.Azure.Cosmos.Query.Core.Metrics;

/// <summary>
/// Exposed metrics received for queries from the backend.
/// </summary>
public sealed class BackendAccumulatedMetrics
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
{
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
internal BackendAccumulatedMetrics(QueryMetrics queryMetrics)
{
this.RetrievedDocumentCount = queryMetrics.BackendMetrics.RetrievedDocumentCount;
this.RetrievedDocumentSize = queryMetrics.BackendMetrics.RetrievedDocumentSize;
this.OutputDocumentCount = queryMetrics.BackendMetrics.OutputDocumentCount;
this.OutputDocumentSize = queryMetrics.BackendMetrics.OutputDocumentSize;
this.IndexHitRatio = queryMetrics.BackendMetrics.IndexHitRatio;
this.TotalTime = queryMetrics.BackendMetrics.TotalTime;
this.QueryCompilationTime = queryMetrics.BackendMetrics.QueryPreparationTimes.QueryCompilationTime;
this.LogicalPlanBuildTime = queryMetrics.BackendMetrics.QueryPreparationTimes.LogicalPlanBuildTime;
this.PhysicalPlanBuildTime = queryMetrics.BackendMetrics.QueryPreparationTimes.PhysicalPlanBuildTime;
this.QueryOptimizationTime = queryMetrics.BackendMetrics.QueryPreparationTimes.QueryOptimizationTime;
this.IndexLookupTime = queryMetrics.BackendMetrics.IndexLookupTime;
this.DocumentLoadTime = queryMetrics.BackendMetrics.DocumentLoadTime;
this.VMExecutionTime = queryMetrics.BackendMetrics.VMExecutionTime;
this.SystemFunctionExecutionTime = queryMetrics.BackendMetrics.RuntimeExecutionTimes.SystemFunctionExecutionTime;
this.UserDefinedFunctionExecutionTime = queryMetrics.BackendMetrics.RuntimeExecutionTimes.UserDefinedFunctionExecutionTime;
this.QueryEngineExecutionTime = queryMetrics.BackendMetrics.RuntimeExecutionTimes.QueryEngineExecutionTime;
this.DocumentWriteTime = queryMetrics.BackendMetrics.DocumentWriteTime;
}

/// <summary>
/// Gets the total query time in the Azure Cosmos database service.
/// </summary>
public TimeSpan TotalTime { get; }

/// <summary>
/// Gets the query compile time in the Azure DocumentDB database service.
/// </summary>
public TimeSpan QueryCompilationTime { get; }

/// <summary>
/// Gets the query logical plan build time in the Azure DocumentDB database service.
/// </summary>
public TimeSpan LogicalPlanBuildTime { get; }

/// <summary>
/// Gets the query physical plan build time in the Azure DocumentDB database service.
/// </summary>
public TimeSpan PhysicalPlanBuildTime { get; }

/// <summary>
/// Gets the query optimization time in the Azure DocumentDB database service.
/// </summary>
public TimeSpan QueryOptimizationTime { get; }

/// <summary>
/// Gets the number of documents retrieved during query in the Azure Cosmos database service.
/// </summary>
public long RetrievedDocumentCount { get; }

/// <summary>
/// Gets the size of documents retrieved in bytes during query in the Azure Cosmos DB service.
/// </summary>
public long RetrievedDocumentSize { get; }

/// <summary>
/// Gets the number of documents returned by query in the Azure Cosmos DB service.
/// </summary>
public long OutputDocumentCount { get; }

/// <summary>
/// Gets the size of documents outputted in bytes during query in the Azure Cosmos database service.
/// </summary>
public long OutputDocumentSize { get; }

/// <summary>
/// Gets the query QueryPreparationTimes in the Azure Cosmos database service.
/// </summary>
public TimeSpan QueryPreparationTimes { get; }

/// <summary>
/// Gets the query index lookup time in the Azure Cosmos database service.
/// </summary>
public TimeSpan IndexLookupTime { get; }

/// <summary>
/// Gets the document loading time during query in the Azure Cosmos database service.
/// </summary>
public TimeSpan DocumentLoadTime { get; }

/// <summary>
/// Gets the query runtime execution times during query in the Azure Cosmos database service.
/// </summary>
public TimeSpan RuntimeExecutionTimes { get; }

/// <summary>
/// Gets the output writing/serializing time during query in the Azure Cosmos database service.
/// </summary>
public TimeSpan DocumentWriteTime { get; }

/// <summary>
/// Gets the index hit ratio by query in the Azure Cosmos database service.
/// </summary>
public double IndexHitRatio { get; }

/// <summary>
/// Gets the VMExecution Time.
/// </summary>
public TimeSpan VMExecutionTime { get; }

/// <summary>
/// Gets the query system function execution time in the Azure Cosmos DB service.
/// </summary>
public TimeSpan SystemFunctionExecutionTime { get; }

/// <summary>
/// Gets the query user defined function execution time in the Azure Cosmos DB service.
/// </summary>
public TimeSpan UserDefinedFunctionExecutionTime { get; }

/// <summary>
/// Gets the total query runtime execution time in the Azure Cosmos DB service.
/// </summary>
public TimeSpan QueryEngineExecutionTime { get; }

/// <summary>
/// String representation of the QueryBackendMetrics.
/// </summary>
/// <returns>Metric text</returns>
public override string ToString()
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
{
return $"totalExecutionTimeInMs={this.TotalTime.TotalMilliseconds};queryCompileTimeInMs={this.QueryCompilationTime.TotalMilliseconds};queryLogicalPlanBuildTimeInMs={this.LogicalPlanBuildTime.TotalMilliseconds};queryPhysicalPlanBuildTimeInMs={this.PhysicalPlanBuildTime.TotalMilliseconds};queryOptimizationTimeInMs={this.QueryOptimizationTime.TotalMilliseconds};indexLookupTimeInMs={this.IndexLookupTime.TotalMilliseconds};documentLoadTimeInMs={this.DocumentLoadTime.TotalMilliseconds};systemFunctionExecuteTimeInMs={this.SystemFunctionExecutionTime.TotalMilliseconds};userFunctionExecuteTimeInMs={this.UserDefinedFunctionExecutionTime.TotalMilliseconds};retrievedDocumentCount={this.RetrievedDocumentCount};retrievedDocumentSize={this.RetrievedDocumentSize};outputDocumentCount={this.OutputDocumentCount};outputDocumentSize={this.OutputDocumentSize};writeOutputTimeInMs={this.DocumentWriteTime.TotalMilliseconds};indexUtilizationRatio={this.IndexHitRatio}";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public Accumulator(TimeSpan queryEngineExecutionTime, TimeSpan systemFunctionExe
this.UserDefinedFunctionExecutionTime = userDefinedFunctionExecutionTimes;
}

public TimeSpan QueryEngineExecutionTime { get; set; }
public TimeSpan SystemFunctionExecutionTime { get; set; }
public TimeSpan UserDefinedFunctionExecutionTime { get; set; }
public TimeSpan QueryEngineExecutionTime { get; }
public TimeSpan SystemFunctionExecutionTime { get; }
public TimeSpan UserDefinedFunctionExecutionTime { get; }

public Accumulator Accumulate(RuntimeExecutionTimes runtimeExecutionTimes)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Microsoft.Azure.Cosmos.Tests.Query.Metrics
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

internal class BackendAccumulatedMetricsTests
{
//TODO
}
}