Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alert dependable on Action Group #1112

Open
OutOfScopeia opened this issue Jun 10, 2024 · 2 comments
Open

Alert dependable on Action Group #1112

OutOfScopeia opened this issue Jun 10, 2024 · 2 comments

Comments

@OutOfScopeia
Copy link
Contributor

The problem:
We encountered a case where our deployment failed because an alert (microsoft.insights/metricAlerts) that contains an add_action with an action group (defined in the same script, the same template) was being deployed before the action group existed.
I figured I needed to be explicit about deployment order, using the ARM template's dependsOn mechanism. There is already one add_linked_resource in the alert config, linking the appInsights instance, and I inserted another one, linking the Action Group by its Id. This is legal in Farmer (no compile errors/warnings), but it generates an invalid ARM template. On closer inspection of the generated template, the add_linked_resource adds the linked resource in 3 places:
the dependsOn part - which is exactly what I intended,
tags - this one's ok,
scopes - this is the one that makes the template illegal.

The error message when attempting to deploy this illegal template manually from azure portal is:
"Scopes property is invalid. Only single resource is allowed for criteria type SingleResourceMultipleMetricCriteria. If you want to create an alert on multiple resources, use MultipleResourceMultipleMetricCriteria odata.type."

Any thoughts on how to encode the order of deployment explicitly directly from Farmer? If there is a provision for this in Farmer already, I haven't found it.

@ninjarobot
Copy link
Collaborator

@OutOfScopeia do you have an example of the Farmer code that produces this? I assume Farmer produces the wrong type of metric criteria, but an example would really help.

@OutOfScopeia
Copy link
Contributor Author

open Farmer
open Farmer.Arm
open Farmer.Builders

let deployName = "alert-test-app19"
let actionGroupName = $"{deployName}-ag"

let law = logAnalytics {
    name $"{deployName}-law"
}

let ai = appInsights {
    name $"{deployName}-ai"
    log_analytics_workspace law
}

let web = webApp {
    name $"{deployName}"
    operating_system OS.Linux
    runtime_stack (DotNet "8.0")
    link_to_app_insights ai
}

let ag = actionGroup {
    name actionGroupName
    // 12 chars max and cannot be null or empty
    short_name (
        if actionGroupName.Length > 12
        then
            actionGroupName.Substring(0, 12)
        else
            actionGroupName
    )
    add_arm_role_receivers [ ArmRoleReceiver.Create(name = "CIT Monitoring Reader", armRole = Roles.MonitoringReader) ]
}

let aiId, agId =
    (ai :> IBuilder).ResourceId,
    (ag :> IBuilder).ResourceId
    //(ai :> IBuilder).ResourceId |> Managed, 
    //(ag :> IBuilder).ResourceId |> Managed

let exceptionsAlert = alert {
    name $"Server Exceptions - {ai.Name}"
    description "Exception(s) found in the last hour of logs"
    frequency (System.TimeSpan.FromHours(0.5) |> IsoDateTime.OfTimeSpan)
    window (System.TimeSpan.FromHours(1.0) |> IsoDateTime.OfTimeSpan)
    add_linked_resources [ aiId; agId ]
    severity AlertSeverity.Warning
    add_action ag.ActionGroupId

    single_resource_multiple_metric_criteria [
        {
            MetricNamespace = aiId.Type
            MetricName = Insights.MetricsName "exceptions/server"
            Threshold = 0
            Comparison = MetricComparison.GreaterThan
            Aggregation = MetricAggregation.Count
        }
    ]
}

let deployment = arm {
    location Location.UKSouth
    add_resources [
        web
        law
        ai
        ag
        exceptionsAlert
    ]
}

deployment |> Writer.quickWrite deployName
deployment |> Deploy.execute deployName Deploy.NoParameters |> ignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants