共用方式為


Azure 監視器中的轉換結構

Azure 監視器中的轉換可讓您在傳入資料儲存在 Log Analytics 工作區之前對其進行篩選或修改。 其會實作為資料收集規則 (DCR) 中的 Kusto 查詢語言 (KQL) 陳述式。 本文提供如何結構化此查詢的詳細資料,以及允許 KQL 語言的限制。

轉換結構

KQL 陳述式會個別套用至資料來源中的每個項目。 其必須了解傳入資料的格式,並在目標資料表的結構中建立輸出。 名為 source 的虛擬資料表代表輸入資料流。 source 資料表資料列符合輸入資料流定義。 下列是轉換的典型範例。 此範例包含下列功能:

  • 使用 where 陳述式篩選傳入資料
  • 使用 extend 運算子加入新的資料行
  • 使用 project 運算子格式化輸出以符合目標資料表的資料行
source  
| where severity == "Critical" 
| extend Properties = parse_json(properties)
| project
    TimeGenerated = todatetime(["time"]),
    Category = category,
    StatusDescription = StatusDescription,
    EventName = name,
    EventId = tostring(Properties.EventId)

KQL 限制

由於轉換會個別套用至每個記錄,因此其不能使用任何對多個記錄採取動作的 KQL 運算子。 僅支援接受單一資料列作為輸入並傳回不超過一個資料列的運算子。 例如,不支援 summarize,因為其摘要多個記錄。 如需支援功能的完整清單,請參閱支援的 KQL 功能

資料收集規則 (DCR) 中的轉換允許您在傳入資料儲存到 Log Analytics 工作區之前對其進行篩選或修改。 本文說明如何在 DCR 中建置轉換,包含用於轉換陳述式的 Kusto 查詢語言 (KQL) 的詳細資料和限制。

必要欄

每個轉換的輸出都必須在名為 TimeGenerated 的類型 datetime 的資料行中包含有效的時間戳。 請務必包含在最終 extendproject 區塊中! 在轉換的輸出中建立或更新沒有 TimeGenerated DCR 會導致錯誤。

處理動態資料

考慮使用動態資料的下列輸入:

{
    "TimeGenerated" : "2021-11-07T09:13:06.570354Z",
    "Message": "Houston, we have a problem",
    "AdditionalContext": {
        "Level": 2,
        "DeviceID": "apollo13"
    }
}

若要存取 AdditionalContext 中的屬性,請將其定義為輸入資料流中的動態類型資料行:

"columns": [
    {
        "name": "TimeGenerated",
        "type": "datetime"
    },
    {
        "name": "Message",
        "type": "string"
    }, 
    {
        "name": "AdditionalContext",
        "type": "dynamic"
    }
]

AdditionalContext 資料行的內容現在可以在 KQL 轉換中剖析和使用:

source
| extend parsedAdditionalContext = parse_json(AdditionalContext)
| extend Level = toint (parsedAdditionalContext.Level)
| extend DeviceId = tostring(parsedAdditionalContext.DeviceID)

動態常值

使用 parse_json 函式來處理動態常值

例如,下列查詢提供相同功能:

print d=dynamic({"a":123, "b":"hello", "c":[1,2,3], "d":{}})
print d=parse_json('{"a":123, "b":"hello", "c":[1,2,3], "d":{}}')

支援的 KQL 功能

支援的陳述式

Let 陳述式

let 的右側可以是純量運算式、表格式運算式或使用者定義函式。 僅支援具有純量引數的使用者定義函式。

表格式運算式陳述式

KQL 陳述式唯一支援的資料來源如下所示:

  • source,表示來源資料。 例如:
source
| where ActivityId == "383112e4-a7a8-4b94-a701-4266dfc18e41"
| project PreciseTimeStamp, Message
  • print 運算子,一律產生單一資料列。 例如:
print x = 2 + 2, y = 5 | extend z = exp2(x) + exp2(y)

表格式運算子

Scalar 運算子

數值運算子

支援所有數值運算子

DateTime 和 TimeSpan 算術運算子

支援所有 DateTime 和 TimeSpan 算術運算子

字串運算子

支援下列字串運算子

  • ==
  • !=
  • =~
  • !~
  • contains
  • !contains
  • contains_cs
  • !contains_cs
  • has
  • !has
  • has_cs
  • !has_cs
  • startswith
  • !startswith
  • startswith_cs
  • !startswith_cs
  • endswith
  • !endswith
  • endswith_cs
  • !endswith_cs
  • 符合 RegEx
  • !in

位元運算子

支援下列位元運算子

  • binary_and()
  • binary_or()
  • binary_xor()
  • binary_not()
  • binary_shift_left()
  • binary_shift_right()

純量函式

位元函式

轉換函數

DateTime 和 TimeSpan 函式

動態和陣列函式

數學函式

條件式函式

字串函數

類型函式

特殊函式

parse_cef_dictionary

假設字串包含 CEF 訊息,parse_cef_dictionary 會將訊息的 Extension 屬性剖析為動態索引鍵/值物件。 分號是保留字元,應該在將原始訊息傳遞至方法之前加以取代,如範例所示。

| extend cefMessage=iff(cefMessage contains_cs ";", replace(";", " ", cefMessage), cefMessage) 
| extend parsedCefDictionaryMessage =parse_cef_dictionary(cefMessage) 
| extend parsecefDictionaryExtension = parsedCefDictionaryMessage["Extension"]
| project TimeGenerated, cefMessage, parsecefDictionaryExtension

Sample output of parse_cef_dictionary function.

geo_location

假設有包含IP位址的字串 (支援IPv4和IPv6),geo_location 函式會傳回大約地理位置,包括下列屬性:

  • Country
  • 區域
  • 狀態
  • City
  • 緯度
  • 緯度
| extend GeoLocation = geo_location("1.0.0.5")

Screenshot of sample output of geo_location function.

重要

由於此函式所使用的 IP 地理位置服務本質,如果過度使用,可能會造成資料擷取延遲。 在每個轉換使用此函式時,請謹慎使用多次。

識別碼引號

視需要使用識別碼引號

下一步