Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
29 changes: 25 additions & 4 deletions src/System.Management.Automation/utils/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;

Expand Down Expand Up @@ -61,6 +62,26 @@ internal enum TelemetryType
RemoteSessionOpen,
}

/// <summary>
/// Set up the telemetry initializer to mask the platform specific names.
/// </summary>
public class NameObscurerTelemetryInitializer : ITelemetryInitializer
{
// Report the platform name information as "na".
private const string _notavailable = "na";

/// <summary>
/// Initialize properties we are obscuring to "na".
/// </summary>
/// <param name="telemetry">The instance of our telemetry.</param>
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.Cloud.RoleName = _notavailable;
telemetry.Context.GetInternalContext().NodeName = _notavailable;
telemetry.Context.Cloud.RoleInstance = _notavailable;
}
}

/// <summary>
/// Send up telemetry for startup.
/// </summary>
Expand Down Expand Up @@ -112,17 +133,17 @@ static ApplicationInsightsTelemetry()
CanSendTelemetry = !GetEnvironmentVariableAsBool(name: _telemetryOptoutEnvVar, defaultValue: false);
if (CanSendTelemetry)
{
s_sessionId = Guid.NewGuid().ToString();
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = _psCoreTelemetryKey;

// Set this to true to reduce latency during development
configuration.TelemetryChannel.DeveloperMode = false;

// Be sure to obscure any information about the client node name.
configuration.TelemetryInitializers.Add(new NameObscurerTelemetryInitializer());

s_telemetryClient = new TelemetryClient(configuration);
// Be sure to obscure any information about the client node.
s_telemetryClient.Context.Cloud.RoleInstance = string.Empty;
s_telemetryClient.Context.GetInternalContext().NodeName = string.Empty;
s_sessionId = Guid.NewGuid().ToString();

// use a hashset when looking for module names, it should be quicker than a string comparison
s_knownModules = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
Expand Down