Skip to content

Commit

Permalink
[Query] Adds public backend metrics property to Diagnostics (#4001)
Browse files Browse the repository at this point in the history
* initial commit

* some pr comments, WIP

* Refactor

* more

* Public constructors and modify accumulators

* accumulator updates and undo test changes

* add test

* PR comments

* bug fix

* ToString() refactor

* contract updates

* test updates

* small fixes

* text fix

* Update accumulators

* fix

* PR comments

* small fix

* Rename BE -> ServerSide

* more renaming

* Update API and tests

* separate public and internal classes

* API update

* change namespace

* Pr comments

* public constructors and bug fix

* API updates

* renaming and test updates

* PR comments

* more PR comments

* PR comments, test additions

* API updates and more tests

* tests and pkrangeid update

* PR comments

* more PR comments

* smol test fix

* PR comments - renaming properties and constructor rehash

* contract update

* seal classes and private fields.

* update indexHitRatio calc

* mocking refactor to abstract classes

* contract updates

* PR comments - Update documentation
  • Loading branch information
Maya-Painter authored Sep 8, 2023
1 parent 7c3f5e1 commit b7b1064
Show file tree
Hide file tree
Showing 38 changed files with 1,420 additions and 742 deletions.
13 changes: 13 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,19 @@ public virtual int GetFailedRequestCount()
throw new NotImplementedException($"{nameof(CosmosDiagnostics)}.{nameof(GetFailedRequestCount)}");
}

/// <summary>
/// This represents the backend query metrics for the request.
/// </summary>
/// <remarks>
/// This is only applicable for query operations. For all other operations this will return null.
/// </remarks>
/// <returns>The accumulated backend metrics for the request.</returns>
public virtual ServerSideCumulativeMetrics 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 Cosmos DB database service.
/// </summary>
Expand Down
24 changes: 22 additions & 2 deletions Microsoft.Azure.Cosmos/src/Diagnostics/CosmosTraceDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ 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;

internal sealed class CosmosTraceDiagnostics : CosmosDiagnostics
{
private readonly Lazy<ServerSideCumulativeMetrics> accumulatedMetrics;

public CosmosTraceDiagnostics(ITrace trace)
{
if (trace == null)
Expand All @@ -30,6 +33,7 @@ public CosmosTraceDiagnostics(ITrace trace)
}

this.Value = rootTrace;
this.accumulatedMetrics = new Lazy<ServerSideCumulativeMetrics>(() => PopulateServerSideCumulativeMetrics(this.Value));
}

public ITrace Value { get; }
Expand All @@ -49,6 +53,11 @@ public override TimeSpan GetClientElapsedTime()
return this.Value?.Summary?.RegionsContacted;
}

public override ServerSideCumulativeMetrics GetQueryMetrics()
{
return this.accumulatedMetrics.Value;
}

internal bool IsGoneExceptionHit()
{
return this.WalkTraceTreeForGoneException(this.Value);
Expand All @@ -61,9 +70,9 @@ private bool WalkTraceTreeForGoneException(ITrace currentTrace)
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 Expand Up @@ -99,6 +108,17 @@ private ReadOnlyMemory<byte> WriteTraceToJsonWriter(JsonSerializationFormat json
return jsonTextWriter.GetResult();
}

private static ServerSideCumulativeMetrics PopulateServerSideCumulativeMetrics(ITrace trace)
{
ServerSideMetricsInternalAccumulator accumulator = new ServerSideMetricsInternalAccumulator();
ServerSideMetricsInternalAccumulator.WalkTraceTreeForQueryMetrics(trace, accumulator);

IReadOnlyList<ServerSidePartitionedMetricsInternal> serverSideMetricsList = accumulator.GetPartitionedServerSideMetrics().Select(metrics => new ServerSidePartitionedMetricsInternal(metrics)).ToList();

ServerSideCumulativeMetrics accumulatedMetrics = new ServerSideCumulativeMetricsInternal(serverSideMetricsList);
return accumulatedMetrics.PartitionedMetrics.Count != 0 ? accumulatedMetrics : null;
}

public override DateTime? GetStartTimeUtc()
{
if (this.Value == null || this.Value.StartTime == null)
Expand Down
237 changes: 0 additions & 237 deletions Microsoft.Azure.Cosmos/src/Query/Core/Metrics/BackendMetrics.cs

This file was deleted.

38 changes: 0 additions & 38 deletions Microsoft.Azure.Cosmos/src/Query/Core/Metrics/ClientSideMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Microsoft.Azure.Cosmos.Query.Core.Metrics
{
using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Stores client side QueryMetrics.
Expand Down Expand Up @@ -54,42 +53,5 @@ public ClientSideMetrics(
/// Gets the Fetch Execution Ranges for this continuation of the query.
/// </summary>
public IEnumerable<FetchExecutionRange> FetchExecutionRanges { get; }

public ref struct Accumulator
{
public Accumulator(long retries, double requestCharge, IEnumerable<FetchExecutionRange> fetchExecutionRanges)
{
this.Retries = retries;
this.RequestCharge = requestCharge;
this.FetchExecutionRanges = fetchExecutionRanges;
}

public long Retries { get; }

public double RequestCharge { get; }

public IEnumerable<FetchExecutionRange> FetchExecutionRanges { get; }

public Accumulator Accumulate(ClientSideMetrics clientSideMetrics)
{
if (clientSideMetrics == null)
{
throw new ArgumentNullException(nameof(clientSideMetrics));
}

return new Accumulator(
retries: this.Retries + clientSideMetrics.Retries,
requestCharge: this.RequestCharge + clientSideMetrics.RequestCharge,
fetchExecutionRanges: (this.FetchExecutionRanges ?? Enumerable.Empty<FetchExecutionRange>()).Concat(clientSideMetrics.FetchExecutionRanges));
}

public static ClientSideMetrics ToClientSideMetrics(Accumulator accumulator)
{
return new ClientSideMetrics(
retries: accumulator.Retries,
requestCharge: accumulator.RequestCharge,
fetchExecutionRanges: accumulator.FetchExecutionRanges);
}
}
}
}
Loading

0 comments on commit b7b1064

Please sign in to comment.