Coder Perfect

Multiple Event Grid Topics with Azure Metric Alerts

Problem

I’m trying to monitor dead-letter events across multiple Event Grid Topics in an ecosystem, but I can’t seem to find a query in Azure Monitor that will output these types of events at scale for alert rule creation – I’m thinking my only option is to set metric alerts within the Event Grid Topics themselves rather than creating a single alert rule that monitors all EG topics. I’d like to do this with IaC, but I’m scared that the only way to do so is using a PowerShell script that loops through all of the potential Event Grid Topics within a subscription and performs a new metric alert command (so that each Event Grid Topic has its own alert).

Is it possible that I’m becoming insane? Any suggestions for a more efficient approach to this task?

Asked by Trevor Suter

Solution #1

We tried giving numerous event grid resourceIds to the -target resource scope flag in the ‘add-azmetricalertrulev2’ cmdlet, but it failed with the following error.

We tested this in our local setting and it works perfectly. The following statements are based on the analysis.

To establish an alert for each event grid topic in the subscription and to monitor Dead Letter Events, use the PowerShell script below. The following resources will be created by the script.

The Powershell script is as follows:

$rg="<RGGroup>" ## resource group to for creating action group & alert 

$eventgridlist=Get-AzEventGridTopic 
$eventgridid=$eventgridlist.PsTopicsList.ToArray().id
$windowSize = New-TimeSpan -Minutes 5
$frequency = New-TimeSpan -Minutes 1

$condition = New-AzMetricAlertRuleV2Criteria -MetricName "DeadLetteredCount" -TimeAggregation Total -Operator GreaterThan -Threshold 1

$email = New-AzActionGroupReceiver -Name "AGgroupactions" -EmailReceiver -EmailAddress "<emailid>"
Set-AzActionGroup -Name "EGTopic_actionGroup" -ResourceGroup $rg -Receiver $email -ShortName "egalert"
$name=1 

foreach( $i in $eventgridid){
Add-AzMetricAlertRuleV2 -Name "eventgridalert_$name" -ResourceGroupName $rg -WindowSize $windowSize -Frequency $frequency -TargetResourceScope $i -TargetResourceType "Microsoft.EventGrid/topics" -TargetResourceRegion " " -Description "This is description" -Severity 3 -ActionGroup $actionGroupId -Condition $condition 
$name++
}

For your convenience, I’ve included a screenshot of the Sample Output:

Answered by VenkateshDodda-MT

Post is based on https://stackoverflow.com/questions/71072796/applying-azure-metric-alerts-to-multiple-event-grid-topics