Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.26k stars 4.6k forks source link

[BUG] UnsupportedRequestContent error response when using Azure.ResourceManager.Monitor to create AutoscaleSettingResource #43948

Closed bhanuprakash-1 closed 4 months ago

bhanuprakash-1 commented 4 months ago

Library name and version

Azure.ResourceManager.Monitor 1.3.1, Azure.ResourceManager 1.11.1, Azure.Identity 1.11.2

Describe the bug

I'm trying to create a AutoScaleSetting for my already existing App Service plan using the .NET SDKs Azure.ResourceManager.Monitor and Azure.ResourceManager. I was able to create other resources like App service, Web App etc in the same Resource group successfully. I'm using the DefaultAzureCredential() and my own identity(I'm logged into visual studio, where this code is running as a console app) and I'm also owner of the subscription and Resource group and App service plan, for which I'm trying to create the AutoScale setting.

But I'm getting exception and exception details are: the 400 error response is not clear on what exactly is the error.

Below is the error response:

Request content is not well formed or supported.
Status: 400 (Bad Request)
ErrorCode: UnsupportedRequestContent

Content:
{"code":"UnsupportedRequestContent","message":"Request content is not well formed or supported."}

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: <null>, Headers: {   Cache-Control: no-cache   Pragma: no-cache   Strict-Transport-Security: max-age=31536000; includeSubDomains   x-ms-request-id: 00000000-0000-0000-0000-000000000000   x-ms-ratelimit-remaining-subscription-writes: 1199   x-ms-correlation-request-id: 0498c1ee-52ff-4dc2-b304-7c22835f916e   x-ms-routing-request-id: CENTRALINDIA:20240508T182214Z:0498c1ee-52ff-4dc2-b304-7c22835f916e   X-Content-Type-Options: nosniff   X-Cache: CONFIG_NOCACHE   X-MSEdge-Ref: Ref A: C29CF5F458C94A8CB637104186C06A56 Ref B: MAA201060515019 Ref C: 2024-05-08T18:22:12Z   Date: Wed, 08 May 2024 18:22:13 GMT }}

My code which I'm using to configure Autoscale setting resource is:

// Create Autoscale setting
            ResourceIdentifier resourceIdentifierAppSvcPlan = appServicePlanLro.Value.Id;
            MonitorScaleCapacity scaleCapacity = new MonitorScaleCapacity(minimum: 3, maximum: 30, @default: 3);

            // cpu scale up logic
            MetricTrigger metricTriggerCpuScaleUp = new MetricTrigger(
                metricName: "CpuPercentage",
                metricResourceId: resourceIdentifierAppSvcPlan,
                timeGrain: TimeSpan.FromMinutes(1),
                statistic: MetricStatisticType.Average,
                timeWindow: TimeSpan.FromMinutes(1),
                timeAggregation: MetricTriggerTimeAggregationType.Average,
                @operator: MetricTriggerComparisonOperation.GreaterThan,
                threshold: 50);
            metricTriggerCpuScaleUp.MetricNamespace = "microsoft.web/serverfarms";
            metricTriggerCpuScaleUp.IsDividedPerInstance = false;

            MonitorScaleAction monitorScaleActionScaleUp = new MonitorScaleAction(
                MonitorScaleDirection.Increase,
                MonitorScaleType.PercentChangeCount,
                cooldown: TimeSpan.FromMinutes(1));
            monitorScaleActionScaleUp.Value = "100";

            // cpu scale down logic
            // scale down when cpu usage less than 20%
            MetricTrigger metricTriggerCpuScaleDown = new MetricTrigger(
                metricName: "CpuPercentage",
                metricResourceId: resourceIdentifierAppSvcPlan,
                timeGrain: TimeSpan.FromMinutes(1),
                statistic: MetricStatisticType.Average,
                timeWindow: TimeSpan.FromMinutes(5),
                timeAggregation: MetricTriggerTimeAggregationType.Average,
                @operator: MetricTriggerComparisonOperation.LessThan,
                threshold: 20);
            metricTriggerCpuScaleDown.MetricNamespace = "microsoft.web/serverfarms";
            metricTriggerCpuScaleDown.IsDividedPerInstance = false;

            MonitorScaleAction monitorScaleActionScaleDown = new MonitorScaleAction(
                MonitorScaleDirection.Decrease,
                MonitorScaleType.ChangeCount,
                cooldown: TimeSpan.FromMinutes(5));
            monitorScaleActionScaleDown.Value = "1";

            AutoscaleRule autoscaleRuleCpuScaleUp = new AutoscaleRule(metricTriggerCpuScaleUp, monitorScaleActionScaleUp);
            AutoscaleRule autoscaleRuleCpuScaleDown = new AutoscaleRule(metricTriggerCpuScaleDown, monitorScaleActionScaleDown);
            IEnumerable<AutoscaleRule> autoscaleRules = new List<AutoscaleRule> { autoscaleRuleCpuScaleUp, autoscaleRuleCpuScaleDown };

            AutoscaleProfile autoscaleProfile = new AutoscaleProfile("bhanu sdk app scale profile", scaleCapacity, autoscaleRules);
            IEnumerable<AutoscaleProfile> autoscaleProfiles = new List<AutoscaleProfile> { autoscaleProfile };
            AutoscaleSettingData autoscaleSettingData = new AutoscaleSettingData(AzureLocation.WestUS2, autoscaleProfiles);

            Console.WriteLine($"Autoscale setting before creation: {JsonConvert.SerializeObject(autoscaleSettingData)}\n");
            Console.ReadLine();

            ArmOperation<AutoscaleSettingResource> autoscaleSettingLro = await resourceGroup.GetAutoscaleSettings().CreateOrUpdateAsync(
                Azure.WaitUntil.Completed,
                "bhanu auto scale setting sdk",
                autoscaleSettingData).ConfigureAwait(false);

Expected behavior

400 error response should have a clear error message. Or the exception thrown by the .NET SDK should clear out what the user error is.

Actual behavior

Added exception message and x-ms-correlation id above.

Reproduction Steps

Have added code above. Use the code to repro.

Environment

OS Windows 11, Visual studio Enterprise 2022 (64-bit), version 17.9.6

jsquire commented 4 months ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

melina5656 commented 4 months ago

Hi @bhanuprakash-1, The reason for your issue is that a property named TargetResourceId was missing, which exists in AutoscaleSettingData. You can refer to the following code snippet:

AutoscaleSettingData autoscaleSettingData = new AutoscaleSettingData(AzureLocation.WestUS2, autoscaleProfiles)
{
    TargetResourceId = appServicePlan.Data.Id,
};

Additionally, there are some minor issues in your code.

MonitorScaleCapacity scaleCapacity = new MonitorScaleCapacity(minimum: 3, maximum: 30, @default: 3);

The range of values for minimum and maximum is generally 1 to 10.

monitorScaleActionScaleUp.Value = "100";

This an invalid scale action value. You should provide a scale value less than or equal to the difference between the capacity maximum and the capacity minimum.  Thank you for your feedback.