開始使用Azure Application Insights REST API

Application Insights是Azure監控我們開發的系統一個很好的服務之一,主要可以讓我們省去很多工,就可以抓取到系統運作資訊和問題點。通常,我們能到Azure Portal查看Application Insights紀錄的資訊,不過,Azure上面資訊統計或是分析方式,畢竟還是微軟官方提供的,如果想要客製化來分析資料,就必須把資料抓回來,自己針對資料做相關性分析,現在可以透過Azure Application Insights REST API讓我們把資料給傳遞回來

Azure Application Insights REST API現在是屬於Beta版,所以,使用上難免有一些不方便,且功能性還不是那麼完全,但是,一些常用性的資訊已經給被傳遞出來,在開始使用Azure Application Insights REST API前,必須先到Azure Portal進行一些準備事項

事前準備


  • Application Insights付費機制最少必須是Standard

為什麼有這限制,我猜因該是在於是否有Data Export功能所致吧

  • 建立Application Insights API金鑰
記得建立完成後,要把金鑰記錄下來,不然事後回來是查不到的

開始使用API吧


目前在API的資料可以提供的資料種類Metric,EventsQuery三種方式,個人最常用到的是查詢Events類型的資料,而Application Insights API查詢資料方式是使用OData的語法做資料搜尋

Application Insights API的Address如下

1
https://api.applicationinsights.io/beta/apps/{APPID}/{queryType}/{queryPath}?{parameterString}

網址中的參數分別為:

  1. APPID : 就是Application Insights中的金鑰
  2. queryType : 就是要查詢資料種類類型
  3. queryPath : 就是要查詢的資料位置,若是用Event Type來說,目前提供requests$all可以使用,不同類型使用的位置也不同
  4. parameterString : 這就是使用OData的指令進行查詢了,例如使用$top,$filter…等,這邊針對有哪些欄位部分,可能還是必須進入Portal查看會比較清楚

在每個類型可用的queryPath,也可以查看下面這個表

在Event類型下的資訊,共有這六種分類

所以,當呼叫API回傳的Json格式也是包含這六種分類的資料內容,這邊先把回傳的Json資料結構轉換成強型別使用

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
public class ApplicationInsightsMessage
{
public string odatacontext { get; set; }
public AiMessages[] aimessages { get; set; }
public Value[] value { get; set; }
}
public class AiMessages
{
public string code { get; set; }
public string message { get; set; }
}
public class Value
{
public DateTime timestamp { get; set; }
public string id { get; set; }
public string type { get; set; }
public int count { get; set; }
public Exception exception { get; set; }
public Customdimensions customDimensions { get; set; }
public object customMeasurements { get; set; }
public Operation operation { get; set; }
public Session session { get; set; }
public User user { get; set; }
public Application application { get; set; }
public Client client { get; set; }
public Cloud cloud { get; set; }
public Ai ai { get; set; }
public Pageview pageView { get; set; }
public Customevent customEvent { get; set; }
}
public class Exception
{
public string problemId { get; set; }
public string handledAt { get; set; }
public string type { get; set; }
public object message { get; set; }
public string assembly { get; set; }
public string method { get; set; }
public string outerType { get; set; }
public string outerMessage { get; set; }
public object outerAssembly { get; set; }
public object outerMethod { get; set; }
public string innermostType { get; set; }
public string innermostMessage { get; set; }
public object innermostAssembly { get; set; }
public object innermostMethod { get; set; }
public object severityLevel { get; set; }
public Detail[] details { get; set; }
}
public class Detail
{
public string outerId { get; set; }
public string message { get; set; }
public string type { get; set; }
public string id { get; set; }
}
public class Customdimensions
{
public string CPUCore { get; set; }
public string ApplicationVersion { get; set; }
public string Time { get; set; }
public string PhysicalMemory { get; set; }
}
public class Operation
{
public object name { get; set; }
public object id { get; set; }
public object parentId { get; set; }
public object syntheticSource { get; set; }
}
public class Session
{
public string id { get; set; }
}
public class User
{
public string id { get; set; }
public object authenticatedId { get; set; }
public object accountId { get; set; }
}
public class Application
{
public object version { get; set; }
}
public class Client
{
public string type { get; set; }
public object model { get; set; }
public string os { get; set; }
public string ip { get; set; }
public string city { get; set; }
public object stateOrProvince { get; set; }
public string countryOrRegion { get; set; }
public object browser { get; set; }
}
public class Cloud
{
public object roleName { get; set; }
public object roleInstance { get; set; }
}
public class Ai
{
public string appId { get; set; }
public string appName { get; set; }
public string iKey { get; set; }
public string sdkVersion { get; set; }
}
public class Pageview
{
public string name { get; set; }
public object url { get; set; }
public object duration { get; set; }
public object performanceBucket { get; set; }
}
public class Customevent
{
public string name { get; set; }
}

然後,就可以透過API把資料取回來,其中在x-api-key填入你剛剛在Portal建立Application Insights API時所產生的金鑰

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string APPID = "XXX";
string queryType = "events";
string queryPath = "$all";
string parameterString = "$top=1";
string Result = string.Empty;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", "XXXX");
var req = string.Format("https://api.applicationinsights.io/beta/apps/{0}/{1}/{2}?{3}", APPID, queryType, queryPath, parameterString);
HttpResponseMessage response = client.GetAsync(req).Result;
if (response.IsSuccessStatusCode)
{
ApplicationInsightsMessage applicationInsightsMessage =
JsonConvert.DeserializeObject<ApplicationInsightsMessage>(
response.Content.ReadAsStringAsync().Result);
return applicationInsightsMessage;
}
else
{
return null;
}

這樣就可以呼叫API取資料了,這寫一個小小的Unit Test驗證是否正確,因為,目前實測結果,當資料送出後,Application Insights收到大約要經過三分鐘,所以,這邊必須先Sleep,不然會發生測試錯誤

1
2
3
4
_telemetryClient.TrackPageView("TrackPageViewTest2");
System.Threading.Thread.Sleep(210000);
ApplicationInsightsMessage AIM = ApplicationInsightsAPI.ReadEvent();
Assert.AreEqual("TrackPageViewTest2", AIM.value[0].pageView.name);

目前上面資料點保存時間只有七天,如果資料是屬於統計分析或是針對資料做相依性分析,建議把資料傳回自己的DB保存起來

參考資料


  1. OData : http://www.odata.org
  2. Application Insights API : https://dev.applicationinsights.io/documentation/overview