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 4 commits
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
11 changes: 11 additions & 0 deletions Microsoft.Azure.Cosmos/src/Diagnostics/CosmosDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using Microsoft.Azure.Cosmos.Query.Core.Metrics;

/// <summary>
/// Contains the cosmos diagnostic information for the current request to Azure Cosmos DB service.
Expand Down Expand Up @@ -43,6 +44,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 accumulated backend metrics for the request.</returns>
public virtual BackendMetrics 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 @@ -6,9 +6,9 @@ namespace Microsoft.Azure.Cosmos.Diagnostics
{
using System;
using System.Collections.Generic;
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,21 +49,52 @@ public override TimeSpan GetClientElapsedTime()
return this.Value?.Summary?.RegionsContacted;
}

public override BackendMetrics GetQueryMetrics()
{
BackendMetrics.Accumulator accumulator = this.WalkTraceTreeForQueryMetrics(this.Value, new BackendMetrics.Accumulator());
return BackendMetrics.Accumulator.ToBackendMetrics(accumulator);

}

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

private BackendMetrics.Accumulator WalkTraceTreeForQueryMetrics(ITrace currentTrace, BackendMetrics.Accumulator accumulator)
{
if (currentTrace == null)
{
return accumulator;
}

foreach (object datum in currentTrace.Data.Values)
{
if (datum is QueryMetricsTraceDatum queryMetricsTraceDatum)
{
accumulator = accumulator.Accumulate(queryMetricsTraceDatum.QueryMetrics.BackendMetrics);
return accumulator;
}
}

foreach (ITrace childTrace in currentTrace.Children)
{
accumulator = this.WalkTraceTreeForQueryMetrics(childTrace, accumulator);
}

return accumulator;
}

private bool WalkTraceTreeForGoneException(ITrace currentTrace)
{
if (currentTrace == null)
{
return false;
}

foreach (object datums in currentTrace.Data.Values)
foreach (object datum in currentTrace.Data.Values)
{
if (datums is ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum)
if (datum is ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum)
{
foreach (StoreResponseStatistics responseStatistics in clientSideRequestStatisticsTraceDatum.StoreResponseStatisticsList)
{
Expand Down
27 changes: 12 additions & 15 deletions Microsoft.Azure.Cosmos/src/Query/Core/Metrics/BackendMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ namespace Microsoft.Azure.Cosmos.Query.Core.Metrics
/// <summary>
/// Metrics received for queries from the backend.
/// </summary>
#if INTERNAL
#pragma warning disable SA1600
#pragma warning disable CS1591
public
#else
internal
#endif
sealed class BackendMetrics
public sealed class BackendMetrics
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// QueryMetrics that with all members having default (but not null) members.
/// </summary>
public static readonly BackendMetrics Empty = new BackendMetrics(
internal static readonly BackendMetrics Empty = new BackendMetrics(
retrievedDocumentCount: default,
retrievedDocumentSize: default,
outputDocumentCount: default,
Expand All @@ -36,7 +29,7 @@ sealed class BackendMetrics
runtimeExecutionTimes: RuntimeExecutionTimes.Empty,
documentWriteTime: default);

public BackendMetrics(
internal BackendMetrics(
long retrievedDocumentCount,
long retrievedDocumentSize,
long outputDocumentCount,
Expand Down Expand Up @@ -124,12 +117,16 @@ public BackendMetrics(
/// </summary>
public TimeSpan VMExecutionTime { get; }

public override string ToString()
/// <summary>
/// String representation of BackendMetric.
/// </summary>
/// <returns>BackendMetric text</returns>
public override string ToString() //todo move this logic elsewhere?
{
return $"totalExecutionTimeInMs={this.TotalTime.TotalMilliseconds};queryCompileTimeInMs={this.QueryPreparationTimes.QueryCompilationTime.TotalMilliseconds};queryLogicalPlanBuildTimeInMs={this.QueryPreparationTimes.LogicalPlanBuildTime.TotalMilliseconds};queryPhysicalPlanBuildTimeInMs={this.QueryPreparationTimes.PhysicalPlanBuildTime.TotalMilliseconds};queryOptimizationTimeInMs={this.QueryPreparationTimes.QueryOptimizationTime.TotalMilliseconds};indexLookupTimeInMs={this.IndexLookupTime.TotalMilliseconds};documentLoadTimeInMs={this.DocumentLoadTime.TotalMilliseconds};systemFunctionExecuteTimeInMs={this.RuntimeExecutionTimes.SystemFunctionExecutionTime.TotalMilliseconds};userFunctionExecuteTimeInMs={this.RuntimeExecutionTimes.UserDefinedFunctionExecutionTime.TotalMilliseconds};retrievedDocumentCount={this.RetrievedDocumentCount};retrievedDocumentSize={this.RetrievedDocumentSize};outputDocumentCount={this.OutputDocumentCount};outputDocumentSize={this.OutputDocumentSize};writeOutputTimeInMs={this.DocumentWriteTime.TotalMilliseconds};indexUtilizationRatio={this.IndexHitRatio}";
}

public static BackendMetrics CreateFromIEnumerable(IEnumerable<BackendMetrics> backendMetricsEnumerable)
internal static BackendMetrics CreateFromIEnumerable(IEnumerable<BackendMetrics> backendMetricsEnumerable)
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
{
BackendMetrics.Accumulator accumulator = default;
foreach (BackendMetrics backendMetrics in backendMetricsEnumerable)
Expand All @@ -140,12 +137,12 @@ public static BackendMetrics CreateFromIEnumerable(IEnumerable<BackendMetrics> b
return BackendMetrics.Accumulator.ToBackendMetrics(accumulator);
}

public static bool TryParseFromDelimitedString(string delimitedString, out BackendMetrics backendMetrics)
internal static bool TryParseFromDelimitedString(string delimitedString, out BackendMetrics backendMetrics)
{
return BackendMetricsParser.TryParse(delimitedString, out backendMetrics);
}

public static BackendMetrics ParseFromDelimitedString(string delimitedString)
internal static BackendMetrics ParseFromDelimitedString(string delimitedString)
{
if (!BackendMetricsParser.TryParse(delimitedString, out BackendMetrics backendMetrics))
{
Expand All @@ -155,7 +152,7 @@ public static BackendMetrics ParseFromDelimitedString(string delimitedString)
return backendMetrics;
}

public ref struct Accumulator
internal ref struct Accumulator
Maya-Painter marked this conversation as resolved.
Show resolved Hide resolved
{
public Accumulator(
TimeSpan totalTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@ namespace Microsoft.Azure.Cosmos.Query.Core.Metrics
/// <summary>
/// Query preparation metrics in the Azure DocumentDB database service.
/// </summary>
#if INTERNAL
#pragma warning disable SA1600
#pragma warning disable CS1591
public
#else
internal
#endif
sealed class QueryPreparationTimes
public sealed class QueryPreparationTimes
{
public static readonly QueryPreparationTimes Zero = new QueryPreparationTimes(
internal static readonly QueryPreparationTimes Zero = new QueryPreparationTimes(
queryCompilationTime: default,
logicalPlanBuildTime: default,
physicalPlanBuildTime: default,
Expand All @@ -30,7 +23,7 @@ sealed class QueryPreparationTimes
/// <param name="logicalPlanBuildTime">Query logical plan build time</param>
/// <param name="physicalPlanBuildTime">Query physical plan build time</param>
/// <param name="queryOptimizationTime">Query optimization time</param>
public QueryPreparationTimes(
internal QueryPreparationTimes(
TimeSpan queryCompilationTime,
TimeSpan logicalPlanBuildTime,
TimeSpan physicalPlanBuildTime,
Expand Down Expand Up @@ -62,7 +55,7 @@ public QueryPreparationTimes(
/// </summary>
public TimeSpan QueryOptimizationTime { get; }

public ref struct Accumulator
internal ref struct Accumulator
{
public Accumulator(TimeSpan queryCompliationTime, TimeSpan logicalPlanBuildTime, TimeSpan physicalPlanBuildTime, TimeSpan queryOptimizationTime)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@ namespace Microsoft.Azure.Cosmos.Query.Core.Metrics
/// <summary>
/// Query runtime execution times in the Azure Cosmos DB service.
/// </summary>
#if INTERNAL
#pragma warning disable SA1600
#pragma warning disable CS1591
public
#else
internal
#endif
sealed class RuntimeExecutionTimes
public sealed class RuntimeExecutionTimes
{
public static readonly RuntimeExecutionTimes Empty = new RuntimeExecutionTimes(
internal static readonly RuntimeExecutionTimes Empty = new RuntimeExecutionTimes(
queryEngineExecutionTime: default,
systemFunctionExecutionTime: default,
userDefinedFunctionExecutionTime: default);
Expand All @@ -28,7 +21,7 @@ sealed class RuntimeExecutionTimes
/// <param name="queryEngineExecutionTime">Query end - to - end execution time</param>
/// <param name="systemFunctionExecutionTime">Total time spent executing system functions</param>
/// <param name="userDefinedFunctionExecutionTime">Total time spent executing user - defined functions</param>
public RuntimeExecutionTimes(
internal RuntimeExecutionTimes(
TimeSpan queryEngineExecutionTime,
TimeSpan systemFunctionExecutionTime,
TimeSpan userDefinedFunctionExecutionTime)
Expand All @@ -53,7 +46,7 @@ public RuntimeExecutionTimes(
/// </summary>
public TimeSpan UserDefinedFunctionExecutionTime { get; }

public ref struct Accumulator
internal ref struct Accumulator
{
public Accumulator(TimeSpan queryEngineExecutionTime, TimeSpan systemFunctionExecutionTime, TimeSpan userDefinedFunctionExecutionTimes)
{
Expand All @@ -62,9 +55,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
Loading