Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.26k stars 3.86k forks source link

New-AzMetricAlertRuleV2DimensionSelection - ValuesToExclude Unusable #22256

Closed BSmeeton closed 1 year ago

BSmeeton commented 1 year ago

Description

When trying to create dimensions to use in the creation of metric alerts (New-AzMetricAlertRuleV2Criteria) it appears that the -ValuesToInclude parameter is mandatory. This is causing issues when wanting to use the ValuesToExclude parameter as the two conflict and cannot be used in parallel for the same dimension.

Issue script & Debug output

$DebugPreference='Continue'
$dim1 = New-AzMetricAlertRuleV2DimensionSelection -DimensionName "healthStatus" -ValuesToExclude "Healthy" -ValuesToInclude "Persistent Unhealthy"
$criteria = New-AzMetricAlertRuleV2Criteria -MetricName "BackupHealthEvent"  -TimeAggregation Count -Operator GreaterThan -Threshold 0 -DimensionSelection $dim1
Add-AzMetricAlertRuleV2 -Name "Test-Metric1" -ResourceGroupName "rg" -WindowSize 00:05:00 -Frequency 00:05:00 -TargetResourceId "id" -Severity 4 -Condition $criteria -ActionGroupId "id"

##Removed ID Values due to security

DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
PUT

Absolute Uri:
https://management.azure.com/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Insights/metricAlerts/Test-Metric1?api-version=2018-03-01

Headers:
x-ms-client-request-id        : b1e915f2-d68f-4f43-9566-52f254020491
accept-language               : en-US

Body:
{
  "properties": {
    "description": "This new Metric alert rule was created from Powershell version: 4.4.1",
    "severity": 4,
    "enabled": true,
    "scopes": [
      "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.RecoveryServices/vaults/xxx"
    ],
    "evaluationFrequency": "PT5M",
    "windowSize": "PT5M",
    "criteria": {
      "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
      "allOf": [
        {
          "criterionType": "StaticThresholdCriterion",
          "operator": "GreaterThan",
          "threshold": 0.0,
          "name": "metric1",
          "metricName": "BackupHealthEvent",
          "timeAggregation": "Count",
          "dimensions": [
            {
              "name": "healthStatus",
              "operator": "Include",
              "values": [
                "Persistent Unhealthy"
              ]
            },
            {
              "name": "healthStatus",
              "operator": "Exclude",
              "values": [
                "Healthy"
              ]
            }
          ],
          "skipMetricValidation": false
        }
      ]
    },
    "autoMitigate": true,
    "actions": [
      {
        "actionGroupId": "/subscriptions/xxx/resourceGroups/xxx/providers/microsoft.insights/actionGroups/Test"
      }
    ]
  },
  "location": "global"
}

DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
BadRequest

Headers:
Pragma                        : no-cache
x-ms-request-id               : b9eae791-7d16-43f5-87a6-444abd8452c7,b9eae791-7d16-43f5-87a6-444abd8452c7
api-supported-versions        : 2017-09-01-preview, 2018-03-01
Strict-Transport-Security     : max-age=31536000; includeSubDomains
Arr-Disable-Session-Affinity  : true
X-Content-Type-Options        : nosniff
x-ms-ratelimit-remaining-subscription-resource-requests: 299
x-ms-correlation-request-id   : b9eae791-7d16-43f5-87a6-444abd8452c7
x-ms-routing-request-id       : WESTEUROPE:20230705T134352Z:b9eae791-7d16-43f5-87a6-444abd8452c7
Cache-Control                 : no-cache
Date                          : Wed, 05 Jul 2023 13:43:51 GMT
Server                        : Microsoft-IIS/10.0
X-AspNet-Version              : 4.0.30319
X-Powered-By                  : ASP.NET

Body:
{
  "code": "BadRequest",
  "message": "A dimension with the same name appears more than once in the same condition. Activity ID: b9eae791-7d16-43f5-87a6-444abd8452c7."
}

DEBUG: 2:43:52 PM - [ConfigManager] Got nothing from [EnableDataCollection], Module = [], Cmdlet = []. Returning default value [True].
Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException, Message: A dimension with the same name appears more than once in the same condition. Activity ID: b9eae791-7d16-43f5-87a6-444abd8452c7., Code: BadRequest, Status code:BadRequest, Reason
phrase: BadRequest

Environment data

Name                           Value
----                           -----
PSVersion                      5.1.19041.2673
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.2673
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module versions

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.12.1     Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault...}
Script     1.7.3      Az.Automation                       {Export-AzAutomationDscConfiguration, Export-AzAutomationDscNodeReportContent, Export-AzAutomationRunbook, Get-AzAutomationAccount...}
Script     4.4.1      Az.Monitor                          {Add-AzLogProfile, Add-AzMetricAlertRule, Add-AzMetricAlertRuleV2, Add-AzWebtestAlertRule...}
Script     5.6.0      Az.Resources                        {Export-AzResourceGroup, Export-AzTemplateSpec, Get-AzDenyAssignment, Get-AzDeployment...}

Error output

Operation returned an invalid status code 'BadRequest'
StackTrace     :    at Microsoft.Azure.Management.Monitor.MetricAlertsOperations.<CreateOrUpdateWithHttpMessagesAsync>d__8.MoveNext()
                 --- End of stack trace from previous location where exception was thrown ---
                    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                    at Microsoft.Azure.Management.Monitor.MetricAlertsOperationsExtensions.<CreateOrUpdateAsync>d__7.MoveNext()
isra-fel commented 1 year ago

@VeryEarly could you take a look at this?

VeryEarly commented 1 year ago

Hi @BSmeeton ,

Thanks for reporting, fixed New-AzMetricAlertRuleV2DimensionSelection to allow exclude or include. Will be available on 2023-09-05 PST. However for your case, you might want to create two conditions, one with dimension to include the other with dimension to exclude (Because for a given condition, one dimension name can only be related to one dimension).

BSmeeton commented 1 year ago

Thank you for addressing the issue 👍🏻