Azure Monitor Alert rule send notification to slack channel

350 views Asked by At

I hope somebody can help me out to solve or at least address me into the right path to solve the following issue.

I have a new alert rule created in azure monitor to alert on Response Time of a few services. Reading the microsoft documentations, i found that i cannot trigger those alert and send notification to slack channel directly, but i need a logic app to take care of this.

So i create the rule and the logic app. The logic app have 2 steps, as follow:

  • When a HTTP request is received with the following Request Body
{
    "properties": {
        "body": {
            "properties": {
                "data": {
                    "properties": {
                        "context": {
                            "properties": {
                                "condition": {
                                    "properties": {
                                        "allOf": {
                                            "items": {
                                                "properties": {
                                                    "dimensions": {
                                                        "type": "array"
                                                    },
                                                    "metricName": {
                                                        "type": "string"
                                                    },
                                                    "metricNamespace": {
                                                        "type": "string"
                                                    },
                                                    "metricValue": {
                                                        "type": "number"
                                                    },
                                                    "operator": {
                                                        "type": "string"
                                                    },
                                                    "threshold": {
                                                        "type": "string"
                                                    },
                                                    "timeAggregation": {
                                                        "type": "string"
                                                    },
                                                    "webTestName": {}
                                                },
                                                "required": [
                                                    "metricName",
                                                    "metricNamespace",
                                                    "operator",
                                                    "threshold",
                                                    "timeAggregation",
                                                    "dimensions",
                                                    "metricValue",
                                                    "webTestName"
                                                ],
                                                "type": "object"
                                            },
                                            "type": "array"
                                        },
                                        "windowSize": {
                                            "type": "string"
                                        }
                                    },
                                    "type": "object"
                                },
                                "conditionType": {
                                    "type": "string"
                                },
                                "description": {
                                    "type": "string"
                                },
                                "id": {
                                    "type": "string"
                                },
                                "name": {
                                    "type": "string"
                                },
                                "portalLink": {
                                    "type": "string"
                                },
                                "resourceGroupName": {
                                    "type": "string"
                                },
                                "resourceId": {
                                    "type": "string"
                                },
                                "resourceName": {
                                    "type": "string"
                                },
                                "resourceType": {
                                    "type": "string"
                                },
                                "severity": {
                                    "type": "string"
                                },
                                "subscriptionId": {
                                    "type": "string"
                                },
                                "timestamp": {
                                    "type": "string"
                                }
                            },
                            "type": "object"
                        },
                        "properties": {},
                        "status": {
                            "type": "string"
                        },
                        "version": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "schemaId": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "headers": {
            "properties": {
                "CLIENT-IP": {
                    "type": "string"
                },
                "Content-Length": {
                    "type": "string"
                },
                "Content-Type": {
                    "type": "string"
                },
                "Cookie": {
                    "type": "string"
                },
                "DISGUISED-HOST": {
                    "type": "string"
                },
                "Expect": {
                    "type": "string"
                },
                "Host": {
                    "type": "string"
                },
                "Max-Forwards": {
                    "type": "string"
                },
                "User-Agent": {
                    "type": "string"
                },
                "WAS-DEFAULT-HOSTNAME": {
                    "type": "string"
                },
                "X-ARR-LOG-ID": {
                    "type": "string"
                },
                "X-ARR-SSL": {
                    "type": "string"
                },
                "X-AppService-Proto": {
                    "type": "string"
                },
                "X-CorrelationContext": {
                    "type": "string"
                },
                "X-Forwarded-For": {
                    "type": "string"
                },
                "X-Forwarded-Proto": {
                    "type": "string"
                },
                "X-Forwarded-TlsVersion": {
                    "type": "string"
                },
                "X-Original-URL": {
                    "type": "string"
                },
                "X-SITE-DEPLOYMENT-ID": {
                    "type": "string"
                },
                "X-WAWS-Unencoded-URL": {
                    "type": "string"
                }
            },
            "type": "object"
        }
    },
    "type": "object"
}

and the second step is the:

  • Post Message, which is connect to a slack channel.

The problem i am facing is that when an alert triggers and i receive the notification in slack, the notification has empty values.As follow

Name:
Description:
Resource Name:

My expected output here is as follow:

Name: Something
Description: some descritpion
....

As far as i understand and i was able to debug(hopefully) is that the payload generated by the alert, is a common schema, which is completely different from the Request Body passed into the first Step of the logic App. While looking at the dynamic variables in the Second step of the logic app, they point to triggerBody()?['data']?['context']?['name']

Those values matches the HTTP request is received payload in logic app, while they are different from the custom payload in the monitor alert.

Can please anyone help me understand if i am on the right track and how can this be solved to be able to populate correctly the fields in the message that is being sent to slack?

I am sorry if i missed something or not 100% clear, if you need anymore details or clarification, just let me know

1

There are 1 answers

0
Ikhtesam Afrin On BEST ANSWER

I have created an alert by following Customize alert notifications by using Logic Apps.

Initially I added When a HTTP request is received trigger in the workflow by keeping Request Body JSON Schema as empty. Once the alert fired, I took the output from the Run History of Logic App to generate the schema as shown below-

enter image description here

Click on Use sample payload to generate schema

enter image description here

Paste the output and click on Done to generate input schema.

enter image description here

Then I added Post Message (V2) action to connect to slack.

enter image description here

I am able to get the expected output.

enter image description here enter image description here

References-

Logic App HTTP Request JSON from Azure Application Insights Alerting Notifications - Stack Overflow.