Skip to content
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

Prevent managed messaging initialization if profiler is not loaded #5540

Merged
merged 3 commits into from
Oct 17, 2023
Merged
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
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Diagnostics.Monitoring.StartupHook.MonitorMessageDispatcher;
using Microsoft.Diagnostics.Tools.Monitor;
using Microsoft.Diagnostics.Tools.Monitor.HostingStartup;
using Microsoft.Diagnostics.Tools.Monitor.Profiler;
using Microsoft.Diagnostics.Tools.Monitor.StartupHook;
using System;
using System.IO;
Expand Down Expand Up @@ -33,8 +34,12 @@ public static void Initialize()

try
{
SharedInternals.MessageDispatcher = new MonitorMessageDispatcher(new ProfilerMessageSource());
ToolIdentifiers.EnableEnvVar(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.ManagedMessaging);
// Check that the profiler is loaded before establishing the dispatcher, which has a dependency on the existance of the profiler
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(ProfilerIdentifiers.NotifyOnlyProfiler.EnvironmentVariables.ProductVersion)))
{
SharedInternals.MessageDispatcher = new MonitorMessageDispatcher(new ProfilerMessageSource());
ToolIdentifiers.EnableEnvVar(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.ManagedMessaging);
}
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Diagnostics.Monitoring.TestCommon;
using Microsoft.Diagnostics.Monitoring.TestCommon.Runners;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tools.Monitor.HostingStartup;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Diagnostics.Monitoring.Tool.UnitTests
{
[TargetFrameworkMonikerTrait(TargetFrameworkMonikerExtensions.CurrentTargetFrameworkMoniker)]
public sealed class StartupHookTests
{
private ITestOutputHelper _outputHelper;

public StartupHookTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}

// It appears that the profiler isn't loading on musl libc distros for this tests.
[ConditionalTheory(typeof(TestConditions), nameof(TestConditions.IsNotAlpine))]
jander-msft marked this conversation as resolved.
Show resolved Hide resolved
[MemberData(nameof(ProfilerHelper.GetNotifyOnlyArchitectureProfilerPath), MemberType = typeof(ProfilerHelper))]
public async Task StartupHook_WithProfiler_HasManagedMessaging(Architecture architecture, string profilerPath, ProfilerVariant variant)
{
Assert.Equal(ProfilerVariant.NotifyOnly, variant);

await using AppRunner runner = new(
_outputHelper,
Assembly.GetExecutingAssembly());
runner.Architecture = architecture;
runner.ScenarioName = TestAppScenarios.AsyncWait.Name;
runner.EnableMonitorStartupHook = true;

// Environment variables necessary for running the profiler + enable all logging to stderr
runner.Environment.Add(ProfilerHelper.ClrEnvVarEnableNotificationProfilers, ProfilerHelper.ClrEnvVarEnabledValue);
runner.Environment.Add(ProfilerHelper.ClrEnvVarEnableProfiling, ProfilerHelper.ClrEnvVarEnabledValue);
runner.Environment.Add(ProfilerHelper.ClrEnvVarProfiler, ProfilerIdentifiers.NotifyOnlyProfiler.Clsid.StringWithBraces);
runner.Environment.Add(ProfilerHelper.ClrEnvVarProfilerPath, profilerPath);
runner.Environment.Add(ProfilerIdentifiers.EnvironmentVariables.RuntimeInstanceId, Guid.NewGuid().ToString("D"));
runner.Environment.Add(ProfilerIdentifiers.EnvironmentVariables.StdErrLogger_Level, LogLevel.Trace.ToString("G"));

await runner.ExecuteAsync(async () =>
{
DiagnosticsClient client = new(await runner.ProcessIdTask);

Dictionary<string, string> env = client.GetProcessEnvironment();
Assert.True(env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.StartupHook, out string startupHookAvailableValue));
Assert.True(ToolIdentifiers.IsEnvVarValueEnabled(startupHookAvailableValue));
Assert.True(env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.ManagedMessaging, out string managedMessagingAvailableValue));
Assert.True(ToolIdentifiers.IsEnvVarValueEnabled(startupHookAvailableValue));

await runner.SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue);
});
}

[Fact]
public async Task StartupHook_WithoutProfiler_NoManagedMessaging()
{
await using AppRunner runner = new(
_outputHelper,
Assembly.GetExecutingAssembly());
runner.ScenarioName = TestAppScenarios.AsyncWait.Name;
runner.EnableMonitorStartupHook = true;

await runner.ExecuteAsync(async () =>
{
DiagnosticsClient client = new(await runner.ProcessIdTask);

Dictionary<string, string> env = client.GetProcessEnvironment();
Assert.True(env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.StartupHook, out string startupHookAvailableValue));
Assert.True(ToolIdentifiers.IsEnvVarValueEnabled(startupHookAvailableValue));
Assert.False(env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.ManagedMessaging, out _));

await runner.SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue);
});
}
}
}