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

[Parameter Capturing] Add more helpful messages when feature unavailable #5200

Merged
merged 6 commits into from
Aug 28, 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 @@ -10,6 +10,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -49,35 +50,51 @@ public static bool IsEndpointRuntimeSupported(IEndpointInfo endpointInfo)
return endpointInfo.RuntimeVersion != null && endpointInfo.RuntimeVersion.Major >= 7;
}

private async Task<bool> CanEndpointProcessRequestsAsync(CancellationToken token)
private async Task EnsureEndpointCanProcessRequestsAsync(CancellationToken token)
{
static Exception getNotAvailableException(string reason)
{
return new MonitoringException(string.Format(
CultureInfo.InvariantCulture,
Strings.ErrorMessage_ParameterCapturingNotAvailable,
reason));
}

if (!IsEndpointRuntimeSupported(_endpointInfo))
{
throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_UnsupportedRuntime);
}

DiagnosticsClient client = new(_endpointInfo.Endpoint);

IDictionary<string, string> env = await client.GetProcessEnvironmentAsync(token);

const string PreventHostingStartupEnvName = "ASPNETCORE_PREVENTHOSTINGSTARTUP";
if (env.TryGetValue(PreventHostingStartupEnvName, out string preventHostingStartupEnvValue) &&
ToolIdentifiers.IsEnvVarValueEnabled(preventHostingStartupEnvValue))
{
throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_PreventedHostingStartup);
}

if (!env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.ManagedMessaging, out string isManagedMessagingAvailable) ||
!env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.HostingStartup, out string isHostingStartupAvailable))
!ToolIdentifiers.IsEnvVarValueEnabled(isManagedMessagingAvailable))
{
return false;
throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_ManagedMessagingDidNotLoad);
}

return ToolIdentifiers.IsEnvVarValueEnabled(isManagedMessagingAvailable) && ToolIdentifiers.IsEnvVarValueEnabled(isHostingStartupAvailable);
if (!env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.HostingStartup, out string isHostingStartupAvailable) ||
!ToolIdentifiers.IsEnvVarValueEnabled(isHostingStartupAvailable))
{
throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_HostingStartupDidNotLoad);
}
}

public async Task ExecuteAsync(CancellationToken token)
{
try
{
if (!IsEndpointRuntimeSupported(_endpointInfo))
{
throw new MonitoringException(Strings.ErrorMessage_ParameterCapturingRequiresAtLeastNet7);
}

// Check if the endpoint is capable of responding to our requests
if (!await CanEndpointProcessRequestsAsync(token))
{
throw new MonitoringException(Strings.ErrorMessage_ParameterCapturingNotAvailable);
}
await EnsureEndpointCanProcessRequestsAsync(token);

EventParameterCapturingPipelineSettings settings = new()
{
Expand Down
47 changes: 37 additions & 10 deletions src/Tools/dotnet-monitor/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions src/Tools/dotnet-monitor/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -942,9 +942,21 @@
<value>The shared file '{0}' is different from source file '{1}'.</value>
</data>
<data name="ErrorMessage_ParameterCapturingNotAvailable" xml:space="preserve">
<value>Parameter capturing is not available in the process.</value>
<value>Parameter capturing is not available in the process. {0}</value>
<comment>Gets the format string for parameter capturing not being available
1 Format Parameters:
0. reason: The reason why the feature is unavailable.</comment>
</data>
<data name="ErrorMessage_ParameterCapturingRequiresAtLeastNet7" xml:space="preserve">
<value>Parameter capturing is not available in the process, the process needs to be using .NET 7 or newer.</value>
<data name="ParameterCapturingNotAvailable_Reason_PreventedHostingStartup" xml:space="preserve">
<value>The process has prevented hosting startup assemblies from loading using the ASPNETCORE_PREVENTHOSTINGSTARTUP environment variable.</value>
</data>
<data name="ParameterCapturingNotAvailable_Reason_HostingStartupDidNotLoad" xml:space="preserve">
<value>This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has successfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading.</value>
</data>
<data name="ParameterCapturingNotAvailable_Reason_ManagedMessagingDidNotLoad" xml:space="preserve">
<value>An internal error occurred that has prevented communication with the process.</value>
</data>
<data name="ParameterCapturingNotAvailable_Reason_UnsupportedRuntime" xml:space="preserve">
<value>The process needs to be using .NET 7 or newer.</value>
</data>
</root>