From ae6b51b7bd1eb85dfd9a3b0f4ec870a428d9f4a8 Mon Sep 17 00:00:00 2001 From: Sourabh Jain Date: Mon, 13 Mar 2023 17:20:05 +0530 Subject: [PATCH] try1 --- .../OpenTelemetryRecorderFactory.cs | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs index 6c88b3d5db..8a276d4bbf 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/OpenTelemetry/OpenTelemetryRecorderFactory.cs @@ -17,6 +17,8 @@ internal static class OpenTelemetryRecorderFactory /// private static DiagnosticScopeFactory ScopeFactory { get; set; } + private static readonly object lockObject = new object(); + public static OpenTelemetryCoreRecorder CreateRecorder(string operationName, string containerName, string databaseName, @@ -26,28 +28,32 @@ public static OpenTelemetryCoreRecorder CreateRecorder(string operationName, { if (clientContext is { ClientOptions.IsDistributedTracingEnabled: true }) { - OpenTelemetryRecorderFactory.ScopeFactory ??= new DiagnosticScopeFactory(clientNamespace: OpenTelemetryAttributeKeys.DiagnosticNamespace, - resourceProviderNamespace: OpenTelemetryAttributeKeys.ResourceProviderNamespace, - isActivityEnabled: true); - - // If there is no source then it will return default otherwise a valid diagnostic scope - DiagnosticScope scope = OpenTelemetryRecorderFactory - .ScopeFactory - .CreateScope(name: $"{OpenTelemetryAttributeKeys.OperationPrefix}.{operationName}", - kind: clientContext.ClientOptions.ConnectionMode == ConnectionMode.Gateway ? DiagnosticScope.ActivityKind.Internal : DiagnosticScope.ActivityKind.Client); - - // Record values only when we have a valid Diagnostic Scope - if (scope.IsEnabled) + lock (lockObject) { - return new OpenTelemetryCoreRecorder( - scope: scope, - operationName: operationName, - containerName: containerName, - databaseName: databaseName, - operationType: operationType, - clientContext: clientContext, - config: requestOptions?.DistributedTracingOptions ?? clientContext.ClientOptions?.DistributedTracingOptions); + OpenTelemetryRecorderFactory.ScopeFactory ??= new DiagnosticScopeFactory(clientNamespace: OpenTelemetryAttributeKeys.DiagnosticNamespace, + resourceProviderNamespace: OpenTelemetryAttributeKeys.ResourceProviderNamespace, + isActivityEnabled: true); + + // If there is no source then it will return default otherwise a valid diagnostic scope + DiagnosticScope scope = OpenTelemetryRecorderFactory + .ScopeFactory + .CreateScope(name: $"{OpenTelemetryAttributeKeys.OperationPrefix}.{operationName}", + kind: clientContext.ClientOptions.ConnectionMode == ConnectionMode.Gateway ? DiagnosticScope.ActivityKind.Internal : DiagnosticScope.ActivityKind.Client); + + // Record values only when we have a valid Diagnostic Scope + if (scope.IsEnabled) + { + return new OpenTelemetryCoreRecorder( + scope: scope, + operationName: operationName, + containerName: containerName, + databaseName: databaseName, + operationType: operationType, + clientContext: clientContext, + config: requestOptions?.DistributedTracingOptions ?? clientContext.ClientOptions?.DistributedTracingOptions); + } } + } return default;