Skip to content

Commit

Permalink
Merge branch 'master' into users/sourabhjain/tracesnamechanges
Browse files Browse the repository at this point in the history
  • Loading branch information
kirankumarkolli authored Oct 8, 2024
2 parents 9a49edc + 757b004 commit a6f3081
Show file tree
Hide file tree
Showing 10 changed files with 3,455 additions and 45 deletions.
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ClientOfficialVersion>3.43.1</ClientOfficialVersion>
<ClientPreviewVersion>3.44.0</ClientPreviewVersion>
<ClientPreviewSuffixVersion>preview.1</ClientPreviewSuffixVersion>
<ClientOfficialVersion>3.44.0</ClientOfficialVersion>
<ClientPreviewVersion>3.45.0</ClientPreviewVersion>
<ClientPreviewSuffixVersion>preview.0</ClientPreviewSuffixVersion>
<DirectVersion>3.36.0</DirectVersion>
<EncryptionOfficialVersion>2.0.4</EncryptionOfficialVersion>
<EncryptionPreviewVersion>2.1.0</EncryptionPreviewVersion>
Expand Down
1,591 changes: 1,591 additions & 0 deletions Microsoft.Azure.Cosmos/contracts/API_3.44.0.txt

Large diffs are not rendered by default.

1,706 changes: 1,706 additions & 0 deletions Microsoft.Azure.Cosmos/contracts/API_3.45.0-preview.0.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Azure.Cosmos
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Diagnostics;
using Microsoft.Azure.Cosmos.Telemetry;
using Microsoft.Azure.Cosmos.Telemetry.Diagnostics;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;

Expand Down Expand Up @@ -296,7 +297,10 @@ internal static void RecordOtelAttributes(CosmosException exception, DiagnosticS
ClientTelemetryHelper.GetContactedRegions(exception.Diagnostics?.GetContactedRegions()));
scope.AddAttribute(OpenTelemetryAttributeKeys.ExceptionMessage, exception.Message);

CosmosDbEventSource.RecordDiagnosticsForExceptions(exception.Diagnostics);
if (!DiagnosticsFilterHelper.IsSuccessfulResponse(exception.StatusCode, (int)exception.SubStatusCode))
{
CosmosDbEventSource.RecordDiagnosticsForExceptions(exception.Diagnostics);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System.Linq;
using Cosmos.Util;
using Microsoft.Azure.Cosmos.Telemetry.Models;
using System.Threading;

public abstract class ClientTelemetryTestsBase : BaseCosmosClientHelper
{
Expand All @@ -48,6 +49,8 @@ public abstract class ClientTelemetryTestsBase : BaseCosmosClientHelper
protected HttpClientHandlerHelper httpHandler;
protected HttpClientHandlerHelper httpHandlerForNonAzureInstance;

protected ManualResetEventSlim eventSlim;

private bool isClientTelemetryAPICallFailed = false;

public static void ClassInitialize(TestContext _)
Expand All @@ -63,6 +66,8 @@ public static void ClassCleanup()

public virtual void TestInitialize()
{
this.eventSlim = new ManualResetEventSlim(false);

this.actualInfo = new List<ClientTelemetryProperties>();

this.httpHandler = new HttpClientHandlerHelper
Expand All @@ -80,11 +85,13 @@ public virtual void TestInitialize()
}
return this.HttpHandlerRequestCallbackChecks(request);
},
ResponseIntercepter = (response) =>
ResponseIntercepter = (response, request) =>
{
if (response.RequestMessage != null && response.RequestMessage.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);

this.eventSlim.Set();
}

return Task.FromResult(response);
Expand All @@ -94,6 +101,8 @@ public virtual void TestInitialize()
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
this.isClientTelemetryAPICallFailed = true;

this.eventSlim.Set();
}
}
};
Expand All @@ -120,11 +129,13 @@ public virtual void TestInitialize()

return this.HttpHandlerRequestCallbackChecks(request);
},
ResponseIntercepter = (response) =>
ResponseIntercepter = (response, request) =>
{
if (response.RequestMessage != null && response.RequestMessage.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);

this.eventSlim.Set();
}
return Task.FromResult(response);
},
Expand All @@ -133,6 +144,8 @@ public virtual void TestInitialize()
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
this.isClientTelemetryAPICallFailed = true;

this.eventSlim.Set();
}
}
};
Expand Down Expand Up @@ -164,6 +177,8 @@ public virtual async Task Cleanup()
await base.TestCleanup();

Assert.IsFalse(this.isClientTelemetryAPICallFailed, $"Call to client telemetry service endpoint (i.e. {telemetryServiceEndpoint}) failed");

this.eventSlim.Reset();
}

public virtual async Task PointSuccessOperationsTest(ConnectionMode mode, bool isAzureInstance)
Expand Down Expand Up @@ -209,7 +224,7 @@ public virtual async Task PointSuccessOperationsTest(ConnectionMode mode, bool i
{ Documents.OperationType.Delete.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 12,
this.WaitAndAssert(expectedOperationCount: 12,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
isAzureInstance: isAzureInstance);
}
Expand Down Expand Up @@ -240,7 +255,7 @@ await container.ReadItemAsync<JObject>(
{ Documents.OperationType.Read.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.Eventual,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedCacheSource: null,
Expand Down Expand Up @@ -273,7 +288,7 @@ await container.ReadItemStreamAsync(
{ Documents.OperationType.Read.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.ConsistentPrefix,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedCacheSource: null,
Expand Down Expand Up @@ -322,7 +337,7 @@ await container
{ Documents.OperationType.Delete.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 12,
this.WaitAndAssert(expectedOperationCount: 12,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedCacheSource: null);
}
Expand Down Expand Up @@ -352,7 +367,7 @@ public virtual async Task BatchOperationsTest(ConnectionMode mode)
{ Documents.OperationType.Batch.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.Eventual,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand All @@ -378,7 +393,7 @@ public virtual async Task SingleOperationMultipleTimesTest(ConnectionMode mode)
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,// 2 (read, requestLatency + requestCharge) + 2 (create, requestLatency + requestCharge)
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand Down Expand Up @@ -433,7 +448,7 @@ public virtual async Task QueryOperationSinglePartitionTest(ConnectionMode mode)
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 4,
this.WaitAndAssert(expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.ConsistentPrefix);
}
Expand Down Expand Up @@ -492,7 +507,7 @@ public virtual async Task QueryMultiPageSinglePartitionOperationTest(ConnectionM
{ Documents.OperationType.Create.ToString(), 2}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedConsistencyLevel: Microsoft.Azure.Cosmos.ConsistencyLevel.ConsistentPrefix);
Expand Down Expand Up @@ -540,7 +555,7 @@ await ToDoActivity.CreateRandomItems(
{ Documents.OperationType.Create.ToString(), 10}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand Down Expand Up @@ -591,7 +606,7 @@ await ToDoActivity.CreateRandomItems(
{ Documents.OperationType.Create.ToString(), 10}
};

await this.WaitAndAssert(
this.WaitAndAssert(
expectedOperationCount: 4,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}
Expand Down Expand Up @@ -629,7 +644,7 @@ public virtual async Task QueryOperationInvalidContinuationTokenTest(ConnectionM
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedOperationRecordCountMap: expectedRecordCountInOperation);
}

Expand Down Expand Up @@ -665,6 +680,16 @@ public virtual async Task CreateItemWithSubStatusCodeTest(ConnectionMode mode)
return this.HttpHandlerRequestCallbackChecks(request);
};

httpHandler.ResponseIntercepter = (response, request) =>
{
if (request.RequestUri.AbsoluteUri.Equals(telemetryServiceEndpoint.AbsoluteUri))
{
this.eventSlim.Set();
}

return Task.FromResult(response);
};

// Replacing originally initialized cosmos Builder with this one with new handler
this.cosmosClientBuilder = this.cosmosClientBuilder
.WithClientTelemetryOptions(new CosmosClientTelemetryOptions()
Expand Down Expand Up @@ -692,7 +717,7 @@ public virtual async Task CreateItemWithSubStatusCodeTest(ConnectionMode mode)
{ Documents.OperationType.Create.ToString(), 1}
};

await this.WaitAndAssert(expectedOperationCount: 2,
this.WaitAndAssert(expectedOperationCount: 2,
expectedOperationRecordCountMap: expectedRecordCountInOperation,
expectedSubstatuscode: 999999,
isExpectedNetworkTelemetry: false);
Expand All @@ -706,7 +731,7 @@ await this.WaitAndAssert(expectedOperationCount: 2,
/// <param name="expectedConsistencyLevel"> Expected Consistency level of the operation recorded by telemetry</param>
/// <param name="expectedOperationRecordCountMap"> Expected number of requests recorded for each operation </param>
/// <returns></returns>
private async Task WaitAndAssert(
private void WaitAndAssert(
int expectedOperationCount = 0,
Microsoft.Azure.Cosmos.ConsistencyLevel? expectedConsistencyLevel = null,
IDictionary<string, long> expectedOperationRecordCountMap = null,
Expand All @@ -724,7 +749,7 @@ private async Task WaitAndAssert(
HashSet<CacheRefreshInfo> cacheRefreshInfoSet = new HashSet<CacheRefreshInfo>();
do
{
await Task.Delay(TimeSpan.FromMilliseconds(1500)); // wait at least for 1 round of telemetry
this.eventSlim.Wait();

HashSet<OperationInfo> actualOperationSet = new HashSet<OperationInfo>();
HashSet<RequestInfo> requestInfoSet = new HashSet<RequestInfo>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private async Task<Container> CreateContainer(bool isLocalQuorumConsistency)
{
HttpClientHandlerHelper httpHandler = new HttpClientHandlerHelper
{
ResponseIntercepter = async (response) =>
ResponseIntercepter = async (response, _) =>
{
string responseString = await response.Content.ReadAsStringAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tracing;
Expand All @@ -34,6 +35,9 @@ public void TestCleanup()
[TestMethod]
public async Task VerifyPkRangeCacheRefreshOnSplitWithErrorsAsync()
{
ManualResetEventSlim signalSplitException = new ManualResetEventSlim(false);
ManualResetEventSlim pkRangesRefreshed = new ManualResetEventSlim(false);

int throwOnPkRefreshCount = 3;
int pkRangeCalls = 0;
bool causeSplitExceptionInRntbdCall = false;
Expand All @@ -54,9 +58,18 @@ public async Task VerifyPkRangeCacheRefreshOnSplitWithErrorsAsync()
if (pkRangeCalls == throwOnPkRefreshCount)
{
failedIfNoneMatchValue = request.Headers.IfNoneMatch.ToString();
if (signalSplitException.IsSet)
{
pkRangesRefreshed.Set();
}
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}

if (signalSplitException.IsSet)
{
pkRangesRefreshed.Set();
}

return null;
};

Expand All @@ -74,6 +87,8 @@ public async Task VerifyPkRangeCacheRefreshOnSplitWithErrorsAsync()
{
countSplitExceptions++;
causeSplitExceptionInRntbdCall = false;

signalSplitException.Set();
throw new Documents.Routing.PartitionKeyRangeIsSplittingException("Test");
}
})
Expand All @@ -91,37 +106,30 @@ public async Task VerifyPkRangeCacheRefreshOnSplitWithErrorsAsync()
400);

// Start a background job that loops forever
ManualResetEventSlim backgroundOperationReady = new ManualResetEventSlim(false);
List<Exception> exceptions = new();
Task backgroundItemOperatios = Task.Factory.StartNew(() => this.CreateAndReadItemBackgroundLoop(container, exceptions));
Task backgroundItemOperatios = Task.Factory.StartNew(() => this.CreateAndReadItemBackgroundLoop(container, exceptions, backgroundOperationReady));

// Wait for the background job to start
Documents.ValueStopwatch stopwatch = Documents.ValueStopwatch.StartNew();
while (!this.loopBackgroundOperaitons && stopwatch.Elapsed.TotalSeconds < 30)
{
await Task.Delay(TimeSpan.FromSeconds(.5));
}
backgroundOperationReady.Wait();

Assert.IsTrue(this.loopBackgroundOperaitons);
Assert.AreEqual(2, pkRangeCalls);

// Cause direct call to hit a split exception and wait for the background job to hit it
causeSplitExceptionInRntbdCall = true;
stopwatch = Documents.ValueStopwatch.StartNew();
while (causeSplitExceptionInRntbdCall && stopwatch.Elapsed.TotalSeconds < 10)
{
await Task.Delay(TimeSpan.FromSeconds(.5));
}
signalSplitException.Wait();
pkRangesRefreshed.Wait();
Assert.IsFalse(causeSplitExceptionInRntbdCall);
Assert.AreEqual(3, pkRangeCalls);

signalSplitException.Reset();
pkRangesRefreshed.Reset();

// Cause another direct call split exception
causeSplitExceptionInRntbdCall = true;
stopwatch = Documents.ValueStopwatch.StartNew();
while (causeSplitExceptionInRntbdCall && stopwatch.Elapsed.TotalSeconds < 10)
{
await Task.Delay(TimeSpan.FromSeconds(.5));
}

signalSplitException.Wait();
pkRangesRefreshed.Wait();
Assert.IsFalse(causeSplitExceptionInRntbdCall);

Assert.AreEqual(4, pkRangeCalls);
Expand All @@ -147,7 +155,7 @@ public async Task VerifyPkRangeCacheRefreshOnSplitWithErrorsAsync()
await db.DeleteStreamAsync();
}

private async Task CreateAndReadItemBackgroundLoop(Container container, List<Exception> exceptions)
private async Task CreateAndReadItemBackgroundLoop(Container container, List<Exception> exceptions, ManualResetEventSlim backgroundOperationReady)
{
this.loopBackgroundOperaitons = true;

Expand All @@ -167,6 +175,8 @@ private async Task CreateAndReadItemBackgroundLoop(Container container, List<Exc
{
exceptions.Add(ex);
}

backgroundOperationReady.Set();
}
}

Expand Down
Loading

0 comments on commit a6f3081

Please sign in to comment.