diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs index f1d02583f9..1b16f48307 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs @@ -57,10 +57,19 @@ public static OpenTelemetryCoreRecorder CreateRecorder(string operationName, clientContext: clientContext, config: requestOptions?.CosmosThresholdOptions ?? clientContext.ClientOptions?.CosmosClientTelemetryOptions.CosmosThresholdOptions); } - +#if !INTERNAL + // If there are no listeners at operation 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 logged in diagnostics, can be used for tracing purpose in backend. + 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) { + // This id would be useful to trace calls at backend services when distributed tracing feature is available there. trace.AddDatum("DistributedTraceId", Activity.Current.TraceId); } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientConfigurationDiagnosticTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientConfigurationDiagnosticTest.cs index 9a5c4255a7..742d6df4ab 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientConfigurationDiagnosticTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientConfigurationDiagnosticTest.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; @@ -47,9 +46,9 @@ public async Task ClientConfigTest() Assert.IsNotNull(response.Diagnostics); ITrace trace = ((CosmosTraceDiagnostics)response.Diagnostics).Value; #if PREVIEW - Assert.AreEqual(trace.Data.Count, 2, string.Join(",", trace.Data.Select(a => $"{a.Key}: {a.Value}"))); // Distributed Tracing Id + Assert.AreEqual(actual: trace.Data.Count, expected: 2, message: string.Join(",", trace.Data.Select(a => $"{a.Key}: {a.Value}"))); // Distributed Tracing Id #else - Assert.AreEqual(trace.Data.Count, 1, string.Join(",", trace.Data.Select(a => $"{a.Key}: {a.Value}"))); + Assert.AreEqual(actual: trace.Data.Count, expected: 1, message: string.Join(",", trace.Data.Select(a => $"{a.Key}: {a.Value}"))); #endif ClientConfigurationTraceDatum clientConfigurationTraceDatum = (ClientConfigurationTraceDatum)trace.Data["Client Configuration"]; Assert.IsNotNull(clientConfigurationTraceDatum.UserAgentContainer.UserAgent);