avatarKyosuke Ito

Summarize

How to send a metrics alert by azure to slack with logic apps.

Purpose

As I tried to send a metrics alert which is openAI requests to slack. Even though I did it, there were some traps. So I will share it with you to help you.

How to do it.

We should do 3 different tasks, which is ‘create a logic app’, ‘create an action group’, ‘create a metrics alert’.

About creating a logic app and an alert rule.

At first, I tried to send a metric alert with slack incoming webhook. It seems okay but I didn’t do it because slack payload format is different from azure one. Therefore, we need to create a logic app or azure function to format it. In this case, I chose a logic app because it’s easier.

We hopefully have a nice workflow to create metrics alert below.

https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-logic-apps?tabs=send-email

And create alert rule like this.

https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-create-metric-alert-rule

We can almost follow it but in terms of sending a message to slack, we need to do slightly diferrent approach.

Points

When we do ‘add action’ in logic app, we need to search slack and select post message(v2). At first there is no option so we need to click see more.

The post can be set like below. We can set data whatever you want.

This is input form when we want to send a post like above.

We need to type your channel name. However, if you want to send a message to private channel, use channel ID instead of channel name. How checking channel ID is simple just right click on your channel and click ‘view channel detail’.

Above image has only channel ID but it’s successfully sent a message to slack.

Actually, I had a problem about adding a new line when I typed ‘Message text’ with enter key.

I would like to change a new line but I didn’t do it like below.

I noticed the code view was strange because there is no ‘\n’ in code view.

Even if I directly added ‘\n’ in code, it didn’t work.

Because this ‘\n’ converts to ‘\\n’ in code view.

Finally I solved this issue by editing this code view in ‘Logic app code view’ like below.

Conclusion

Even though we followed official way, some points could be traps and it’s diffucult to solve.

I hope my research solve your problem.

Reference

microsoft ‘Customize alert notifications by using Logic Apps’:https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-logic-apps?tabs=send-email

microsoft ‘Create or edit a metric alert rule’:https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-create-metric-alert-rule

AzureのアラートをSlackに通知する方法:https://tech-lab.sios.jp/archives/3788

Thank you for reading!!

Alerts
Azure Logic Apps
Azure
Slack
Recommended from ReadMedium