-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CosmosClientSideRequestStatistics merged into DiagnosticContext (#1189)
* Add ability to disable diagnostic on a request level. * Updated unit test * Exposed API in request options * Updated naming * Renamed to Empty as prefix * Moving CosmosClientSideRequestStatistics to diagnostics * CosmosClientSideRequestStatistics is integrated into DiagnosticContext. * Fixing comments * Updated baseline test * Create if not exist API now contain all context information instead of the last call. * Updated CosmosClientSideRequestStatistics to include entire diagnostic context on tostring to avoid missing information. * IClientSideRequestStatistics now returns the v2 ToString version to avoid breaking changes. * Fixed comments. Removed PointOperationStats when StoreResponseStats is populated. * Fixed tests, added response time to PointOperationStats * Refactored to use CTOR when possible. Updated logic to handle gateway requests better. * Add v2 compatibility to avoid issues from the additional size of the diagnostic context. * Added Utc to end of time properties. * Updated test * IClientSideRequestStatistics ToString methods now just call the new DiagnosticContext to get the full information.
- Loading branch information
Showing
50 changed files
with
756 additions
and
770 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Microsoft.Azure.Cosmos/src/Diagnostics/AddressResolutionStatistics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
namespace Microsoft.Azure.Cosmos.Diagnostics | ||
{ | ||
using System; | ||
|
||
internal sealed class AddressResolutionStatistics : CosmosDiagnosticsInternal | ||
{ | ||
public AddressResolutionStatistics( | ||
DateTime startTime, | ||
DateTime endTime, | ||
string targetEndpoint) | ||
{ | ||
this.StartTime = startTime; | ||
this.EndTime = endTime; | ||
this.TargetEndpoint = targetEndpoint ?? throw new ArgumentNullException(nameof(startTime)); | ||
} | ||
|
||
public DateTime StartTime { get; } | ||
public DateTime? EndTime { get; set; } | ||
public string TargetEndpoint { get; } | ||
|
||
public override void Accept(CosmosDiagnosticsInternalVisitor visitor) | ||
{ | ||
visitor.Visit(this); | ||
} | ||
|
||
public override TResult Accept<TResult>(CosmosDiagnosticsInternalVisitor<TResult> visitor) | ||
{ | ||
return visitor.Visit(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
Microsoft.Azure.Cosmos/src/Diagnostics/CosmosClientSideRequestStatistics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Text; | ||
using Microsoft.Azure.Cosmos.Diagnostics; | ||
using Microsoft.Azure.Documents; | ||
using Newtonsoft.Json; | ||
|
||
internal sealed class CosmosClientSideRequestStatistics : CosmosDiagnosticsInternal, IClientSideRequestStatistics | ||
{ | ||
private readonly object lockObject = new object(); | ||
|
||
public CosmosClientSideRequestStatistics(CosmosDiagnosticsContext diagnosticsContext = null) | ||
{ | ||
this.RequestStartTimeUtc = DateTime.UtcNow; | ||
this.RequestEndTimeUtc = null; | ||
this.EndpointToAddressResolutionStatistics = new Dictionary<string, AddressResolutionStatistics>(); | ||
this.ContactedReplicas = new List<Uri>(); | ||
this.FailedReplicas = new HashSet<Uri>(); | ||
this.RegionsContacted = new HashSet<Uri>(); | ||
this.DiagnosticsContext = diagnosticsContext ?? new CosmosDiagnosticsContextCore(); | ||
this.DiagnosticsContext.AddDiagnosticsInternal(this); | ||
} | ||
|
||
private DateTime RequestStartTimeUtc { get; } | ||
|
||
private DateTime? RequestEndTimeUtc { get; set; } | ||
|
||
private Dictionary<string, AddressResolutionStatistics> EndpointToAddressResolutionStatistics { get; } | ||
|
||
public List<Uri> ContactedReplicas { get; set; } | ||
|
||
public HashSet<Uri> FailedReplicas { get; } | ||
|
||
public HashSet<Uri> RegionsContacted { get; } | ||
|
||
public TimeSpan RequestLatency | ||
{ | ||
get | ||
{ | ||
if (this.RequestEndTimeUtc.HasValue) | ||
{ | ||
return this.RequestEndTimeUtc.Value - this.RequestStartTimeUtc; | ||
} | ||
|
||
return TimeSpan.MaxValue; | ||
} | ||
} | ||
|
||
public bool IsCpuOverloaded { get; private set; } = false; | ||
|
||
public CosmosDiagnosticsContext DiagnosticsContext { get; } | ||
|
||
public void RecordRequest(DocumentServiceRequest request) | ||
{ | ||
} | ||
|
||
public void RecordResponse(DocumentServiceRequest request, StoreResult storeResult) | ||
{ | ||
DateTime responseTime = DateTime.UtcNow; | ||
Uri locationEndpoint = request.RequestContext.LocationEndpointToRoute; | ||
StoreResponseStatistics responseStatistics = new StoreResponseStatistics( | ||
responseTime, | ||
storeResult, | ||
request.ResourceType, | ||
request.OperationType, | ||
locationEndpoint); | ||
|
||
if (storeResult?.IsClientCpuOverloaded ?? false) | ||
{ | ||
this.IsCpuOverloaded = true; | ||
} | ||
|
||
lock (this.lockObject) | ||
{ | ||
if (!this.RequestEndTimeUtc.HasValue || responseTime > this.RequestEndTimeUtc) | ||
{ | ||
this.RequestEndTimeUtc = responseTime; | ||
} | ||
|
||
if (locationEndpoint != null) | ||
{ | ||
this.RegionsContacted.Add(locationEndpoint); | ||
} | ||
|
||
this.DiagnosticsContext.AddDiagnosticsInternal(responseStatistics); | ||
} | ||
} | ||
|
||
public string RecordAddressResolutionStart(Uri targetEndpoint) | ||
{ | ||
string identifier = Guid.NewGuid().ToString(); | ||
AddressResolutionStatistics resolutionStats = new AddressResolutionStatistics( | ||
startTime: DateTime.UtcNow, | ||
endTime: DateTime.MaxValue, | ||
targetEndpoint: targetEndpoint == null ? "<NULL>" : targetEndpoint.ToString()); | ||
|
||
lock (this.lockObject) | ||
{ | ||
this.EndpointToAddressResolutionStatistics.Add(identifier, resolutionStats); | ||
this.DiagnosticsContext.AddDiagnosticsInternal(resolutionStats); | ||
} | ||
|
||
return identifier; | ||
} | ||
|
||
public void RecordAddressResolutionEnd(string identifier) | ||
{ | ||
if (string.IsNullOrEmpty(identifier)) | ||
{ | ||
return; | ||
} | ||
|
||
DateTime responseTime = DateTime.UtcNow; | ||
lock (this.lockObject) | ||
{ | ||
if (!this.EndpointToAddressResolutionStatistics.ContainsKey(identifier)) | ||
{ | ||
throw new ArgumentException("Identifier {0} does not exist. Please call start before calling end.", identifier); | ||
} | ||
|
||
if (!this.RequestEndTimeUtc.HasValue || responseTime > this.RequestEndTimeUtc) | ||
{ | ||
this.RequestEndTimeUtc = responseTime; | ||
} | ||
|
||
this.EndpointToAddressResolutionStatistics[identifier].EndTime = responseTime; | ||
} | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
// This is required for the older IClientSideRequestStatistics | ||
// Capture the entire diagnostic context in the toString to avoid losing any information | ||
// for any APIs using the older interface. | ||
return this.DiagnosticsContext.ToString(); | ||
} | ||
|
||
public void AppendToBuilder(StringBuilder stringBuilder) | ||
{ | ||
// This is required for the older IClientSideRequestStatistics | ||
// Capture the entire diagnostic context in the toString to avoid losing any information | ||
// for any APIs using the older interface. | ||
stringBuilder.Append(this.DiagnosticsContext.ToString()); | ||
} | ||
|
||
public override void Accept(CosmosDiagnosticsInternalVisitor visitor) | ||
{ | ||
visitor.Visit(this); | ||
} | ||
|
||
public override TResult Accept<TResult>(CosmosDiagnosticsInternalVisitor<TResult> visitor) | ||
{ | ||
return visitor.Visit(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.