33
44using System . Collections . Frozen ;
55using System . Diagnostics ;
6- using System . Text . RegularExpressions ;
76using Microsoft . DotNet . Cli . InternalMicrosoft ;
87using Microsoft . DotNet . Cli . Utils ;
98using Microsoft . DotNet . Configurer ;
10- using Microsoft . DotNet . Utilities ;
119
1210#if MICROSOFT_ENABLE_TELEMETRY_AZURE_MONITOR
1311using Azure . Monitor . OpenTelemetry . Exporter ;
2018
2119namespace Microsoft . DotNet . Cli . Telemetry ;
2220
23- public partial class TelemetryClient : ITelemetryClient
21+ public class TelemetryClient : ITelemetryClient
2422{
25- private const string CopilotAgentSessionIdPropertyName = "CopilotAgentSessionId" ;
2623 private static FrozenDictionary < string , string ? > s_commonProperties = [ ] ;
27- private readonly IEnvironmentProvider _environmentProvider ;
24+ private readonly TelemetryCommonProperties _commonPropertiesCollector ;
2825 private Task ? _trackEventTask ;
2926
3027 private static readonly MeterProviderBuilder s_metricsProviderBuilder ;
@@ -166,7 +163,8 @@ public TelemetryClient() : this(null) { }
166163
167164 public TelemetryClient ( string ? sessionId , IEnvironmentProvider ? environmentProvider = null )
168165 {
169- _environmentProvider = environmentProvider ?? new EnvironmentProvider ( ) ;
166+ environmentProvider ??= new EnvironmentProvider ( ) ;
167+ _commonPropertiesCollector = new TelemetryCommonProperties ( environmentProvider : environmentProvider ) ;
170168
171169 // This is some kind of special condition for MSBuild-related tests.
172170 if ( DisabledForTests )
@@ -177,7 +175,7 @@ public TelemetryClient(string? sessionId, IEnvironmentProvider? environmentProvi
177175 Instance = this ;
178176 IsInitialized = true ;
179177
180- Enabled = ! _environmentProvider . GetEnvironmentVariableAsBool ( EnvironmentVariableNames . TELEMETRY_OPTOUT ,
178+ Enabled = ! environmentProvider . GetEnvironmentVariableAsBool ( EnvironmentVariableNames . TELEMETRY_OPTOUT ,
181179 // When building in the official CI pipeline, this makes the complier enable telemetry by default. Otherwise, it is disabled.
182180 // It is the reason tests don't send telemetry, because we don't run tests in the official CI pipeline.
183181 defaultValue : CompileOptions . TelemetryOptOutDefault ) ;
@@ -203,10 +201,10 @@ public TelemetryClient(string? sessionId, IEnvironmentProvider? environmentProvi
203201
204202 var initialSessionId = ! string . IsNullOrEmpty ( sessionId )
205203 ? sessionId
206- : _environmentProvider . GetEnvironmentVariable ( EnvironmentVariableNames . DOTNET_CLI_TELEMETRY_SESSIONID ) ;
204+ : environmentProvider . GetEnvironmentVariable ( EnvironmentVariableNames . DOTNET_CLI_TELEMETRY_SESSIONID ) ;
207205
208206 CurrentSessionId ??= ! string . IsNullOrEmpty ( initialSessionId ) ? initialSessionId : Guid . NewGuid ( ) . ToString ( ) ;
209- s_commonProperties = new TelemetryCommonProperties ( ) . GetTelemetryCommonProperties ( CurrentSessionId ) ;
207+ s_commonProperties = _commonPropertiesCollector . GetTelemetryCommonProperties ( CurrentSessionId ) ;
210208 }
211209
212210 /// <summary>
@@ -356,12 +354,12 @@ public void TrackEvent(string eventName, IDictionary<string, string?>? propertie
356354 }
357355
358356 // Capture before queueing: a reused MSBuild host can change its environment for the next request.
359- string copilotAgentSessionId = GetCopilotAgentSessionId ( ) ;
357+ var contextProperties = _commonPropertiesCollector . GetTelemetryContextProperties ( ) ;
360358
361359 // Continue the task in different threads.
362360 _trackEventTask = _trackEventTask == null
363- ? Task . Run ( ( ) => TrackEventTask ( eventName , properties , copilotAgentSessionId ) )
364- : _trackEventTask . ContinueWith ( _ => TrackEventTask ( eventName , properties , copilotAgentSessionId ) ) ;
361+ ? Task . Run ( ( ) => TrackEventTask ( eventName , properties , contextProperties ) )
362+ : _trackEventTask . ContinueWith ( _ => TrackEventTask ( eventName , properties , contextProperties ) ) ;
365363 }
366364
367365 public void ThreadBlockingTrackEvent ( string eventName , IDictionary < string , string ? > ? properties )
@@ -371,7 +369,7 @@ public void ThreadBlockingTrackEvent(string eventName, IDictionary<string, strin
371369 return ;
372370 }
373371
374- TrackEventTask ( eventName , properties , GetCopilotAgentSessionId ( ) ) ;
372+ TrackEventTask ( eventName , properties , _commonPropertiesCollector . GetTelemetryContextProperties ( ) ) ;
375373 }
376374
377375 internal void WaitForPendingEvents ( )
@@ -382,29 +380,16 @@ internal void WaitForPendingEvents()
382380 internal static void WaitForInternalMicrosoftDetection ( ) =>
383381 s_internalMicrosoftTelemetry . WaitForCompletion ( ) ;
384382
385- private string GetCopilotAgentSessionId ( )
386- {
387- string ? sessionId = _environmentProvider . GetEnvironmentVariable ( EnvironmentVariableNames . COPILOT_AGENT_SESSION_ID ) ;
388- if ( string . IsNullOrEmpty ( sessionId ) )
389- {
390- return "not_present" ;
391- }
392-
393- return CopilotSessionUuidPattern ( ) . IsMatch ( sessionId ) ? sessionId : Sha256Hasher . Hash ( sessionId ) ;
394- }
395-
396- // Match Copilot's hash_string_if_not_uuid: canonical UUID versions 1-8 with RFC variant, or nil.
397- [ GeneratedRegex ( @"\A(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)\z" ,
398- RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ) ]
399- private static partial Regex CopilotSessionUuidPattern ( ) ;
400-
401- private static void TrackEventTask ( string eventName , IDictionary < string , string ? > ? properties , string copilotAgentSessionId )
383+ private static void TrackEventTask (
384+ string eventName ,
385+ IDictionary < string , string ? > ? properties ,
386+ IReadOnlyDictionary < string , string ? > contextProperties )
402387 {
403388 try
404389 {
405390 properties ??= new Dictionary < string , string ? > ( ) ;
406391 properties . Add ( "event id" , Guid . NewGuid ( ) . ToString ( ) ) ;
407- var @event = new ActivityEvent ( $ "dotnet/cli/{ eventName } ", tags : MakeTags ( properties , copilotAgentSessionId ) ) ;
392+ var @event = new ActivityEvent ( $ "dotnet/cli/{ eventName } ", tags : MakeTags ( properties , contextProperties ) ) ;
408393 Activity . Current ? . AddEvent ( @event ) ;
409394 }
410395 catch ( Exception e )
@@ -413,17 +398,19 @@ private static void TrackEventTask(string eventName, IDictionary<string, string?
413398 }
414399 }
415400
416- private static ActivityTagsCollection MakeTags ( IDictionary < string , string ? > eventProperties , string copilotAgentSessionId )
401+ private static ActivityTagsCollection MakeTags (
402+ IDictionary < string , string ? > eventProperties ,
403+ IReadOnlyDictionary < string , string ? > contextProperties )
417404 {
418405 var common = s_commonProperties
406+ . Where ( p => ! contextProperties . ContainsKey ( p . Key ) )
419407 . Select ( p => new KeyValuePair < string , object ? > ( p . Key , p . Value ) ) ;
420408 var properties = eventProperties
421- . Where ( p => p . Value is not null && p . Key != CopilotAgentSessionIdPropertyName )
409+ . Where ( p => p . Value is not null && ! contextProperties . ContainsKey ( p . Key ) )
422410 . Select ( p => new KeyValuePair < string , object ? > ( p . Key , p . Value ) )
423411 . OrderBy ( p => p . Key ) ;
424- ActivityTagsCollection tags = [ .. common , .. properties ] ;
425- tags . Add ( CopilotAgentSessionIdPropertyName , copilotAgentSessionId ) ;
426-
427- return tags ;
412+ var context = contextProperties
413+ . Select ( p => new KeyValuePair < string , object ? > ( p . Key , p . Value ) ) ;
414+ return [ .. common , .. properties , .. context ] ;
428415 }
429416}
0 commit comments