diff --git a/src/Agent.Listener/Configuration/ConfigurationManager.cs b/src/Agent.Listener/Configuration/ConfigurationManager.cs index 58c8ae43ca..45dcf280f2 100644 --- a/src/Agent.Listener/Configuration/ConfigurationManager.cs +++ b/src/Agent.Listener/Configuration/ConfigurationManager.cs @@ -21,6 +21,7 @@ using System.Security.AccessControl; using System.Security.Principal; using Newtonsoft.Json; +using Microsoft.Win32; using Microsoft.VisualStudio.Services.Agent.Listener.Telemetry; namespace Microsoft.VisualStudio.Services.Agent.Listener.Configuration @@ -42,6 +43,9 @@ public sealed class ConfigurationManager : AgentService, IConfigurationManager private ILocationServer _locationServer; private ServerUtil _serverUtil; + private const string VsTelemetryRegPath = @"SOFTWARE\Microsoft\VisualStudio\Telemetry\PersistentPropertyBag\c57a9efce9b74de382d905a89852db71"; + private const string VsTelemetryRegKey = "IsPipelineAgent"; + public override void Initialize(IHostContext hostContext) { ArgUtil.NotNull(hostContext, nameof(hostContext)); @@ -421,6 +425,12 @@ public async Task ConfigureAsync(CommandSettings command) serviceControlManager.GenerateScripts(agentSettings); } + if(PlatformUtil.RunningOnWindows) + { + // add vstelemetry registrykey + this.AddVSTelemetryRegKey(); + } + try { var telemetryData = new Dictionary @@ -577,6 +587,12 @@ public async Task UnconfigureAsync(CommandSettings command) // delete agent runtime option _store.DeleteAgentRuntimeOptions(); + if(PlatformUtil.RunningOnWindows) + { + // delete vstelemetry registrykey + this.DeleteVSTelemetryRegKey(); + } + _store.DeleteSettings(); _term.WriteLine(StringUtil.Loc("Success") + currentAction); } @@ -855,6 +871,48 @@ private void WriteSection(string message) _term.WriteLine(); } + [SupportedOSPlatform("windows")] + private void AddVSTelemetryRegKey() + { + try + { + //create the VsTelemetryRegKey under currentuser/VsTelemetryRegPath and set value to true + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(VsTelemetryRegPath, writable: true)) + { + if (key != null && key.GetValue(VsTelemetryRegKey) == null) + { + key.SetValue(VsTelemetryRegKey, "s:true", RegistryValueKind.String); + } + } + } + catch (Exception) + { + // ignore failure as this is not critical to agent functionality + Trace.Info("Error while adding VSTelemetry regkey"); + } + } + + [SupportedOSPlatform("windows")] + private void DeleteVSTelemetryRegKey() + { + try + { + // delete the VsTelemetryRegKey under currentuser/VsTelemetryRegPath + using (RegistryKey key = Registry.CurrentUser.OpenSubKey(VsTelemetryRegPath, writable: true)) + { + if (key != null && key.GetValue(VsTelemetryRegKey) != null) + { + key.DeleteValue(VsTelemetryRegKey); + } + } + } + catch (Exception) + { + // ignore failure as this is not critical to agent functionality + Trace.Info("Error while deleting VSTelemetry regkey"); + } + } + [SupportedOSPlatform("windows")] private void CheckAgentRootDirectorySecure() {