Skip to content

Add a new interface to enable ConfigManager to handle various Events #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ----------------------------------------------------------------------------------
//
// 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;

namespace Microsoft.Azure.Commands.Common.Authentication.Config
{
public interface IConfigManagerWithEvents: IConfigManager
{
event EventHandler<ConfigEventArgs> ConfigRead;
event EventHandler<ConfigEventArgs> ConfigUpdated;
event EventHandler<ConfigEventArgs> ConfigCleared;
}
}
34 changes: 34 additions & 0 deletions src/Authentication.Abstractions/Models/Config/ConfigEventArgs .cs
Original file line number Diff line number Diff line change
@@ -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<string, string> ExtendedProperties { get; } = new Dictionary<string, string>();

public ConfigEventArgs(string configKey, string configValue)
{
ConfigKey = configKey;
ConfigValue = configValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
24 changes: 24 additions & 0 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -772,6 +774,28 @@ protected virtual void InitializeQosEvent()
}

_qosEvent.SanitizerInfo = new SanitizerTelemetry(OutputSanitizer?.RequireSecretsDetection == true);

AttachConfigReadHandlerForTelemetry();
}

/// <summary>
/// Attach config read event handler to add config telemetry
/// </summary>
private void AttachConfigReadHandlerForTelemetry()
{
AzureSession.Instance.TryGetComponent<IConfigManager>(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()
Expand Down