🎫

Event Hubs から Log Analytics ワークスペースにログを連携したい

に公開

はじめに

Azure の各種ログは基本的に直接 Log Analytics ワークスペースに取り込むことが可能です。しかし、特定の要件に応じてカスタム ログの収集や標準テーブルのログをカスタム テーブルに取り込む必要が生じる場合があります。

そのような場合、Azure のログ ストリーミング サービスである Event Hubs を利用することで、Log Analytics ワークスペースにログを直接連携することが可能です。Event Hubs 用の追加コストは発生しますが、すべて PaaS で構成できるため、運用負荷を軽減できる点が大きな利点です。本記事では、この仕組みについて調査・検証した内容をご紹介します。

前提条件

詳細については、以下の公式ドキュメントをご参照ください(2025年9月時点でプレビュー機能です)。
特に注意すべき点として、Log Analytics ワークスペースを専用クラスターにアタッチするか、コミットメントプランを利用する必要があります。
また、サポートされているリージョンも事前にご確認ください。
https://learn.microsoft.com/ja-jp/azure/azure-monitor/logs/ingest-logs-event-hub

Azure Event Hubs から Azure Monitor ログにイベントを送信するには、次のリソースが必要です。

  • 少なくとも共同作成者権限を持っている Log Analytics ワークスペース。
  • Log Analytics ワークスペースを専用クラスターにリンクするか、コミットメント レベルを持っている必要があります。
  • パブリック ネットワーク アクセスを許可する Event Hubs 名前空間。 公衆ネットワーク アクセスが無効である場合、[信頼できる Microsoft サービスがこのファイアウォールをバイパスすることを許可] が [はい] に設定されていることを確認します。
  • イベントを含むイベント ハブ。 イベント ハブにイベントを送信するには、「Azure Event Hubs でのイベントの送受信のチュートリアル」の手順に従うか、Azure リソースの診断設定を構成します。

設定手順

カスタム テーブルの作成

以下の PowerShell スクリプトを用いて、カスタムテーブルを作成します。カラムは固定です。
その他のパラメータを適宜ご自身の環境に合わせて設定してください。

# パラメータの設定
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$resourcegroupName = "rg-monitor"
$workspaceName = "mylaws"
$tableName = "FromEHLog_CL"
$column = @{
    'TimeGenerated' = 'DateTime';
    'RawData' = 'string';
    'Properties' = 'dynamic';
}

# Azure へのサインイン
Connect-AzAccount -subscription $subscriptionId

# カスタムテーブルの作成
New-AzOperationalInsightsTable -ResourceGroupName $resourceGroupName -WorkspaceName $workspaceName -TableName $tableName -Column $column

データ収集エンドポイントの作成

データ収集エンドポイントの作成方法については、以下の公式ドキュメントをご参照ください。
https://learn.microsoft.com/ja-jp/azure/azure-monitor/data-collection/data-collection-endpoint-overview?tabs=portal#create-a-data-collection-endpoint

データ収集ルールの作成

カスタムテンプレートのデプロイからデータ収集ルールを作成します。また、必要に応じて変換ルールをカスタマイズすることも可能です。
※ Learn のドキュメントに記載の ARM テンプレートと同一内容です。

{
    "$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'))]"
                    }
                ]
            }
        }
    ]
}


Event Hubs へのアクセス許可設定

DCR のマネージド ID に対して、Event Hubs をスコープとした「Azure Event Hubs のデータ受信者」ロールを付与します。

EventHubs と DCR の紐づけ

Event Hubs と DCR を関連付けるには、以下のテンプレートを実行します。
※ Learn のドキュメントに記載の ARM テンプレートと同一内容です。

{
  "$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')]"
      }
    }
  ]
}


ログの確認

取り込まれたログは、RawData フィールドに格納されていることを確認できます。

まとめ

本記事では、Azure Event Hubs から Log Analytics ワークスペースのカスタムテーブルへログを取り込む手順についてご紹介しました。プレビューである点と専用クラスターもしくはコミットメント プランが必要である点がネックですが、こちらの構成で柔軟かつマネージドなログ収集基盤を構築できます。

Microsoft (有志)

Discussion