Skip to content

Commit

Permalink
Merge to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Willey committed Mar 16, 2020
2 parents 1bfd02f + 4bcc82d commit 56d98c5
Show file tree
Hide file tree
Showing 50 changed files with 917 additions and 482 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ internal virtual async Task ValidateOperationAsync(
throw new InvalidOperationException(ClientResources.UnsupportedBulkRequestOptions);
}

Debug.Assert(itemRequestOptions.DiagnosticContext == null, "Disable Diagnostics is not supported on Bulk operations");
if (itemRequestOptions.DiagnosticContext != null)
{
throw new ArgumentException("DiagnosticContext is not allowed when AllowBulkExecution is set to true");
}

Debug.Assert(BatchAsyncContainerExecutor.ValidateOperationEPK(operation, itemRequestOptions));
}

Expand Down Expand Up @@ -242,7 +246,7 @@ private async Task<PartitionKeyRangeBatchExecutionResult> ExecuteAsync(
partitionKey: null,
streamPayload: serverRequestPayload,
requestEnricher: requestMessage => BatchAsyncContainerExecutor.AddHeadersToRequestMessage(requestMessage, serverRequest.PartitionKeyRangeId),
diagnosticsScope: diagnosticsContext,
diagnosticsContext: diagnosticsContext,
cancellationToken: cancellationToken).ConfigureAwait(false);

using (diagnosticsContext.CreateScope("BatchAsyncContainerExecutor.ToResponse"))
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Batch/BatchExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public BatchExecutor(

public async Task<TransactionalBatchResponse> ExecuteAsync(CancellationToken cancellationToken)
{
using (this.diagnosticsContext.CreateOverallScope("BatchExecuteAsync"))
using (this.diagnosticsContext.GetOverallScope())
{
BatchExecUtils.EnsureValid(this.inputOperations, this.batchOptions);

Expand Down Expand Up @@ -95,7 +95,7 @@ private async Task<TransactionalBatchResponse> ExecuteServerRequestAsync(
requestMessage.Headers.Add(HttpConstants.HttpHeaders.IsBatchAtomic, bool.TrueString);
requestMessage.Headers.Add(HttpConstants.HttpHeaders.IsBatchOrdered, bool.TrueString);
},
diagnosticsScope: this.diagnosticsContext,
diagnosticsContext: this.diagnosticsContext,
cancellationToken);

using (this.diagnosticsContext.CreateScope("TransactionalBatchResponse"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private TransactionalBatchResponse(
this.RequestCharge = requestCharge;
this.RetryAfter = retryAfter;
this.ActivityId = activityId;
this.Diagnostics = diagnosticsContext;
this.Diagnostics = diagnosticsContext.Diagnostics;
this.DiagnosticsContext = diagnosticsContext ?? throw new ArgumentNullException(nameof(diagnosticsContext));
}

Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/CosmosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ private Task<ResponseMessage> CreateDatabaseStreamInternalAsync(
partitionKey: null,
streamPayload: streamPayload,
requestEnricher: (httpRequestMessage) => httpRequestMessage.AddThroughputHeader(throughput),
diagnosticsScope: null,
diagnosticsContext: null,
cancellationToken: cancellationToken);
}

Expand Down
16 changes: 11 additions & 5 deletions Microsoft.Azure.Cosmos/src/Diagnostics/CosmosDiagnosticScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ namespace Microsoft.Azure.Cosmos.Diagnostics
internal sealed class CosmosDiagnosticScope : CosmosDiagnosticsInternal, IDisposable
{
private readonly Stopwatch ElapsedTimeStopWatch;
private readonly Action<TimeSpan> ElapsedTimeCallback;
private bool isDisposed = false;

public CosmosDiagnosticScope(
string name,
Action<TimeSpan> elapsedTimeCallback = null)
string name)
{
this.Id = name;
this.ElapsedTimeStopWatch = Stopwatch.StartNew();
this.ElapsedTimeCallback = elapsedTimeCallback;
}

public string Id { get; }
Expand All @@ -40,6 +37,16 @@ public bool TryGetElapsedTime(out TimeSpan elapsedTime)
return true;
}

internal TimeSpan GetElapsedTime()
{
return this.ElapsedTimeStopWatch.Elapsed;
}

internal bool IsComplete()
{
return this.ElapsedTimeStopWatch.IsRunning;
}

public void Dispose()
{
if (this.isDisposed)
Expand All @@ -48,7 +55,6 @@ public void Dispose()
}

this.ElapsedTimeStopWatch.Stop();
this.ElapsedTimeCallback?.Invoke(this.ElapsedTimeStopWatch.Elapsed);
this.isDisposed = true;
}

Expand Down
15 changes: 14 additions & 1 deletion Microsoft.Azure.Cosmos/src/Diagnostics/CosmosDiagnostics.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
using System;

/// <summary>
/// Contains the cosmos diagnostic information for the current request to Azure Cosmos DB service.
/// </summary>
public abstract class CosmosDiagnostics
{
/// <summary>
/// This represent the end to end elapsed time of the request.
/// If the request is still in progress it will return the current
/// elapsed time since the start of the request.
/// </summary>
/// <returns>The clients end to end elapsed time of the request.</returns>
public virtual TimeSpan GetClientElapsedTime()
{
// Default implementation avoids breaking change for users upgrading.
throw new NotImplementedException($"CosmosDiagnostics.GetElapsedTime");
}

/// <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 @@ -7,8 +7,6 @@ namespace Microsoft.Azure.Cosmos
using System.Collections;
using System.Collections.Generic;
using Microsoft.Azure.Cosmos.Diagnostics;
using Microsoft.Azure.Documents;
using static Microsoft.Azure.Cosmos.CosmosClientSideRequestStatistics;

/// <summary>
/// This represents the diagnostics interface used in the SDK.
Expand All @@ -21,14 +19,18 @@ internal abstract class CosmosDiagnosticsContext : CosmosDiagnosticsInternal, IE

public abstract int FailedRequestCount { get; protected set; }

public abstract TimeSpan? TotalElapsedTime { get; protected set; }

public abstract string UserAgent { get; protected set; }

internal abstract CosmosDiagnosticScope CreateOverallScope(string name);
internal abstract CosmosDiagnostics Diagnostics { get; }

internal abstract CosmosDiagnosticScope GetOverallScope();

internal abstract CosmosDiagnosticScope CreateScope(string name);

internal abstract TimeSpan GetClientElapsedTime();

internal abstract bool IsComplete();

internal abstract void AddDiagnosticsInternal(PointOperationStatistics pointOperationStatistics);

internal abstract void AddDiagnosticsInternal(QueryPageDiagnostics queryPageDiagnostics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Microsoft.Azure.Cosmos.Diagnostics;

/// <summary>
Expand All @@ -22,9 +25,9 @@ internal sealed class CosmosDiagnosticsContextCore : CosmosDiagnosticsContext

private static readonly string DefaultUserAgentString;

private readonly bool IsDefaultUserAgent = true;
private readonly CosmosDiagnosticScope overallScope;

private bool isOverallScopeSet = false;
private bool IsDefaultUserAgent = true;

static CosmosDiagnosticsContextCore()
{
Expand All @@ -37,6 +40,8 @@ public CosmosDiagnosticsContextCore()
{
this.StartUtc = DateTime.UtcNow;
this.ContextList = new List<CosmosDiagnosticsInternal>();
this.Diagnostics = new CosmosDiagnosticsCore(this);
this.overallScope = new CosmosDiagnosticScope("Overall");
}

public override DateTime StartUtc { get; }
Expand All @@ -45,26 +50,23 @@ public CosmosDiagnosticsContextCore()

public override int FailedRequestCount { get; protected set; }

public override TimeSpan? TotalElapsedTime { get; protected set; }

public override string UserAgent { get; protected set; } = CosmosDiagnosticsContextCore.DefaultUserAgentString;

internal override CosmosDiagnosticScope CreateOverallScope(string name)
internal override CosmosDiagnostics Diagnostics { get; }

internal override TimeSpan GetClientElapsedTime()
{
CosmosDiagnosticScope scope;
// If overall is already set then let the original set the elapsed time.
if (this.isOverallScopeSet)
{
scope = new CosmosDiagnosticScope(name);
}
else
{
scope = new CosmosDiagnosticScope(name, this.SetElapsedTime);
this.isOverallScopeSet = true;
}
return this.overallScope.GetElapsedTime();
}

this.ContextList.Add(scope);
return scope;
internal override bool IsComplete()
{
return this.overallScope.IsComplete();
}

internal override CosmosDiagnosticScope GetOverallScope()
{
return this.overallScope;
}

internal override CosmosDiagnosticScope CreateScope(string name)
Expand Down Expand Up @@ -131,6 +133,7 @@ internal override void AddDiagnosticsInternal(CosmosDiagnosticsContext newContex

internal override void SetSdkUserAgent(string userAgent)
{
this.IsDefaultUserAgent = false;
this.UserAgent = userAgent;
}

Expand Down Expand Up @@ -158,11 +161,6 @@ private void AddRequestCount(int statusCode)
}
}

private void SetElapsedTime(TimeSpan totalElapsedTime)
{
this.TotalElapsedTime = totalElapsedTime;
}

private void AddSummaryInfo(CosmosDiagnosticsContext newContext)
{
if (Object.ReferenceEquals(this, newContext))
Expand All @@ -175,12 +173,6 @@ private void AddSummaryInfo(CosmosDiagnosticsContext newContext)
this.SetSdkUserAgent(newContext.UserAgent);
}

// Use the larger of the total elapsed times
if (this.TotalElapsedTime < newContext.TotalElapsedTime)
{
this.TotalElapsedTime = newContext.TotalElapsedTime;
}

this.TotalRequestCount += newContext.TotalRequestCount;
this.FailedRequestCount += newContext.FailedRequestCount;
}
Expand Down
33 changes: 33 additions & 0 deletions Microsoft.Azure.Cosmos/src/Diagnostics/CosmosDiagnosticsCore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;

/// <summary>
/// Contains the cosmos diagnostic information for the current request to Azure Cosmos DB service.
/// </summary>
internal class CosmosDiagnosticsCore : CosmosDiagnostics
{
internal CosmosDiagnosticsCore(CosmosDiagnosticsContext diagnosticsContext)
{
this.Context = diagnosticsContext ?? throw new ArgumentNullException(nameof(diagnosticsContext));
}

internal CosmosDiagnosticsContext Context { get; }

/// <inheritdoc/>
public override TimeSpan GetClientElapsedTime()
{
return this.Context.GetClientElapsedTime();
}

/// <inheritdoc/>
public override string ToString()
{
return this.Context.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Azure.Cosmos.Diagnostics
/// <summary>
/// Extends <see cref="CosmosDiagnostics"/> to expose internal APIs.
/// </summary>
internal abstract class CosmosDiagnosticsInternal : CosmosDiagnostics
internal abstract class CosmosDiagnosticsInternal
{
public abstract void Accept(CosmosDiagnosticsInternalVisitor visitor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ public override void Visit(CosmosDiagnosticsContext cosmosDiagnosticsContext)
this.jsonWriter.WritePropertyName("StartUtc");
this.jsonWriter.WriteValue(cosmosDiagnosticsContext.StartUtc.ToString("o", CultureInfo.InvariantCulture));

this.jsonWriter.WritePropertyName("ElapsedTime");

if (cosmosDiagnosticsContext.TotalElapsedTime.HasValue)
if (cosmosDiagnosticsContext.IsComplete())
{
this.jsonWriter.WriteValue(cosmosDiagnosticsContext.TotalElapsedTime.Value);
this.jsonWriter.WritePropertyName("RunningElapsedTime");
}
else
{
this.jsonWriter.WriteValue("Timer Never Stopped.");
this.jsonWriter.WritePropertyName("TotalElapsedTime");
}

this.jsonWriter.WriteValue(cosmosDiagnosticsContext.GetClientElapsedTime());

this.jsonWriter.WritePropertyName("UserAgent");
this.jsonWriter.WriteValue(cosmosDiagnosticsContext.UserAgent);
Expand Down Expand Up @@ -109,16 +109,17 @@ public override void Visit(CosmosDiagnosticScope cosmosDiagnosticScope)
this.jsonWriter.WritePropertyName("Id");
this.jsonWriter.WriteValue(cosmosDiagnosticScope.Id);

this.jsonWriter.WritePropertyName("ElapsedTime");
if (cosmosDiagnosticScope.TryGetElapsedTime(out TimeSpan elapsedTime))
if (cosmosDiagnosticScope.IsComplete())
{
this.jsonWriter.WriteValue(elapsedTime);
this.jsonWriter.WritePropertyName("RunningElapsedTime");
}
else
{
this.jsonWriter.WriteValue("Timer Never Stopped.");
this.jsonWriter.WritePropertyName("ElapsedTime");
}

this.jsonWriter.WriteValue(cosmosDiagnosticScope.GetElapsedTime());

this.jsonWriter.WriteEndObject();
}

Expand Down
Loading

0 comments on commit 56d98c5

Please sign in to comment.