Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading