Skip to content

Commit

Permalink
[Parameter Capturing] Add more helpful messages when feature unavaila…
Browse files Browse the repository at this point in the history
…ble (#5200)
  • Loading branch information
schmittjoseph authored Aug 28, 2023
1 parent 479e901 commit f333144
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 26 deletions.
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>

0 comments on commit f333144

Please sign in to comment.