Microsoft Teams 結合Azure Application Insights Alert

之前還很高興的把Application Insights與Slack串接起來,可參考Azure Application Insights發Alert訊息到Slack ,但沒多久Microsoft又出了一個Microsoft Teams的協同工具,再加上部分工作已經轉移到Teams,為了整合各項資訊到同一平台上,所以,打算把原本發送到Slack的轉移到到Teams上面,再加上Slack免費版只有10000則訊息上限,Teams這方面則沒有上限,當然就二話不說轉過來囉。

基本上是沒有辦法直接在Application Insights的webhooks直接與Teams連結,這之間依舊必須透過logic app和Teams的連接器做橋接

建立Teams 連接器


首先在Teams內建立連接器,把連接器產生的URL放到logic App

  1. 選擇連接器

2.選擇傳入Webhook

3.給這webhook取個名字,按下確定後,就會產生URL,把這URL保留下來,等下開發logic app會需要用到

這樣在Teams上面設定就算完成了

開發Logic App


關於開發Logic App部分,方法跟之前與Slack方式類似,只是這次不需要在Azure上建立連接器,可以先參考這兩篇

這邊會用到Logic App原因主要是要把Application Insights送出來的Alert格式轉換成O365 Connect可接受的格式,Application Insights送出的格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"status": "Activated",
"context": {
"id": "XXXXX",
"name": Server exception",
"description": "",
"conditionType": "Metric",
"condition": { "metricName": "basicExceptionServer.count", "metricUnit": "", "metricValue": "1", "threshold": "1", "timeAggregation": "Average", "operator": "GreaterThanOrEqual", "windowSize": "5" },
"subscriptionId": "XXXXXX",
"resourceGroupName": "XXXXXX",
"timestamp": "11/24/2016 15:35:03",
"resourceName": "XXXXXX",
"resourceType": "components",
"resourceId": "XXXXX",
"resourceRegion": "East US",
"portalLink": "https://XXXXXX"
},
"properties": {}
}

而Microsoft Teams是透過Office 365 API Connectors做串聯,所以,必須將上面資訊對應到Office 365 API Connectors訊息格式內,關於這格式說明,可以參考下面這篇

所以,在Logic App部分只需要用到HTTP就可以,這樣就簡單多

在這Task內的設定如下,其中的URI就放入連接器的URL,而在body部分,就串出符合O365訊息格式資訊就可以

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"HTTP": {
"type": "Http",
"inputs": {
"method": "POST",
"uri": "XXXXX",
"headers": {
"Content-Type": "application/json"
},
"body": {
"sections": [
{
"activityText": "[Look Problem](@{triggerBody()['context']['portalLink']})",
"activityTitle": "Type : @{triggerBody()['context']['resourceName']}"
},
{
"facts": [
{
"name": "Detail Information",
"value": "@{triggerBody()}"
}
],
"title": "Details"
}
],
"summary": "Application Insights Alert",
"title": "@{triggerBody()['context']['name']}"
}
},
"runAfter": {}
}

開發完成後,只要把Logic App的URL放入Application Insights Alert的webhooks裡面就可以,這樣只要有發生錯誤或是相關警示訊息,就會拋到Teams裡面了,個人覺得這部分開發相對比Slack簡單一點