Kusto 查詢語言 (KQL) 包括用於時間序列分析、異常偵測、預測和根本原因分析的機器學習運算子、函數和外掛程式。 使用這些 KQL 功能以在 Azure 監視器中執行進階資料分析,而沒有將資料匯出至外部機器學習工具的額外負荷。
Azure 監視器 Logs 是儲存你的日誌資料的服務。 Log Analytics 是 Azure 入口網站中用來使用 KQL 查詢資料的工具。 本教學中連結的 KQL 函式參考文章是共享的 Kusto 文件。
在本教學課程中,您會了解如何:
- 建立時間序列
- 識別時間序列中的異常
- 調整異常偵測設定以精簡結果
- 分析異常根本原因
附註
這個教學提供了連結到一個 Log Analytics 的示範環境,你可以在那裡執行 KQL 查詢範例。 示範環境中的資料是動態的,因此查詢結果與本文所示的查詢結果不同。 在你自己的環境以及所有使用 KQL 的 Azure 監視器 工具中執行相同的 KQL 查詢和原則。
必要條件
- 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶。
- 具有記錄資料的工作區。
需要的權限
例如,您必須擁有所查詢 Log Analytics 工作區的 Microsoft.OperationalInsights/workspaces/query/*/read 權限,這是由 Log Analytics 讀者內建角色所提供。
用 make-series 建立時間序列
使用 KQL make-series 運算子來建立時間序列。
根據 使用資料表中的日誌建立時間序列,該記錄包含工作空間中每個資料表每小時所接收的資料量,包括可計費與不可計費資料。
此查詢會使用 make-series 來繪製過去 21 天內,工作區中每個資料表每天所擷取的總計費資料量:
let starttime = 21d; // The start date of the time series, counting back from the current date
let endtime = 0d; // The end date of the time series, counting back from the current date
let timeframe = 1d; // How often to sample data
Usage // The table we're analyzing
| where TimeGenerated between (startofday(ago(starttime))..startofday(ago(endtime))) // Time range for the query, beginning at 12:00 AM of the first day and ending at 12:00 AM of the last day in the time range
| where IsBillable == "true" // Include only billable data in the result set
| make-series ActualUsage=sum(Quantity) default = 0 on TimeGenerated from startofday(ago(starttime)) to startofday(ago(endtime)) step timeframe by DataType // Creates the time series, listed by data type
| render timechart // Renders results in a timechart
結果圖表顯示了一些異常,例如資料類型:AzureDiagnosticsSecurityEvent
若要列出時間序列中的所有異常,請使用series_decompose_anomalies()「尋找時間序列異常」中描述的函數,series_decompose_anomalies())。
附註
如需 make-series 語法和使用方式的詳細資訊,請參閱 make-series 運算子。
在時間序列中尋找異常點,series_decompose_anomalies()
series_decompose_anomalies() 函數會採用一系列值作為輸入,並擷取異常。
給出 make series 查詢 的結果集,作為函數的輸入 series_decompose_anomalies() :
let starttime = 21d; // Start date for the time series, counting back from the current date
let endtime = 0d; // End date for the time series, counting back from the current date
let timeframe = 1d; // How often to sample data
Usage // The table we're analyzing
| where TimeGenerated between (startofday(ago(starttime))..startofday(ago(endtime))) // Time range for the query, beginning at 12:00 AM of the first day and ending at 12:00 AM of the last day in the time range
| where IsBillable == "true" // Includes only billable data in the result set
| make-series ActualUsage=sum(Quantity) default = 0 on TimeGenerated from startofday(ago(starttime)) to startofday(ago(endtime)) step timeframe by DataType // Creates the time series, listed by data type
| extend(Anomalies, AnomalyScore, ExpectedUsage) = series_decompose_anomalies(ActualUsage) // Scores and extracts anomalies based on the output of make-series
| mv-expand ActualUsage to typeof(double), TimeGenerated to typeof(datetime), Anomalies to typeof(double),AnomalyScore to typeof(double), ExpectedUsage to typeof(long) // Expands the array created by series_decompose_anomalies()
| where Anomalies != 0 // Returns all positive and negative deviations from expected usage
| project TimeGenerated,ActualUsage,ExpectedUsage,AnomalyScore,Anomalies,DataType // Defines which columns to return
| sort by abs(AnomalyScore) desc // Sorts results by anomaly score in descending ordering
此查詢會傳回所有資料表在過去三周的所有使用異常:
查詢結果顯示函數:
- 計算每個資料表的預期每日使用量。
- 比較實際每日使用量與預期使用量。
- 將異常分數指派給每個資料點,指出實際使用量與預期使用量的偏差範圍。
- 識別每個資料表中的正 (
1) 和負 (-1) 異常。
附註
如需 series_decompose_anomalies() 語法和使用方式的詳細資訊,請參閱 series_decompose_anomalies()。
調整異常偵測設定以精簡結果
最好檢閱初始查詢結果,並視需要調整查詢。 輸入資料中的極端值可能會影響函數的學習,而且您可能需要調整函數的異常偵測設定,以取得更精確的結果。
篩選 series_decompose_anomalies() 查詢的結果是否有 AzureDiagnostics 資料類型異常:
隨著21天查詢窗口的移動,結果中的日期和分數會改變。 將目前結果與 建立時間序列查詢 的圖表比較,並注意該函數識別為異常點:
結果中出現差異的原因是 series_decompose_anomalies() 函數針對預期的使用量值進行異常評分,而函數根據輸入系列中的完整值範圍來進行計算。
為了比較從先前學到的基線,將近期點與先前學到的點進行比較,從函數的學習過程中排除一個或多個點。
series_decompose_anomalies() 函數的語法為:
series_decompose_anomalies(Series [, Threshold, Seasonality, Trend, Test_points, AD_method, Seasonality_threshold])
函數取以下參數:
| 參數 | 說明 | 預設值 |
|---|---|---|
Series |
輸入數值序列需分析。 | Required |
Threshold |
異常偵測閾值。 較低的數值會增加靈敏度。 | 1.5 |
Seasonality |
控制季節分析。
-1 自動偵測季節性, 0 並關閉它,並以正整數設定週期。 |
-1 |
Trend |
趨勢分析方法,例如 avg 或 linefit。 |
avg |
Test_points |
數列結束時,從學習(迴歸)過程中排除的點數。 | 0 |
AD_method |
異常偵測方法。 | ctukey |
Seasonality_threshold |
自動偵測時季節性評分的門檻。 | 0.6 |
若要排除學習過程中最後一個資料點,請設 Test_points 為 1。 這個設定總是保留著本系列的最後一點。 它並不指固定的曆法日期:
let starttime = 21d; // Start date for the time series, counting back from the current date
let endtime = 0d; // End date for the time series, counting back from the current date
let timeframe = 1d; // How often to sample data
Usage // The table we're analyzing
| where TimeGenerated between (startofday(ago(starttime))..startofday(ago(endtime))) // Time range for the query, beginning at 12:00 AM of the first day and ending at 12:00 AM of the last day in the time range
| where IsBillable == "true" // Includes only billable data in the result set
| make-series ActualUsage=sum(Quantity) default = 0 on TimeGenerated from startofday(ago(starttime)) to startofday(ago(endtime)) step timeframe by DataType // Creates the time series, listed by data type
| extend(Anomalies, AnomalyScore, ExpectedUsage) = series_decompose_anomalies(ActualUsage, 1.5, -1, 'avg', 1) // Excludes the final series value from learning. Other input values are the function defaults
| mv-expand ActualUsage to typeof(double), TimeGenerated to typeof(datetime), Anomalies to typeof(double),AnomalyScore to typeof(double), ExpectedUsage to typeof(long) // Expands the array created by series_decompose_anomalies()
| where Anomalies != 0 // Returns all positive and negative deviations from expected usage
| project TimeGenerated,ActualUsage,ExpectedUsage,AnomalyScore,Anomalies,DataType // Defines which columns to return
| sort by abs(AnomalyScore) desc // Sorts results by anomaly score in descending ordering
篩選 AzureDiagnostics 資料類型的結果:
將修改後的結果與原始結果進行比較。 根據目前資料,保留最後一點可能會改變其期望值、異常分數或分類。 只有當你打算評估或預測序列末尾的那麼多點時,才會增加 Test_points 。
使用 diffpatterns() 插件分析異常的根本原因
比較預期值與異常值,可協助您瞭解兩個集合之間的差異原因。
KQL diffpatterns() 外掛程式會比較兩個相同結構的資料集,並找出可描述兩個資料集之間差異的模式。
以下查詢會選擇目前 21 天內最 AzureDiagnostics 強的使用異常。 接著將該日期的紀錄與其他日期的紀錄進行比較。 打開 Log Analytics 示範環境,並執行查詢。
let starttime = 21d; // Start date for the time series, counting back from the current date
let endtime = 0d; // End date for the time series, counting back from the current date
let anomalyDate = toscalar(
Usage
| where TimeGenerated between (startofday(ago(starttime))..startofday(ago(endtime)))
| where IsBillable == "true" and DataType == "AzureDiagnostics"
| make-series ActualUsage=sum(Quantity) default = 0 on TimeGenerated from startofday(ago(starttime)) to startofday(ago(endtime)) step 1d
| extend (Anomalies, AnomalyScore, ExpectedUsage) = series_decompose_anomalies(ActualUsage)
| mv-expand TimeGenerated to typeof(datetime), Anomalies to typeof(double), AnomalyScore to typeof(double)
| where Anomalies != 0
| top 1 by abs(AnomalyScore) desc
| project TimeGenerated
);
AzureDiagnostics
| extend AnomalyDate = iff(startofday(TimeGenerated) == anomalyDate, "AnomalyDate", "OtherDates") // Splits the result set into the selected anomaly date and all other dates
| where TimeGenerated between (startofday(ago(starttime))..startofday(ago(endtime))) // Defines the time range for the query
| project AnomalyDate, Resource // Defines which columns to return
| evaluate diffpatterns(AnomalyDate, "OtherDates", "AnomalyDate") // Compares usage on the anomaly date with the regular usage pattern
查詢會將表格中的每個項目識別為發生在 AnomalyDate 或 OtherDates。
diffpatterns()外掛接著將這些資料集拆分(A 是 OtherDates,B 是異常日期),並回傳導致這些資料集差異的模式。 如果查詢未偵測到異常,請在執行分析前擴大時間範圍或調整異常閾值。
檢視回傳的模式,找出 AnomalyDate 與其他 日期記錄數量或百分比差異最大的資源。 這些值會隨所選異常現象及示範工作區目前內容而變化。
PercentDiffAB 資料行顯示 A 與 B 之間的絕對百分比點差異 (|PercentA - PercentB|),而這是兩個集合之間差異的主要量值。 預設情況下,外 diffpatterns() 掛會回傳兩個資料集之間超過 5% 的差異。 調整閾值參數以改變此行為:
| Argument | 說明 | 預設值 |
|---|---|---|
| 閾值 | 兩個資料集之間的最小百分點差異,以回應一個模式。 接受介於 0.015 與 1之間的值。 |
0.05 |
例如,若要回傳兩個資料集之間 20% 或以上的差異,就如上方查詢中所設定 | evaluate diffpatterns(AnomalyDate, "OtherDates", "AnomalyDate", "~", 0.20) 。 查詢僅回傳絕對百分比差距達20% 以上的模式。 結果數量取決於所選異常現象及當前示範數據。 下圖展示了一個範例結果:
附註
欲了解更多語 diffpatterns() 法與使用資訊,請參閱 diffpatterns 插件。
下一步
深入了解: