Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Maya-Painter committed Jul 21, 2023
1 parent 5ec7b4b commit f440f1b
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 3 deletions.
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
32 changes: 32 additions & 0 deletions Microsoft.Azure.Cosmos/src/Diagnostics/CosmosTraceDiagnostics.cs
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)
{
if (currentTrace == null)
{
return false;
}

foreach (object datums in currentTrace.Data.Values)
{
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
{
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()
{
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
}
}

0 comments on commit f440f1b

Please sign in to comment.