Skip to content

Commit

Permalink
Return HTTP 400 for disabled features (#5527)
Browse files Browse the repository at this point in the history
  • Loading branch information
jander-msft authored Oct 13, 2023
1 parent ca1f597 commit bb6e92b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ namespace Microsoft.Diagnostics.Monitoring.WebApi.Controllers
{
internal static class ControllerExtensions
{
public static ActionResult FeatureNotEnabled(this ControllerBase controller, string featureName)
{
return new BadRequestObjectResult(new ProblemDetails()
{
Detail = string.Format(Strings.Message_FeatureNotEnabled, featureName),
Status = StatusCodes.Status400BadRequest
});
}

public static ActionResult NotAcceptable(this ControllerBase controller)
{
return new StatusCodeResult((int)HttpStatusCode.NotAcceptable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public async Task<ActionResult> CaptureParameters(
{
if (!_parameterCapturingOptions.Value.GetEnabled())
{
return NotFound();
return this.FeatureNotEnabled(Strings.FeatureName_ParameterCapturing);
}

ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);
Expand Down Expand Up @@ -617,7 +617,7 @@ public Task<ActionResult> CaptureStacks(
{
if (!_callStacksOptions.Value.GetEnabled())
{
return Task.FromResult<ActionResult>(NotFound());
return Task.FromResult<ActionResult>(this.FeatureNotEnabled(Strings.FeatureName_CallStacks));
}

ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Diagnostics.Monitoring.Options;
using Microsoft.Diagnostics.Monitoring.WebApi.Exceptions;
using Microsoft.Diagnostics.Monitoring.WebApi.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -64,7 +63,7 @@ public Task<ActionResult> GetExceptions(
{
if (!_options.Value.GetEnabled())
{
return Task.FromResult<ActionResult>(NotFound());
return Task.FromResult<ActionResult>(this.FeatureNotEnabled(Strings.FeatureName_Exceptions));
}

ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);
Expand Down
36 changes: 36 additions & 0 deletions src/Microsoft.Diagnostics.Monitoring.WebApi/Strings.Designer.cs

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

12 changes: 12 additions & 0 deletions src/Microsoft.Diagnostics.Monitoring.WebApi/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@
<value>Value must be of type string.</value>
<comment>Gets a string similar to "Value must be of type string.".</comment>
</data>
<data name="FeatureName_CallStacks" xml:space="preserve">
<value>Call Stacks</value>
</data>
<data name="FeatureName_Exceptions" xml:space="preserve">
<value>Exceptions</value>
</data>
<data name="FeatureName_ParameterCapturing" xml:space="preserve">
<value>Parameter Capturing</value>
</data>
<data name="LogFormatString_CounterEndedPayload" xml:space="preserve">
<value>The counter {0} ended and is no longer receiving metrics.</value>
</data>
Expand Down Expand Up @@ -314,6 +323,9 @@
<data name="Message_CollectionRuleStateReason_Throttled" xml:space="preserve">
<value>This collection rule is temporarily throttled because the ActionCountLimit has been reached within the ActionCountSlidingWindowDuration.</value>
</data>
<data name="Message_FeatureNotEnabled" xml:space="preserve">
<value>The '{0}' feature is not enabled.</value>
</data>
<data name="Message_GeneratedInProcessArtifact" xml:space="preserve">
<value>Finished generating in-process artifact</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ public Task TestInProcessFeaturesEnabledCallStacksDisabled(Architecture targetAr
{
int processId = await runner.ProcessIdTask;

ApiStatusCodeException ex = await Assert.ThrowsAsync<ApiStatusCodeException>(() => client.CaptureStacksAsync(processId, WebApi.StackFormat.Json));
Assert.Equal(HttpStatusCode.NotFound, ex.StatusCode);
ValidationProblemDetailsException ex = await Assert.ThrowsAsync<ValidationProblemDetailsException>(() => client.CaptureStacksAsync(processId, WebApi.StackFormat.Json));
Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode);

await runner.SendCommandAsync(TestAppScenarios.Stacks.Commands.Continue);
},
Expand Down Expand Up @@ -279,8 +279,8 @@ public Task TestInProcessFeaturesDisabledCallStacksEnabled(Architecture targetAr
{
int processId = await runner.ProcessIdTask;

ApiStatusCodeException ex = await Assert.ThrowsAsync<ApiStatusCodeException>(() => client.CaptureStacksAsync(processId, WebApi.StackFormat.Json));
Assert.Equal(HttpStatusCode.NotFound, ex.StatusCode);
ValidationProblemDetailsException ex = await Assert.ThrowsAsync<ValidationProblemDetailsException>(() => client.CaptureStacksAsync(processId, WebApi.StackFormat.Json));
Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode);

await runner.SendCommandAsync(TestAppScenarios.Stacks.Commands.Continue);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Log()
{
if (_parameterCapturingOptions.GetEnabled())
{
_logger.ExperimentalFeatureEnabled(Strings.FeatureName_ParameterCapturing);
_logger.ExperimentalFeatureEnabled(Microsoft.Diagnostics.Monitoring.WebApi.Strings.FeatureName_ParameterCapturing);
}

// Experimental features should log a warning when they are activated e.g.
Expand Down
27 changes: 0 additions & 27 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.

9 changes: 0 additions & 9 deletions src/Tools/dotnet-monitor/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,6 @@
<value>'{0}' is not a known trigger type.</value>
<comment>{0} = The type of trigger that is unknown.</comment>
</data>
<data name="FeatureName_CallStacks" xml:space="preserve">
<value>Call Stacks</value>
</data>
<data name="FeatureName_Exceptions" xml:space="preserve">
<value>Exceptions</value>
</data>
<data name="HelpDescription_CommandCollect" xml:space="preserve">
<value>Monitor logs and metrics in a .NET application send the results to a chosen destination.</value>
<comment>Gets the string to display in help that explains what the 'collect' command does.</comment>
Expand Down Expand Up @@ -913,9 +907,6 @@
0. providerName: The name of the provider that failed validation.
1. errorMessage: The validation failure message</comment>
</data>
<data name="FeatureName_ParameterCapturing" xml:space="preserve">
<value>Parameter Capturing</value>
</data>
<data name="ErrorMessage_UnableToFindHostingStartupAssembly" xml:space="preserve">
<value>Unable to find hosting startup assembly at determined path.</value>
</data>
Expand Down

0 comments on commit bb6e92b

Please sign in to comment.