教學課程:將事件從 Azure 事件中樞擷取至 Azure 監視器記錄 (公開預覽)

Azure 事件中樞是巨量資料串流平台,可從多個來源收集事件,以便 Azure 和外部服務擷取。 本文說明如何直接將資料從事件中樞擷取至 Log Analytics工作區。

在本教學課程中,您會了解如何:

  • 在 Log Analytics 工作區中建立事件中樞資料的目的地資料表
  • 建立資料收集端點
  • 建立資料收集規則
  • 將資料收集規則權限授與事件中樞
  • 將資料收集規則與事件中樞產生關聯

必要條件

若要將事件從 Azure 事件中樞傳送至 Azure 監視器記錄,您需要這些資源 (全都在相同的區域中):

支援的區域

Azure 監視器目前支援從這些區域中的事件中樞擷取資料:

美洲 歐洲 中東 非洲 亞太地區
巴西南部 法國中部 阿拉伯聯合大公國北部 南非北部 澳大利亞中部
巴西東南部 北歐 澳大利亞東部
加拿大中部 挪威東部 澳大利亞東南部
加拿大東部 瑞士北部 印度中部
美國東部 瑞士西部 東亞
美國東部 2 英國南部 日本東部
美國中南部 英國西部 Jio 印度西部
美國西部 西歐 南韓中部
美國西部 3 東南亞

收集必要資訊

在後續步驟中,您需要訂用帳戶標識碼、資源群組名稱、工作區名稱、工作區資源標識碼和事件中樞執行個體資源標識碼:

  1. 瀏覽至 Log Analytics 工作區功能表中的工作區,然後選取 [屬性],並複製您的訂用帳戶標識碼資源群組工作區名稱。 您將需要這些詳細資料,才能在本教學課程中建立資源。

    Screenshot showing Log Analytics workspace overview screen with subscription ID, resource group name, and workspace name highlighted.

  2. 選取 [JSON] 以開啟 [資源 JSON] 畫面,並複製工作區的資源識別碼。 您需要工作區資源識別碼,才能建立資料收集規則。

    Screenshot showing the Resource JSON screen with the workspace resource ID highlighted.

  3. 瀏覽至您的事件中樞執行個體,選取 [JSON] 以開啟 [資源 JSON] 畫面,然後複製事件中樞執行個體的資源識別碼。 您需要事件中樞執行個體的資源標識碼,才能將資料收集規則與事件中樞產生關聯。

    Screenshot showing the Resource JSON screen with the event hub resource ID highlighted.

在 Log Analytics 工作區中建立目的地資料表

您必須先設定目的地資料表,才能擷取資料。 您可以將資料擷取至自訂資料表和支援的 Azure 資料表

若要在 Azure 入口網站中建立用來擷取事件的自訂資料表:

  1. 選取 [Cloud Shell] 按鈕,並確保已將環境設定為 [PowerShell]

    Screenshot showing how to open Cloud Shell.

  2. 執行此 PowerShell 命令來建立資料表,並以 JSON 提供資料表名稱 (<table_name>) (若是自訂資料表,則在名稱後方加上 _CL),並在 Invoke-AzRestMethod -Path 命令中設定 <subscription_id><resource_group_name><workspace_name><table_name> 值:

    $tableParams = @'
    {
        "properties": {
            "schema": {
                "name": "<table_name>",
                "columns": [
                    {
                        "name": "TimeGenerated",
                        "type": "datetime",
                        "description": "The time at which the data was ingested."
                    },
                    {
                        "name": "RawData",
                        "type": "string",
                        "description": "Body of the event."
                    },
                    {
                        "name": "Properties",
                        "type": "dynamic",
                        "description": "Additional message properties."
                    }
                ]
            }
        }
    }
    '@
    
    Invoke-AzRestMethod -Path "/subscriptions/<subscription_id>/resourcegroups/<resource_group_name>/providers/microsoft.operationalinsights/workspaces/<workspace_name>/tables/<table_name>?api-version=2021-12-01-preview" -Method PUT -payload $tableParams
    

重要

  • 資料行名稱必須以字母開頭,且最多可以包含 45 個英數字元和底線 (_)。
  • _ResourceIdid_ResourceId_SubscriptionIdTenantIdTypeUniqueIdTitle 是保留的資料行名稱。
  • 資料行名稱區分大小寫。 請務必在資料收集規則中使用正確的大小寫。

建立資料收集端點

若要使用資料收集規則收集資料,您需要資料收集端點:

  1. 建立資料收集端點

    重要

    在與 Log Analytics 工作區相同的區域中建立資料收集端點。

  2. 從資料收集端點的 [概觀] 畫面,選取 [JSON 檢視]

    Screenshot that shows the data collection endpoint Overview screen.

  3. 複製資料收集規則的 [資源識別碼]。 您會在下一個步驟中使用此資訊。

    Screenshot that shows the data collection endpoint JSON view.

建立資料收集規則

Azure 監視器會使用資料收集規則來定義要收集的資料、如何轉換該資料,以及傳送資料的位置。

若要在 Azure 入口網站中建立資料收集規則:

  1. 在 入口網站的搜尋方塊中輸入「範本」,然後選取 [部署自訂範本]

    Screenshot to deploy custom template.

  2. 選取 [在編輯器中組建您自己的範本]

    Screenshot to build template in the editor.

  3. 將下方的 Resource Manager 範本貼至編輯器中,然後選取 [儲存]

    Screenshot to edit Resource Manager template.

    請注意下方資料收集規則中的下列詳細資料:

    • identity - 定義要使用的受控識別類型。 在我們的範例中,我們使用系統指派的身分識別。 您也可以設定使用者指派的受控識別

    • dataCollectionEndpointId:資料收集端點的資源識別碼。

    • streamDeclarations - 定義要從事件中樞擷取的資料 (傳入資料)。 串流宣告無法修改。

      • TimeGenerated - 從事件中樞擷取資料到 Azure 監視器記錄的時間。
      • RawData - 事件的本文。 如需詳細資訊,請參閱讀取事件
      • Properties - 事件中的使用者屬性。 如需詳細資訊,請參閱讀取事件
    • datasources - 指定事件中樞取用者群組和您擷取資料的串流。

    • destinations - 指定將傳送資料過去的所有目的地。 您可以將資料擷取至一個或多個 Log Analytics 工作區

    • dataFlows - 比對資料流與目的地工作區,並指定轉換查詢與目的地資料表。 在我們的範例中,我們會將資料擷取至我們先前建立的自訂資料表。 您也可以擷取至支援的 Azure 資料表

    • transformKql - 指定要在傳送至工作區之前套用至傳入資料 (串流宣告) 的轉換。 在我們的範例中,我們會將 transformKql 設定為 source,這不會以任何方式修改來源的資料,因為我們會將傳入的資料對應至特別使用對應結構描述建立的自訂資料表。 如果您要將資料擷取至具有不同結構描述的資料表,或在擷取之前篩選資料,請定義資料收集轉換

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "dataCollectionRuleName": {
                "type": "string",
                "metadata": {
                    "description": "Specifies the name of the data collection Rule to create."
                }
            },
            "workspaceResourceId": {
                "type": "string",
                "metadata": {
                    "description": "Specifies the Azure resource ID of the Log Analytics workspace to use."
                }
            },
            "endpointResourceId": {
                "type": "string",
                "metadata": {
                    "description": "Specifies the Azure resource ID of the data collection endpoint to use."
                }
            },
            "tableName": {
                "type": "string",
                "metadata": {
                    "description": "Specifies the name of the table in the workspace."
                }
            },
            "consumerGroup": {
                "type": "string",
                "metadata": {
                    "description": "Specifies the consumer group of event hub."
                },
                "defaultValue": "$Default"
            }
        },
        "resources": [
            {
                "type": "Microsoft.Insights/dataCollectionRules",
                "name": "[parameters('dataCollectionRuleName')]",
                "location": "[resourceGroup().location]", 
                "apiVersion": "2022-06-01",
                "identity": {
                                 "type": "systemAssigned"
                  },
                "properties": {
                    "dataCollectionEndpointId": "[parameters('endpointResourceId')]",
                    "streamDeclarations": {
                        "Custom-MyEventHubStream": {
                            "columns": [
                    {
                        "name": "TimeGenerated",
                        "type": "datetime"
                    },
                    {
                        "name": "RawData",
                        "type": "string"
                    },
                    {
                        "name": "Properties",
                        "type": "dynamic"
                    }
                ]
                        }
                    },
                    "dataSources": {
                        "dataImports": {
                             "eventHub": {
                                        "consumerGroup": "[parameters('consumerGroup')]",
                                        "stream": "Custom-MyEventHubStream",
                                        "name": "myEventHubDataSource1"
                                                              }
                                               }
                   },
                    "destinations": {
                        "logAnalytics": [
                            {
                                "workspaceResourceId": "[parameters('workspaceResourceId')]",
                                "name": "MyDestination"
                            }
                        ]
                    },
                    "dataFlows": [
                        {
                            "streams": [
                                "Custom-MyEventHubStream"
                            ],
                            "destinations": [
                                "MyDestination"
                            ],
                            "transformKql": "source",
                            "outputStream": "[concat('Custom-', parameters('tableName'))]"
                        }
                    ]
                }
            }
        ]
    }
    
  4. 在 [自訂部署] 畫面上,指定 [訂用帳戶] 和 [資源群組] 以儲存資料收集規則,然後提供在範本中定義的參數值,包括:

    • 區域 - 資料收集規則的區域。 根據您選取的資源群組自動填入。
    • 資料收集規則名稱 - 為規則指定名稱。
    • 工作區資源識別碼 - 請參閱收集必要資訊
    • 端點資源識別碼 - 在您建立資料收集端點時產生。
    • 資料表名稱 - 目的地資料表的名稱。 在我們的範例中,每當您使用自訂資料表時,資料表名稱後方都必須加上 _CL。 如果您要將資料擷取至 Azure 資料表,請輸入資料表名稱,例如,Syslog (無需後置詞)。
    • 取用者群組 - 根據預設,取用者群組會設定為 $Default。 如有需要,請將值變更為不同的事件中樞取用者群組

    Screenshot showing the Custom Template Deployment screen with the deployment values for the data collection rule set up in this tutorial.

  5. 檢閱詳細資料時,依序選取 [檢閱 + 建立] 和 [建立]

  6. 部署完成時,展開 [部署詳細資料] 方塊,並選取資料收集規則以檢視其詳細資料。 選取 [JSON 檢視]

    Screenshot that shows the Data Collection Rule Overview screen.

  7. 複製資料收集規則的 [資源識別碼]。 您會在下一個步驟中使用此資訊。

    Screenshot that shows the data collection rule JSON view.

設定使用者指定的受控識別 (選擇性)

若要設定資料收集規則以支援使用者指派的身分識別,請在上述範例中取代:

    "identity": {
                        "type": "systemAssigned"
        },

取代為:

    "identity": {
            "type": "userAssigned",
            "userAssignedIdentities": {
                "<identity_resource_Id>": {
                }
            }
        },

若要尋找 <identity_resource_Id> 值,請在 Azure 入口網站中瀏覽至使用者指派的受控識別資源,選取 [JSON] 以開啟 [資源 JSON] 畫面,並複製受控識別的資源識別碼

Screenshot showing Resource JSON screen with the managed identity resource ID highlighted.

將記錄資料擷取至 Azure 資料表 (選擇性)

若要將資料擷取至支援的 Azure 資料表

  1. 在資料收集規則中,變更 outputStream

    寄件者:"outputStream": "[concat('Custom-', parameters('tableName'))]"

    收件者:"outputStream": "outputStream": "[concat(Microsoft-', parameters('tableName'))]"

  2. transformKql定義轉換,將擷取的資料傳送至目的地 Azure 資料表中的目標資料行。

將事件中樞權限授與資料收集規則

透過受控識別,您可以授與權限給任何事件中樞或事件中樞命名空間,來將事件傳送至您建立的資料收集規則和資料收集端點。 當您將權限授與事件中樞命名空間時,命名空間中的所有事件中樞都會繼承該權限。

  1. 從 Azure 入口網站中的事件中樞或事件中樞命名空間,選取 [存取控制 (IAM)]>[新增角色指派]

    Screenshot that shows the Access control screen for the data collection rule.

  2. 選取 [Azure 事件中樞資料接收器],然後選取 [下一步]

    Screenshot that shows the Add Role Assignment screen for the event hub with the Azure Event Hubs Data Receiver role highlighted.

  3. 針對 [存取權指派對象] 選取 [受控識別],然後按一下 [選取成員]。 選取 [資料收集規則],依名稱搜尋資料收集規則,然後按一下 [選取]

    Screenshot that shows how to assign access to managed identity.

  4. 選取 [檢閱 + 指派],然後先確認詳細資料,再儲存您的角色指派。

    Screenshot that shows the Review and Assign tab of the Add Role Assignment screen.

將資料收集規則與事件中樞產生關聯

最後一個步驟是將資料收集規則與您要從中收集事件的事件中樞產生關聯。

您可以將單一資料收集規則與多個共用相同取用者群組的事件中樞產生關聯,並將資料擷取至相同的串流。 或者,您可以將唯一的資料收集規則與每個事件中樞產生關聯。

重要

您必須將至少一個資料收集規則與事件中樞產生關聯,才能從事件中樞擷取資料。 當您刪除與事件中樞相關的所有資料收集規則關聯時,您將停止從事件中樞擷取資料。

若要在 Azure 入口網站中建立資料收集規則關聯:

  1. 在 Azure 入口網站的搜尋方塊中鍵入「範本」,然後選取 [Deploy a custom template] \(部署自訂範本\)

  2. 選取 [在編輯器中組建您自己的範本]

  3. 將下方的 Resource Manager 範本貼至編輯器中,然後選取 [儲存]

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "eventHubResourceID": {
          "type": "string",
          "metadata": {
            "description": "Specifies the Azure resource ID of the event hub to use."
          }
        },
        "associationName": {
          "type": "string",
          "metadata": {
            "description": "The name of the association."
          }
        },
        "dataCollectionRuleID": {
          "type": "string",
          "metadata": {
            "description": "The resource ID of the data collection rule."
          }
        }
      },
      "resources": [
        {
          "type": "Microsoft.Insights/dataCollectionRuleAssociations",
          "apiVersion": "2021-09-01-preview",
          "scope": "[parameters('eventHubResourceId')]",
          "name": "[parameters('associationName')]",
          "properties": {
            "description": "Association of data collection rule. Deleting this association will break the data collection for this event hub.",
            "dataCollectionRuleId": "[parameters('dataCollectionRuleId')]"
          }
        }
      ]
    }
    
  4. 在 [自訂部署] 畫面上,指定 [訂用帳戶] 和 [資源群組] 以儲存資料收集規則關聯,然後提供在範本中定義的參數值,包括:

    • 區域 - 根據您選取的資源群組自動填入。
    • 事件中樞執行個體資源識別碼 - 請參閱收集必要資訊
    • 關聯名稱 - 為關聯指定名稱。
    • 資料收集規則識別碼 - 當您建立資料收集規則時產生。

    Screenshot showing the Custom Template Deployment screen with the deployment values for the data collection rule association set up in this tutorial.

  5. 檢閱詳細資料時,依序選取 [檢閱 + 建立] 和 [建立]

檢查目的地資料表中是否有內嵌的事件

現在您已將資料收集規則與事件中樞建立關聯,Azure 監視器記錄將會擷取保留期間尚未過期的現有事件和所有新事件。

若要檢查目的地資料表是否有擷取的事件:

  1. 瀏覽至您的工作區,然後選取 [記錄]

  2. 在查詢編輯器中撰寫簡單的查詢,然後選取 [執行]

    <table_name>
    

    您應該會看到來自事件中樞的事件。

Screenshot showing the results of a simple query on a custom table. The results consist of events ingested from an event hub.

清除資源

在本教學課程中,您已建立下列資源:

  • 自訂資料表
  • 資料收集端點
  • 資料收集規則
  • 資料收集規則關聯

評估您是否仍然需要這些資源。 個別刪除您不需要的資源,或刪除資源群組來一次刪除所有這些資源。 如果您繼續執行資源,則可能會產生費用。

若要停止從事件中樞擷取資料,請刪除與事件中樞相關的所有資料收集規則關聯或刪除資料收集規則本身。 這些動作也會重設事件中樞檢查點

已知問題與限制

  • 如果您在 Microsoft Entra 目錄之間轉移訂用帳戶,則必須遵循 Azure 資源受控識別已知問題中所述的步驟,以繼續擷取資料。
  • 您可以從事件中樞擷取最多 64 KB 的訊息到 Azure 監視器記錄。

下一步

進一步了解: