Search code examples
azurerestazure-monitoringazure-monitor

Creating Azure monitor alert rule using REST API


I am trying to create an Azure monitor alert rule using REST API.

Using this link to exercise the same: https://learn.microsoft.com/en-us/rest/api/monitor/alert-rules/create-or-update?view=rest-monitor-2016-03-01

Upon doing the same i am getting error like below:

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

Payload is as follows:

{
 "location": "westus",
 "properties": {
  "description": "Test Alert",
  "isEnabled": "true",
  "severity": 2,
  "evaluationFrequency": "PT1M",
  "windowSize": "PT5M",
  "condition": {
   "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
   "allOf": [
    {
     "name": "metric1",
     "metricNamespace": "Microsoft.CognitiveServices/accounts",
     "metricName": "TokenTransaction",
     "operator": "GreaterThan",
     "threshold": 50000,
     "timeAggregation": "Average",
     "dimensions": [
       {
         "name": "FeatureName",
         "operator": "Include",
         "values": [
           "gpt-4"
         ]
       }
     ]
    }
   ]
  },
  "actions": [],
  "scopes": [
  "/subscriptions/subid/resourceGroups/tgname/providers/Microsoft.CognitiveServices/accounts/openainame"
  ]
 }
}

Other details:

PUT https://management.azure.com/subscriptions/subid/resourcegroups/rgname/providers/Microsoft.Insights/alertrules/rulename?api-version=2016-03-01
Authorization: Bearer Token
Content-type: application/json

I dont find any microsoft documents to explain the right payload data, Any sample data to create Metric rule is really appreciated.


Solution

  • When I ran below API call with your request body in my environment, I too got same error:

    PUT https://management.azure.com/subscriptions/subid/resourcegroups/rgname/providers/Microsoft.Insights/alertrules/rulename?api-version=2016-03-01
    
    {
     "location": "westus",
     "properties": {
      "description": "Test Alert",
      "isEnabled": "true",
      "severity": 2,
      "evaluationFrequency": "PT1M",
      "windowSize": "PT5M",
      "condition": {
       "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
       "allOf": [
        {
         "name": "metric1",
         "metricNamespace": "Microsoft.CognitiveServices/accounts",
         "metricName": "TokenTransaction",
         "operator": "GreaterThan",
         "threshold": 50000,
         "timeAggregation": "Average",
         "dimensions": [
           {
             "name": "FeatureName",
             "operator": "Include",
             "values": [
               "gpt-4"
             ]
           }
         ]
        }
       ]
      },
      "actions": [],
      "scopes": [
      "/subscriptions/subid/resourceGroups/tgname/providers/Microsoft.CognitiveServices/accounts/openainame"
      ]
     }
    }
    

    Response:

    enter image description here

    To resolve the error, make use of below API call with modified request body like this:

    PUT https://management.azure.com/subscriptions/subId/resourcegroups/rgname/providers/Microsoft.Insights/metricAlerts/demorule2901?api-version=2018-03-01
    
    {
      "location": "global",
      "tags": {},
      "properties": {
        "description": "Test Alert",
        "severity": 2,
        "enabled": true,
        "scopes": [
          "/subscriptions/subId/resourceGroups/rgname/providers/Microsoft.CognitiveServices/accounts/demoOpenAI2901"
        ],
        "evaluationFrequency": "PT1M",
        "windowSize": "PT5M",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
          "allOf": [
            {
              "criterionType": "StaticThresholdCriterion",
              "name": "metric1",
              "metricName": "TokenTransaction",
              "dimensions": [
                    {
                    "name": "FeatureName",
                    "operator": "Include",
                    "values": [
                    "gpt-4"
                ]
                }
              ],
              "operator": "GreaterThan",
              "threshold": 50000,
              "timeAggregation": "Average"
            }
          ]
        },
        "actions": []
      }
    }
    

    Response:

    enter image description here

    To confirm that, I checked the same in Portal where alert rule created successfully for Azure OpenAI account scope like this:

    enter image description here

    Reference: Metric Alerts - Create Or Update - REST API (Azure Monitor) | Microsoft