Skip to content

Commit

Permalink
OperationCanceledException: Adds Exception Trace as Child (#3319)
Browse files Browse the repository at this point in the history
* add exception trace as a child instead of at beginning

* add test to validate diagnostics ordering

* add using statement to consistently dispose the trace
  • Loading branch information
imanvt authored Jun 30, 2022
1 parent cd18f1d commit 0a307be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ internal CosmosOperationCanceledException(
throw new ArgumentNullException(nameof(trace));
}

trace.AddDatum("Operation Cancelled Exception", originalException);
using (ITrace child = trace.StartChild("CosmosOperationCanceledException"))
{
child.AddDatum("Operation Cancelled Exception", originalException);
}
this.Diagnostics = new CosmosTraceDiagnostics(trace);
this.tokenCancellationRequested = originalException.CancellationToken.IsCancellationRequested;
this.toStringMessage = this.CreateToStringMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Diagnostics;
using Microsoft.Azure.Cosmos.Tracing;
Expand Down Expand Up @@ -133,5 +134,27 @@ public async Task CachedSerializationTest()
string deserializedJson = Encoding.UTF8.GetString(clientConfigurationTraceDatum.SerializedJson.Span);
Assert.IsTrue(deserializedJson.Contains("ConnectionMode"));
}

[TestMethod]
public async Task VerifyDiagnosticsOrderTest()
{
ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
ItemResponse<ToDoActivity> response = await this.Container.CreateItemAsync(testItem, new Cosmos.PartitionKey(testItem.pk));
ITrace trace = ((CosmosTraceDiagnostics)response.Diagnostics).Value;
TestCommon.CreateCosmosClient();
CancellationToken token = new CancellationToken(canceled: true);
try
{
response = await this.Container.ReadItemAsync<ToDoActivity>(testItem.id, new Cosmos.PartitionKey(testItem.pk), cancellationToken: token);
Assert.Fail("Test should throw/catch a CosmosOperationCanceledException");
}
catch (CosmosOperationCanceledException oce)
{
IReadOnlyList<ITrace> children = ((CosmosTraceDiagnostics)oce.Diagnostics).Value.Children;
ITrace exceptionChild = children[^1];
Assert.AreEqual("CosmosOperationCanceledException", exceptionChild.Name);
Assert.IsNotNull(exceptionChild.Data["Operation Cancelled Exception"]);
}
}
}
}

0 comments on commit 0a307be

Please sign in to comment.