diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs b/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs index 562ccb2902..24e9768379 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs @@ -71,7 +71,6 @@ static async Task Main() AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true); _traceProvider = Sdk.CreateTracerProviderBuilder() .AddSource("Azure.Cosmos.Operation", // Cosmos DB source for operation level telemetry - "Azure.Cosmos.Request", // Cosmos DB source for DIRECT Mode network request level telemetry "Sample.Application") .AddAzureMonitorTraceExporter(o => o.ConnectionString = aiConnectionString) // Set up exporter of your choice .AddHttpClientInstrumentation() // Added to capture HTTP telemetry diff --git a/Microsoft.Azure.Cosmos/src/DocumentClient.cs b/Microsoft.Azure.Cosmos/src/DocumentClient.cs index 873f09fa51..ce1c9a43c2 100644 --- a/Microsoft.Azure.Cosmos/src/DocumentClient.cs +++ b/Microsoft.Azure.Cosmos/src/DocumentClient.cs @@ -6654,9 +6654,16 @@ private void InitializeDirectConnectivity(IStoreClientFactory storeClientFactory } else { + // It is decided to switch this feature off completely for external users but keep it unchanged for internal users, + // due to the nature of information, we are collecting here. RNTBD is internal protocol and we do not expose it to the customers. Documents.Telemetry.DistributedTracingOptions distributedTracingOptions = new () { +#if INTERNAL IsDistributedTracingEnabled = !this.cosmosClientTelemetryOptions.DisableDistributedTracing +#else + IsDistributedTracingEnabled = false +#endif + }; StoreClientFactory newClientFactory = new StoreClientFactory( diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs index 8117dda493..f1d02583f9 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs @@ -57,24 +57,7 @@ public static OpenTelemetryCoreRecorder CreateRecorder(string operationName, clientContext: clientContext, config: requestOptions?.CosmosThresholdOptions ?? clientContext.ClientOptions?.CosmosClientTelemetryOptions.CosmosThresholdOptions); } -#if !INTERNAL - // Need a parent activity which groups all network activities under it and is logged in diagnostics and used for tracing purpose. - // If there are listeners at network level then scope is enabled and it tries to create activity. - // However, if available listeners are not subscribed to network event then it will lead to scope being enabled but no activity is created. - else - { - DiagnosticScope requestScope = LazyScopeFactory.Value.CreateScope(name: $"{OpenTelemetryAttributeKeys.NetworkLevelPrefix}.{operationName}"); - openTelemetryRecorder = requestScope.IsEnabled ? OpenTelemetryCoreRecorder.CreateNetworkLevelParentActivity(networkScope: requestScope) : openTelemetryRecorder; - } - // If there are no listeners at operation level and network level and no parent activity created. - // Then create a dummy activity as there should be a parent level activity always to send a traceid to the backend services through context propagation. - // The parent activity id is logged in diagnostics and used for tracing purpose. - if (Activity.Current is null) - { - openTelemetryRecorder = OpenTelemetryCoreRecorder.CreateParentActivity(operationName); - } -#endif // Safety check as diagnostic logs should not break the code. if (Activity.Current?.TraceId != null) { diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BatchOperationsAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BatchOperationsAsync.xml index be3f14d016..8e8a427763 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BatchOperationsAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BatchOperationsAsync.xml @@ -160,13 +160,6 @@ South Central US 400/1001 - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 207 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml index 5de1a309d7..af8d3b4cf7 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.BulkOperationsAsync.xml @@ -343,13 +343,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -687,13 +680,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -1031,13 +1017,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -1375,13 +1354,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -1719,13 +1691,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -2063,13 +2028,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -2407,13 +2365,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -2751,13 +2702,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -3095,13 +3039,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -3439,13 +3376,6 @@ Some Value Some Value - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ChangeFeedAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ChangeFeedAsync.xml index 42d0cb4f23..9d6cb5c5a9 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ChangeFeedAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ChangeFeedAsync.xml @@ -1118,20 +1118,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 304 - @@ -1878,20 +1864,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 304 - @@ -2619,20 +2591,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 304 - @@ -3380,20 +3338,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 304 - @@ -3718,13 +3662,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.QueryAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.QueryAsync.xml index 5a1d7b5c43..7577f2ae4f 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.QueryAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.QueryAsync.xml @@ -723,13 +723,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -1480,13 +1473,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -2218,13 +2204,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -2976,13 +2955,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -3808,13 +3780,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -4569,13 +4534,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -5350,13 +5308,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadFeedAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadFeedAsync.xml index 78241c3050..736119560b 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadFeedAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadFeedAsync.xml @@ -656,13 +656,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -1346,13 +1339,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -2017,13 +2003,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -2708,13 +2687,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadManyAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadManyAsync.xml index ad6be29190..5f242a1d00 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadManyAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.ReadManyAsync.xml @@ -491,13 +491,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -1005,13 +998,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.StreamPointOperationsAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.StreamPointOperationsAsync.xml index 6bf7447aad..d49a6731e7 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.StreamPointOperationsAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.StreamPointOperationsAsync.xml @@ -118,13 +118,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 201 - @@ -241,13 +234,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -372,13 +358,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -498,13 +477,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 204 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.TypedPointOperationsAsync.xml b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.TypedPointOperationsAsync.xml index c3ce1e8786..8cd5278ea1 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.TypedPointOperationsAsync.xml +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/BaselineTest/TestBaseline/EndToEndTraceWriterBaselineTests.TypedPointOperationsAsync.xml @@ -138,13 +138,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 201 - @@ -266,13 +259,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -407,13 +393,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 200 - @@ -537,13 +516,6 @@ Some Value South Central US - - Microsoft.DocumentDB - https://opentelemetry.io/schemas/1.23.0 - Some Value - 0 - 204 - diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/DistributedTracingOTelTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/DistributedTracingOTelTests.cs deleted file mode 100644 index 43d9be341d..0000000000 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/DistributedTracingOTelTests.cs +++ /dev/null @@ -1,278 +0,0 @@ -//------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -//------------------------------------------------------------ - -namespace Microsoft.Azure.Cosmos -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Threading.Tasks; - using Microsoft.Azure.Cosmos.CosmosElements; - using Microsoft.Azure.Cosmos.SDK.EmulatorTests; - using Microsoft.VisualStudio.TestTools.UnitTesting; - using Newtonsoft.Json.Linq; - using OpenTelemetry.Trace; - using OpenTelemetry; - using AzureCore = global::Azure.Core; - using Microsoft.Azure.Cosmos.Telemetry; - using System.Diagnostics; - using Microsoft.Azure.Cosmos.Tracing; - using System.Net.Http; - using Microsoft.Azure.Cosmos.Tests; - - [VisualStudio.TestTools.UnitTesting.TestClass] - public sealed class DistributedTracingOTelTests : BaseCosmosClientHelper - { - [TestInitialize] - public void TestInitialize() - { - AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true); - AzureCore.ActivityExtensions.ResetFeatureSwitch(); - } - - [DataTestMethod] - [DataRow($"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Operation", $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Request", DisplayName = "DirectMode and DistributedFlag On: Asserts activity creation at operation and network level with Diagnostic TraceId being added to logs")] - [DataRow($"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Operation", null, DisplayName = "DirectMode and DistributedFlag On: Asserts activity creation at operation level with Diagnostic TraceId being added to logs")] - [DataRow(null, $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Request", DisplayName = "DirectMode and DistributedFlag On: Asserts activity creation at network level with Diagnostic TraceId being added to logs")] - public async Task SourceEnabled_FlagOn_DirectMode_RecordsOperationNetworkActivity_AssertLogTraceId_AssertTraceparent(string operationLevelSource, string networkLevelSource) - { - string[] sources = new string[] { operationLevelSource, networkLevelSource }; - sources = sources.Where(x => x != null).ToArray(); - - using TracerProvider provider = Sdk.CreateTracerProviderBuilder() - .AddCustomOtelExporter() - .AddSource(sources) - .Build(); - - await base.TestInit(validateSinglePartitionKeyRangeCacheCall: false, - customizeClientBuilder: (builder) => builder - .WithClientTelemetryOptions(new CosmosClientTelemetryOptions() - { - DisableDistributedTracing = false - }) - .WithConnectionModeDirect()); - - Container containerResponse = await this.database.CreateContainerAsync( - id: Guid.NewGuid().ToString(), - partitionKeyPath: "/id", - throughput: 20000); - - CosmosObject cosmosObject = CosmosObject.Create( - new Dictionary() - { - { "id", CosmosString.Create("1") } - }); - - ItemResponse createResponse = await containerResponse.CreateItemAsync(JToken.Parse(cosmosObject.ToString())); - - //Assert traceparent header in Direct mode request - Assert.IsTrue(createResponse.RequestMessage.Headers.TryGetValue("traceparent", out string traceheader)); - Assert.IsNotNull(traceheader); - string[] traceheaderParts = traceheader.Split('-'); - string traceheaderId = traceheaderParts[1]; - - //Assert traceId in Diagnostics logs - string diagnosticsCreateItem = createResponse.Diagnostics.ToString(); - JObject objDiagnosticsCreate = JObject.Parse(diagnosticsCreateItem); - string distributedTraceId = (string)objDiagnosticsCreate["data"]["DistributedTraceId"]; - Assert.IsFalse(string.IsNullOrEmpty(distributedTraceId)); - - //Assert diagnostics log trace id is same as parent trace id of the activity - string operationName = (string)objDiagnosticsCreate["name"]; - string traceIdCreateItem = CustomOtelExporter.CollectedActivities - .Where(x => x.OperationName.Contains(operationName)) - .FirstOrDefault() - .TraceId - .ToString(); - //Assert created activity traceId and diagnosticsLog traceId - Assert.AreEqual(distributedTraceId, traceIdCreateItem); - - //Assert requestHeader trace id and and diagnosticsLog traceId - Assert.AreEqual(distributedTraceId, traceheaderId); - - //Assert activity creation - Assert.IsNotNull(CustomOtelExporter.CollectedActivities); - - if (networkLevelSource != null) - { - // Assert activity created at network level have an existing parent activity - Activity networkLevelChildActivity = CustomOtelExporter.CollectedActivities - .Where(x => x.OperationName.Contains("Request")) - .FirstOrDefault(); - Assert.IsNotNull(CustomOtelExporter.CollectedActivities - .Where(x => x.Id == networkLevelChildActivity.ParentId)); - } - } - - [DataTestMethod] - [DataRow($"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Operation", $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Request", DisplayName = "GatewayMode and DistributedFlag On: Asserts activity creation at operation and network level with Diagnostic TraceId being added to logs")] - [DataRow($"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Operation", null, DisplayName = "GatewayMode and DistributedFlag On: Asserts activity creation at operation level with Diagnostic TraceId being added to logs")] - [DataRow(null, $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Request", DisplayName = "GatewayMode and DistributedFlag On: Asserts activity creation at network level with Diagnostic TraceId being added to logs")] - public async Task SourceEnabled_FlagOn_GatewayMode_RecordsOperationNetworkActivity_AssertLogTraceId_AssertTraceparent(string operationLevelSource, string networkLevelSource) - { - string[] sources = new string[] { operationLevelSource, networkLevelSource }; - sources = sources.Where(x => x != null).ToArray(); - - HttpClientHandlerHelper httpClientHandlerHelper = new HttpClientHandlerHelper - { - RequestCallBack = (request, cancellation) => - { - if (request.Headers.TryGetValues("traceparent", out IEnumerable traceparentHeaderValues)) - { - Assert.IsNotNull(traceparentHeaderValues); - } - return null; - } - }; - - using TracerProvider provider = Sdk.CreateTracerProviderBuilder() - .AddCustomOtelExporter() - .AddSource(sources) - .Build(); - - await base.TestInit(validateSinglePartitionKeyRangeCacheCall: false, - customizeClientBuilder: (builder) => builder - .WithClientTelemetryOptions(new CosmosClientTelemetryOptions() - { - DisableDistributedTracing = false - }) - .WithHttpClientFactory(() => new HttpClient(httpClientHandlerHelper)) - .WithConnectionModeGateway()); - - ContainerResponse containerResponse = await this.database.CreateContainerAsync( - id: Guid.NewGuid().ToString(), - partitionKeyPath: "/id", - throughput: 20000); - - //Assert traceId in Diagnostics logs - string diagnosticsCreateContainer = containerResponse.Diagnostics.ToString(); - JObject objDiagnosticsCreate = JObject.Parse(diagnosticsCreateContainer); - string distributedTraceId = (string)objDiagnosticsCreate["data"]["DistributedTraceId"]; - Assert.IsFalse(string.IsNullOrEmpty(distributedTraceId)); - - //Assert diagnostics log trace id is same as parent trace id of the activity - string operationName = (string)objDiagnosticsCreate["name"]; - string traceIdCreateContainer = CustomOtelExporter.CollectedActivities - .Where(x => x.OperationName.Contains(operationName)) - .FirstOrDefault() - .TraceId - .ToString(); - Assert.AreEqual(distributedTraceId, traceIdCreateContainer); - - //Assert activity creation - Assert.IsNotNull(CustomOtelExporter.CollectedActivities); - } - - [DataTestMethod] - [DataRow(false, false, "random.source.name", DisplayName = "DirectMode, DistributedFlag On, Random/No Source:Asserts no activity creation")] - [DataRow(true, false, "random.source.name", DisplayName = "GatewayMode, DistributedFlag On, Random/No Source:Asserts no activity creation")] - [DataRow(false, true, "random.source.name", DisplayName = "DirectMode, DistributedFlag Off, Random/No Source:Asserts no activity creation")] - [DataRow(true, true, "random.source.name", DisplayName = "GatewayMode, DistributedFlag Off, Random/No Source:Asserts no activity creation")] - [DataRow(false, true, $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Operation", DisplayName = "DirectMode, DistributedFlag Off, OperationLevel Source:Asserts no activity creation")] - [DataRow(true, true, $"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.Operation", DisplayName = "GatewayMode, DistributedFlag Off, OperationLevel Source:Asserts no activity creation")] - public async Task NoSourceNoFlagEnabled_ResultsInNoOperationNetworkActivityCreation_AssertLogTraceId(bool useGateway, bool disableDistributingTracing, string source) - { - using TracerProvider provider = Sdk.CreateTracerProviderBuilder() - .AddCustomOtelExporter() - .AddSource(source) - .Build(); - - if (useGateway) - { - await base.TestInit(validateSinglePartitionKeyRangeCacheCall: false, - customizeClientBuilder: (builder) => builder - .WithClientTelemetryOptions(new CosmosClientTelemetryOptions() - { - DisableDistributedTracing = disableDistributingTracing - }) - .WithConnectionModeGateway()); - } - else - { - await base.TestInit(validateSinglePartitionKeyRangeCacheCall: false, - customizeClientBuilder: (builder) => builder - .WithClientTelemetryOptions(new CosmosClientTelemetryOptions() - { - DisableDistributedTracing = disableDistributingTracing - })); - } - - ContainerResponse containerResponse = await this.database.CreateContainerAsync( - id: Guid.NewGuid().ToString(), - partitionKeyPath: "/id", - throughput: 20000); - - //Assert traceId in Diagnostics logs - string diagnosticsCreateContainer = containerResponse.Diagnostics.ToString(); - JObject objDiagnosticsCreate = JObject.Parse(diagnosticsCreateContainer); - - if (!disableDistributingTracing) - { - //DistributedTraceId present in logs - string distributedTraceId = (string)objDiagnosticsCreate["data"]["DistributedTraceId"]; - Assert.IsFalse(string.IsNullOrEmpty(distributedTraceId), "Distributed Trace Id is not there in diagnostics"); - } - else - { - //DistributedTraceId field not present in logs - Assert.IsNull(objDiagnosticsCreate["data"]["DistributedTraceId"], "Distributed Trace Id has value in diagnostics i.e. " + (string)objDiagnosticsCreate["data"]["DistributedTraceId"]); - } - - //Assert no activity with attached source is created - Assert.AreEqual(0, CustomOtelExporter.CollectedActivities.Count()); - } - - - [DataTestMethod] - [DataRow(false)] - [DataRow(true)] - public async Task SuppressListenerEvents_ResultsInNoScopeActivityCreation_AssertTraceIdNotNull(bool useGateway) - { - // Initialize CustomListener with suppression - CustomListener customListener = new CustomListener($"{OpenTelemetryAttributeKeys.DiagnosticNamespace}.*", "Azure-Cosmos-Operation-Request-Diagnostics", true); - - if (useGateway) - { - await base.TestInit(validateSinglePartitionKeyRangeCacheCall: false, - customizeClientBuilder: (builder) => builder - .WithClientTelemetryOptions(new CosmosClientTelemetryOptions() - { - DisableDistributedTracing = false - }) - .WithConnectionModeGateway()); - } - else - { - await base.TestInit(validateSinglePartitionKeyRangeCacheCall: false, - customizeClientBuilder: (builder) => builder - .WithClientTelemetryOptions(new CosmosClientTelemetryOptions() - { - DisableDistributedTracing = false - })); - } - - ContainerResponse containerResponse = await this.database.CreateContainerAsync( - id: Guid.NewGuid().ToString(), - partitionKeyPath: "/id", - throughput: 20000); - - // Assert traceId in Diagnostics logs - string diagnosticsCreateContainer = containerResponse.Diagnostics.ToString(); - JObject objDiagnosticsCreate = JObject.Parse(diagnosticsCreateContainer); - Assert.IsNotNull(objDiagnosticsCreate["data"]["DistributedTraceId"], "Distributed Trace Id has value in diagnostics i.e. " + (string)objDiagnosticsCreate["data"]["DistributedTraceId"]); - - // Cleanup - customListener.Dispose(); - } - - [TestCleanup] - public async Task CleanUp() - { - await base.TestCleanup(); - - AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", false); - AzureCore.ActivityExtensions.ResetFeatureSwitch(); - } - } -} \ No newline at end of file