From b4136b584acee7ce507064d82538b54f89cb9b70 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Thu, 13 Apr 2023 22:51:42 -0700 Subject: [PATCH] Fix activity correlator to continue using same GUID for current thread activity --- .../Data/Common/ActivityCorrelator.cs | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs index ef6b9b6cd2..5823921abc 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/ActivityCorrelator.cs @@ -19,16 +19,14 @@ internal sealed class ActivityId internal readonly Guid Id; internal readonly uint Sequence; - internal ActivityId(uint sequence) + internal ActivityId(Guid? currentActivityId, uint sequence = 1) { - this.Id = Guid.NewGuid(); - this.Sequence = sequence; + Id = currentActivityId ?? Guid.NewGuid(); + Sequence = sequence; } public override string ToString() - { - return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.Id, this.Sequence); - } + => string.Format(CultureInfo.InvariantCulture, "{0}:{1}", Id, Sequence); } // Declare the ActivityId which will be stored in TLS. The Id is unique for each thread. @@ -40,27 +38,12 @@ public override string ToString() /// /// Get the current ActivityId /// - internal static ActivityId Current - { - get - { - if (t_tlsActivity == null) - { - t_tlsActivity = new ActivityId(1); - } - return t_tlsActivity; - } - } + internal static ActivityId Current => t_tlsActivity ??= new ActivityId(null); /// /// Increment the sequence number and generate the new ActivityId /// /// ActivityId - internal static ActivityId Next() - { - t_tlsActivity = new ActivityId( (t_tlsActivity?.Sequence ?? 0) + 1); - - return t_tlsActivity; - } + internal static ActivityId Next() => t_tlsActivity = new ActivityId(t_tlsActivity?.Id, (t_tlsActivity?.Sequence ?? 0) + 1); } }