Skip to content

Commit

Permalink
Added a check on Cosmos Exception to make sure events are generated o…
Browse files Browse the repository at this point in the history
…nly for the allowed status and substatuscode
  • Loading branch information
sourabh1007 committed Oct 8, 2024
1 parent 43c14a3 commit a5e8358
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
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 @@ -6,6 +6,8 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.IO;
using System.Net;
using System.Security.Cryptography;
Expand Down Expand Up @@ -421,6 +423,40 @@ public void ValidateErrorHandling()
}
}

[TestMethod]
public void GenerateEventWithDiagnosticWithFilterOnStatusCodeTest()
{
using (TestEventListener listener = new TestEventListener())
{
CosmosException exception = new CosmosException(
statusCode: HttpStatusCode.BadRequest,
message: "testmessage",
stackTrace: null,
headers: null,
trace: NoOpTrace.Singleton,
error: null,
innerException: null);

CosmosException.RecordOtelAttributes(exception, default);

Assert.AreEqual(1, TestEventListener.CapturedEvents.Count);

TestEventListener.CapturedEvents.Clear();

exception = new CosmosException(
statusCode: HttpStatusCode.NotFound,
message: "testmessage",
stackTrace: null,
headers: null,
trace: NoOpTrace.Singleton,
error: null,
innerException: null);

CosmosException.RecordOtelAttributes(exception, default);
Assert.AreEqual(0, TestEventListener.CapturedEvents.Count);
}
}

private void ValidateExceptionInfo(
CosmosException exception,
HttpStatusCode httpStatusCode,
Expand Down Expand Up @@ -474,4 +510,24 @@ private void ValidateExceptionInfo(
Assert.AreEqual(TimeSpan.FromMilliseconds(retryAfter), exception.Headers.RetryAfter);
}
}

internal class TestEventListener : EventListener
{
public static List<EventWrittenEventArgs> CapturedEvents { get; } = new List<EventWrittenEventArgs>();

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
// Add captured event to the list
CapturedEvents.Add(eventData);
}

protected override void OnEventSourceCreated(EventSource eventSource)
{
// Enable events from the SampleEventSource
if (eventSource.Name == "Azure-Cosmos-Operation-Request-Diagnostics")
{
this.EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.All);
}
}
}
}

0 comments on commit a5e8358

Please sign in to comment.