From a03fea2c42bf6499f3e56a5ef54783e877a4a18d Mon Sep 17 00:00:00 2001 From: Beisi Zhou Date: Tue, 25 Jun 2024 17:30:29 +0800 Subject: [PATCH 1/3] add IConfigManagerWithEvents --- .../Authentication.Abstractions.csproj | 1 - .../Interfaces/IConfigManagerWithEvents.cs | 14 ++++++++++ .../Models/ConfigEventArgs .cs | 21 +++++++++++++++ .../Models/ConfigReadEventArgs.cs | 20 ++++++++++++++ src/Common/AzurePSCmdlet.cs | 26 ++++++++++++++++++- 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs create mode 100644 src/Authentication.Abstractions/Models/ConfigEventArgs .cs create mode 100644 src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs diff --git a/src/Authentication.Abstractions/Authentication.Abstractions.csproj b/src/Authentication.Abstractions/Authentication.Abstractions.csproj index c0fd66e6b0..5f2f89058a 100644 --- a/src/Authentication.Abstractions/Authentication.Abstractions.csproj +++ b/src/Authentication.Abstractions/Authentication.Abstractions.csproj @@ -54,7 +54,6 @@ - \ No newline at end of file diff --git a/src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs b/src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs new file mode 100644 index 0000000000..0c17b293d0 --- /dev/null +++ b/src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs @@ -0,0 +1,14 @@ +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models; +using Microsoft.Azure.PowerShell.Common.Config; + +using System; + +namespace Microsoft.Azure.Commands.Common.Authentication.Config +{ + public interface IConfigManagerWithEvents: IConfigManager + { + event EventHandler ConfigRead; + event EventHandler ConfigUpdated; + event EventHandler ConfigCleared; + } +} diff --git a/src/Authentication.Abstractions/Models/ConfigEventArgs .cs b/src/Authentication.Abstractions/Models/ConfigEventArgs .cs new file mode 100644 index 0000000000..10359c6b16 --- /dev/null +++ b/src/Authentication.Abstractions/Models/ConfigEventArgs .cs @@ -0,0 +1,21 @@ +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using System; +using System.Collections.Generic; + +namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models +{ + public class ConfigEventArgs : EventArgs, IExtensibleModel + { + public string ConfigKey { get; } + + public string ConfigValue { get; } + + public IDictionary ExtendedProperties { get; } = new Dictionary(); + + public ConfigEventArgs(string configKey, string configValue) + { + ConfigKey = configKey; + ConfigValue = configValue; + } + } +} diff --git a/src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs b/src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs new file mode 100644 index 0000000000..f6f693e3bf --- /dev/null +++ b/src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models +{ + public class ConfigReadEventArgs : ConfigEventArgs + { + public string ConfigTelemetryKey { get; } + + public ConfigReadEventArgs(string configKey, string configValue) : this(configKey, configKey, configValue) + { + } + + public ConfigReadEventArgs(string configKey, string configTelemetryKey, string configValue) : base(configKey, configValue) + { + ConfigTelemetryKey = configTelemetryKey; + } + } +} diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index 55a827670a..a80191b33c 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -14,6 +14,8 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models; +using Microsoft.Azure.Commands.Common.Authentication.Config; using Microsoft.Azure.PowerShell.Common.Config; using Microsoft.Azure.PowerShell.Common.Share.Survey; using Microsoft.Azure.PowerShell.Common.UpgradeNotification; @@ -522,6 +524,25 @@ protected void WriteSurvey() } } + + private void AddTelemetryForConfig() + { + // attach config read event handler to add config telemetry + AzureSession.Instance.TryGetComponent(nameof(IConfigManager), out var configManager); + if (configManager is IConfigManagerWithEvents cm) + { + cm.ConfigRead += OnConfigReaded; + } + } + + private void OnConfigReaded(object sender, ConfigEventArgs args) + { + if (!_qosEvent.ConfigMetrics.ContainsKey(args.ConfigKey) && args is ConfigReadEventArgs readEventArgs) + { + _qosEvent.ConfigMetrics[readEventArgs.ConfigKey] = new ConfigMetrics(readEventArgs.ConfigTelemetryKey, readEventArgs.ConfigValue.ToString()); + } + } + protected new void ThrowTerminatingError(ErrorRecord errorRecord) { FlushDebugMessages(); @@ -546,7 +567,8 @@ protected void WriteSurvey() base.WriteObject(sendToPipeline, enumerateCollection); } - private void SanitizeOutput(object sendToPipeline) + + private void SanitizeOutput(object sendToPipeline) { if (OutputSanitizer?.RequireSecretsDetection == true) { @@ -772,6 +794,8 @@ protected virtual void InitializeQosEvent() } _qosEvent.SanitizerInfo = new SanitizerTelemetry(OutputSanitizer?.RequireSecretsDetection == true); + + AddTelemetryForConfig(); } private void RecordDebugMessages() From 3d48fc4a1b6f62f21dc97c71aa83c86dfa70dbd1 Mon Sep 17 00:00:00 2001 From: Beisi Zhou Date: Tue, 25 Jun 2024 17:48:46 +0800 Subject: [PATCH 2/3] refine codes --- .../Authentication.Abstractions.csproj | 1 + .../Interfaces/IConfigManagerWithEvents.cs | 16 ++++++- .../Models/{ => Config}/ClearConfigOptions.cs | 0 .../Models/{ => Config}/ConfigData.cs | 0 .../Models/{ => Config}/ConfigDefinition.cs | 0 .../Models/Config/ConfigEventArgs .cs | 34 ++++++++++++++ .../Models/{ => Config}/ConfigFilter.cs | 0 .../{ => Config}/ConfigKeysForCommon.cs | 0 .../Models/{ => Config}/ConfigMetrics.cs | 0 .../Models/Config/ConfigReadEventArgs.cs | 30 +++++++++++++ .../Models/{ => Config}/ConfigScope.cs | 0 .../{ => Config}/UpdateConfigOptions.cs | 0 .../Models/ConfigEventArgs .cs | 21 --------- .../Models/ConfigReadEventArgs.cs | 20 --------- src/Common/AzurePSCmdlet.cs | 44 +++++++++---------- 15 files changed, 102 insertions(+), 64 deletions(-) rename src/Authentication.Abstractions/Models/{ => Config}/ClearConfigOptions.cs (100%) rename src/Authentication.Abstractions/Models/{ => Config}/ConfigData.cs (100%) rename src/Authentication.Abstractions/Models/{ => Config}/ConfigDefinition.cs (100%) create mode 100644 src/Authentication.Abstractions/Models/Config/ConfigEventArgs .cs rename src/Authentication.Abstractions/Models/{ => Config}/ConfigFilter.cs (100%) rename src/Authentication.Abstractions/Models/{ => Config}/ConfigKeysForCommon.cs (100%) rename src/Authentication.Abstractions/Models/{ => Config}/ConfigMetrics.cs (100%) create mode 100644 src/Authentication.Abstractions/Models/Config/ConfigReadEventArgs.cs rename src/Authentication.Abstractions/Models/{ => Config}/ConfigScope.cs (100%) rename src/Authentication.Abstractions/Models/{ => Config}/UpdateConfigOptions.cs (100%) delete mode 100644 src/Authentication.Abstractions/Models/ConfigEventArgs .cs delete mode 100644 src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs diff --git a/src/Authentication.Abstractions/Authentication.Abstractions.csproj b/src/Authentication.Abstractions/Authentication.Abstractions.csproj index 5f2f89058a..a0a82bfeec 100644 --- a/src/Authentication.Abstractions/Authentication.Abstractions.csproj +++ b/src/Authentication.Abstractions/Authentication.Abstractions.csproj @@ -54,6 +54,7 @@ + \ No newline at end of file diff --git a/src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs b/src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs index 0c17b293d0..18fb5ddec8 100644 --- a/src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs +++ b/src/Authentication.Abstractions/Interfaces/IConfigManagerWithEvents.cs @@ -1,4 +1,18 @@ -using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models; +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------- + +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models; using Microsoft.Azure.PowerShell.Common.Config; using System; diff --git a/src/Authentication.Abstractions/Models/ClearConfigOptions.cs b/src/Authentication.Abstractions/Models/Config/ClearConfigOptions.cs similarity index 100% rename from src/Authentication.Abstractions/Models/ClearConfigOptions.cs rename to src/Authentication.Abstractions/Models/Config/ClearConfigOptions.cs diff --git a/src/Authentication.Abstractions/Models/ConfigData.cs b/src/Authentication.Abstractions/Models/Config/ConfigData.cs similarity index 100% rename from src/Authentication.Abstractions/Models/ConfigData.cs rename to src/Authentication.Abstractions/Models/Config/ConfigData.cs diff --git a/src/Authentication.Abstractions/Models/ConfigDefinition.cs b/src/Authentication.Abstractions/Models/Config/ConfigDefinition.cs similarity index 100% rename from src/Authentication.Abstractions/Models/ConfigDefinition.cs rename to src/Authentication.Abstractions/Models/Config/ConfigDefinition.cs diff --git a/src/Authentication.Abstractions/Models/Config/ConfigEventArgs .cs b/src/Authentication.Abstractions/Models/Config/ConfigEventArgs .cs new file mode 100644 index 0000000000..bc495065b4 --- /dev/null +++ b/src/Authentication.Abstractions/Models/Config/ConfigEventArgs .cs @@ -0,0 +1,34 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------- + +using System; +using System.Collections.Generic; + +namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models +{ + public class ConfigEventArgs : EventArgs, IExtensibleModel + { + public string ConfigKey { get; } + + public string ConfigValue { get; } + + public IDictionary ExtendedProperties { get; } = new Dictionary(); + + public ConfigEventArgs(string configKey, string configValue) + { + ConfigKey = configKey; + ConfigValue = configValue; + } + } +} diff --git a/src/Authentication.Abstractions/Models/ConfigFilter.cs b/src/Authentication.Abstractions/Models/Config/ConfigFilter.cs similarity index 100% rename from src/Authentication.Abstractions/Models/ConfigFilter.cs rename to src/Authentication.Abstractions/Models/Config/ConfigFilter.cs diff --git a/src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs b/src/Authentication.Abstractions/Models/Config/ConfigKeysForCommon.cs similarity index 100% rename from src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs rename to src/Authentication.Abstractions/Models/Config/ConfigKeysForCommon.cs diff --git a/src/Authentication.Abstractions/Models/ConfigMetrics.cs b/src/Authentication.Abstractions/Models/Config/ConfigMetrics.cs similarity index 100% rename from src/Authentication.Abstractions/Models/ConfigMetrics.cs rename to src/Authentication.Abstractions/Models/Config/ConfigMetrics.cs diff --git a/src/Authentication.Abstractions/Models/Config/ConfigReadEventArgs.cs b/src/Authentication.Abstractions/Models/Config/ConfigReadEventArgs.cs new file mode 100644 index 0000000000..1e7c25175e --- /dev/null +++ b/src/Authentication.Abstractions/Models/Config/ConfigReadEventArgs.cs @@ -0,0 +1,30 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------- + +namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models +{ + public class ConfigReadEventArgs : ConfigEventArgs + { + public string ConfigTelemetryKey { get; } + + public ConfigReadEventArgs(string configKey, string configValue) : this(configKey, configKey, configValue) + { + } + + public ConfigReadEventArgs(string configKey, string configTelemetryKey, string configValue) : base(configKey, configValue) + { + ConfigTelemetryKey = configTelemetryKey; + } + } +} diff --git a/src/Authentication.Abstractions/Models/ConfigScope.cs b/src/Authentication.Abstractions/Models/Config/ConfigScope.cs similarity index 100% rename from src/Authentication.Abstractions/Models/ConfigScope.cs rename to src/Authentication.Abstractions/Models/Config/ConfigScope.cs diff --git a/src/Authentication.Abstractions/Models/UpdateConfigOptions.cs b/src/Authentication.Abstractions/Models/Config/UpdateConfigOptions.cs similarity index 100% rename from src/Authentication.Abstractions/Models/UpdateConfigOptions.cs rename to src/Authentication.Abstractions/Models/Config/UpdateConfigOptions.cs diff --git a/src/Authentication.Abstractions/Models/ConfigEventArgs .cs b/src/Authentication.Abstractions/Models/ConfigEventArgs .cs deleted file mode 100644 index 10359c6b16..0000000000 --- a/src/Authentication.Abstractions/Models/ConfigEventArgs .cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.Azure.Commands.Common.Authentication.Abstractions; -using System; -using System.Collections.Generic; - -namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models -{ - public class ConfigEventArgs : EventArgs, IExtensibleModel - { - public string ConfigKey { get; } - - public string ConfigValue { get; } - - public IDictionary ExtendedProperties { get; } = new Dictionary(); - - public ConfigEventArgs(string configKey, string configValue) - { - ConfigKey = configKey; - ConfigValue = configValue; - } - } -} diff --git a/src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs b/src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs deleted file mode 100644 index f6f693e3bf..0000000000 --- a/src/Authentication.Abstractions/Models/ConfigReadEventArgs.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models -{ - public class ConfigReadEventArgs : ConfigEventArgs - { - public string ConfigTelemetryKey { get; } - - public ConfigReadEventArgs(string configKey, string configValue) : this(configKey, configKey, configValue) - { - } - - public ConfigReadEventArgs(string configKey, string configTelemetryKey, string configValue) : base(configKey, configValue) - { - ConfigTelemetryKey = configTelemetryKey; - } - } -} diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index a80191b33c..6644ca3f9c 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -524,25 +524,6 @@ protected void WriteSurvey() } } - - private void AddTelemetryForConfig() - { - // attach config read event handler to add config telemetry - AzureSession.Instance.TryGetComponent(nameof(IConfigManager), out var configManager); - if (configManager is IConfigManagerWithEvents cm) - { - cm.ConfigRead += OnConfigReaded; - } - } - - private void OnConfigReaded(object sender, ConfigEventArgs args) - { - if (!_qosEvent.ConfigMetrics.ContainsKey(args.ConfigKey) && args is ConfigReadEventArgs readEventArgs) - { - _qosEvent.ConfigMetrics[readEventArgs.ConfigKey] = new ConfigMetrics(readEventArgs.ConfigTelemetryKey, readEventArgs.ConfigValue.ToString()); - } - } - protected new void ThrowTerminatingError(ErrorRecord errorRecord) { FlushDebugMessages(); @@ -567,8 +548,7 @@ private void OnConfigReaded(object sender, ConfigEventArgs args) base.WriteObject(sendToPipeline, enumerateCollection); } - - private void SanitizeOutput(object sendToPipeline) + private void SanitizeOutput(object sendToPipeline) { if (OutputSanitizer?.RequireSecretsDetection == true) { @@ -795,7 +775,27 @@ protected virtual void InitializeQosEvent() _qosEvent.SanitizerInfo = new SanitizerTelemetry(OutputSanitizer?.RequireSecretsDetection == true); - AddTelemetryForConfig(); + AttachConfigReadHandlerForTelemetry(); + } + + /// + /// Attach config read event handler to add config telemetry + /// + private void AttachConfigReadHandlerForTelemetry() + { + AzureSession.Instance.TryGetComponent(nameof(IConfigManager), out var configManager); + if (configManager is IConfigManagerWithEvents cm) + { + cm.ConfigRead += OnConfigRead; + } + } + + private void OnConfigRead(object sender, ConfigEventArgs args) + { + if (!_qosEvent.ConfigMetrics.ContainsKey(args.ConfigKey) && args is ConfigReadEventArgs readEventArgs) + { + _qosEvent.ConfigMetrics[readEventArgs.ConfigKey] = new ConfigMetrics(readEventArgs.ConfigTelemetryKey, readEventArgs.ConfigValue.ToString()); + } } private void RecordDebugMessages() From e9d1df51f9d2bc8a5fc4d1e12c829906327bbfd0 Mon Sep 17 00:00:00 2001 From: Beisi Zhou Date: Tue, 25 Jun 2024 17:56:55 +0800 Subject: [PATCH 3/3] Update src/Authentication.Abstractions/Authentication.Abstractions.csproj --- .../Authentication.Abstractions.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authentication.Abstractions/Authentication.Abstractions.csproj b/src/Authentication.Abstractions/Authentication.Abstractions.csproj index a0a82bfeec..c0fd66e6b0 100644 --- a/src/Authentication.Abstractions/Authentication.Abstractions.csproj +++ b/src/Authentication.Abstractions/Authentication.Abstractions.csproj @@ -54,7 +54,7 @@ - + \ No newline at end of file