0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Azure】Bicepを使ってデータ収集ルール(カスタムテキストログ)を構成する

Last updated at Posted at 2025-01-30

初めに

皆さんこんにちは、データ収集ルール(カスタムテキストログ)はAzurePortalからはあまり知識が無くてもなんとなく上手く構築できてしましまいますが、Bicepで書いていく場合は少しだけ深い知見が必要になります。とはいってもそれほど難しくはないので、一緒に学んでいきましょう。

目次

コードの構成

まずはコードの全体像を見てみましょう。

コードの全体像
resource dataCollectionRules 'Microsoft.Insights/dataCollectionRules@2022-06-01' = {
  name: <リソース名>
  location: <リソースの場所(リソースグループのリージョン)>
  tags: {
    <タグを記載>
  }
  kind: <Linux or Windows>
  properties: {
    dataCollectionEndpointId: <このルールを使用できるデータ収集エンドポイントのリソースID>
    streamDeclarations: {
      <"Custom-" + ストリーム名>: {
        columns: [
          {
            name: <列名>
            type: <列の形式>
          }
          ・・・
        ]
      }
    }
    dataSources: {
      logFiles: [
        {
          name: <データソースのフレンドリ名>
          streams: [
            <"Custom-" + ストリーム名>
          ]
          filePatterns: [
            <取得したいログファイルのパス>
          ]
          format: 'text'
          settings: {
            text: {
              recordStartTimestampFormat: <タイムスタンプのフォーマット>
            }
          }
        }
      ]
    }
    destinations: {
      logAnalytics: [
        {
          name: <宛先のフレンドリ名>
          workspaceResourceId: <格納先のワークスペースのリソースID>
       }
      ]
    }
    dataFlows: [
      {
        streams: [
          <このデータフローのストリームの一覧>
        ]
        destinations: [
          <このデータフローの宛先の一覧>
        ]
        transformKql:<ログの変換用KQL>
        outputStream:<"Custom-" + 保存先のテーブル名>
      }
    ]
  }
}

上のコードを見てわかる通り、コードは以下の4つから構成されます。

それぞれの関係はこんな感じ見たいです。参考:公式ドキュメント
image.png

ここで表記されている2種類のデータ「Known data」と「Custom data」があります。
公式ドキュメントの説明に解説が載っていますが、やや理解が難しいですね。私の解釈は以下表に記載されているデータソースのログは「Known data」と思っています。それ以外が「Cutom data」と思っていますが、扱ったことが無いです。
image.png

streamDeclarations

streamDeclarations
    streamDeclarations: {
      <"Custom-" + ストリーム名>: {
        columns: [
          {
            name: <列名>
            type: <列の形式>
          }
          ・・・
        ]
      }
    }
  • 実はカスタムテキストログ収集においては省略可能
  • 先頭名が"Custom-"となる必要がある
  • 形式は送信先のテーブルと一致する必要はない
  • 後の「dataFlows」で「destinations」と突き合わせする

dataSources

dataSources
    dataSources: {
      logFiles: [
        {
          name: <データソースのフレンドリ名>
          streams: [
            <"Custom-" + ストリーム名>
          ]
          filePatterns: [
            <取得したいログファイルのパス>
          ]
          format: 'text'
          settings: {
            text: {
              recordStartTimestampFormat: <タイムスタンプのフォーマット>
            }
          }
        }
      ]
    }
  • ここでデータソースの種類を指定
  • カスタムテキストログを使用する時は「LogFiles(MS公式ドキュメント|LogFilesDataSource)」を選択
  • 「Streams:」には'"Custom-" + ストリーム名'を記載
  • 「filePatterns:」には、取得先のファイルパスを記載。AzurePortalでは以下の部分。「*」等の正規表現が使用できます。
    image.png
  • LogFilesでは「format:」には'text'を記載
  • 「recordStartTimestampFormat:」はAzurePortalでは以下の部分。
    image.png
  • 「End of Line」を指定したい場合は「recordStartTimestampFormat:」に'ISO 8061'を指定してください。
End of Lineの指定
          settings: {
            text: {
              recordStartTimestampFormat: ISO 8061
            }

警告:2025/1/30 11:00
現在、上記の通りISO 8061に設定をしてもAzurePortal上では「End of Line」と表示されない不具合が発生しています。表示だけの問題であり、機能には影響ないとMSから回答をもらって

destinations

destinations
    destinations: {
      logAnalytics: [
        {
          name: <宛先のフレンドリ名>
          workspaceResourceId: <格納先のワークスペースのリソースID>
       }
      ]
    }

dataFlows

dataFlows
    dataFlows: [
      {
        streams: [
          <このデータフローのストリームの一覧>
        ]
        destinations: [
          <このデータフローの宛先の一覧>
        ]
        transformKql:<ログの変換用KQL>
        outputStream:<"Custom-" + 保存先のテーブル名>
      }
    ]
  • 「streamDeclarations」で宣言していた場合は、ここで「Destination」と紐づけや変換処理を行う

  • 「streams:」には、streamDeclarationsで宣言した名前を記載。

  • 「streamDeclarations」を省略した場合は、「dataSources」の「Streams」に記載の名前を記載。

  • 「destinations:」には、destinationsで宣言した名前を記載

  • 「transformKQL:」には変換式を記載。AzurePortal画面では以下
    image.png

  • 「outputStream:」には'"Custom-" + 保存先のテーブル名'を記載

コードサンプル

以下にコードサンプルを載せてしておきます。

サンプル(Destinationを省略)
  param name string = 'dcr-customtextlogs'
  param location string = 'japaneast'
  param kind string = 'Linux'
  param dataCollectionEndpointId string = '/subscriptions/<サブスクリプション名>/resourceGroups/<リソースグループ名>/providers/Microsoft.Insights/dataCollectionEndpoints/<データ収集エンドポイント名>'
  param dataSources_name string = 'CustomtextDataSource'
  param streams string = 'Custom-demoStream'//ここにストリーム名
  param filePatterns string = '/var/log/demo.log'//ログ取得先のパスを記載
  param transformKql string = 'source| extend TimeGenerated = now()'//TransformKQLを記載
  param format string = 'text'
  param destinations_name string = 'demo-workspace'
  param workspace string = '/subscriptions/<サブスクリプション名>/resourcegroups/<リソースグループ名>/providers/microsoft.operationalinsights/workspaces/<ワークスペース名>'
  param dataFlows_destinations string = 'demo-workspace'//ワークスペース名
  param timeformats string = 'ISO 8601'
  param outputstreams string = 'Custom-demo_CL'//ここにワークスペース名

  resource dataCollectionRules 'Microsoft.Insights/dataCollectionRules@2022-06-01' = {
    name: name
    location: location
    kind: kind
    properties: {
      dataCollectionEndpointId: dataCollectionEndpointId
      dataSources: {
        logFiles: [
          {
            name: dataSources_name 
            streams: [
              streams
            ]
            filePatterns: [
              filePatterns
            ]
            format: format
            settings: {
              text: {
                recordStartTimestampFormat: timeformats
              }
            }
          }
        ]
      }
      destinations: {
        logAnalytics: [
          {
            name: destinations_name
            workspaceResourceId: workspace
        }
        ]
      }
      dataFlows: [
        {
          streams: [
            streams 
          ]
          destinations: [
            dataFlows_destinations 
          ]
          transformKql:transformKql
          outputStream:outputstreams
        }
      ]
    }
  }
サンプル(Destinationを定義)
param name string = 'dcr-customtextlogs'
param location string = 'japaneast'
param kind string = 'Linux'
param dataCollectionEndpointId string = '/subscriptions/<サブスクリプション名>/resourceGroups/<リソースグループ名>/providers/Microsoft.Insights/dataCollectionEndpoints/<データ収集エンドポイント名>'
param dataSources_name string = 'CustomtextDataSource'
param streams string = 'Custom-demoStream'//ここにストリーム名
param filePatterns string = '/var/log/demo.log'//ログ取得先のパスを記載
param transformKql string = 'source| extend TimeGenerated = now()'//TransformKQLを記載
param format string = 'text'
param destinations_name string = 'demo-workspace'
param workspace string = '/subscriptions/<サブスクリプション名>/resourcegroups/<リソースグループ名>/providers/microsoft.operationalinsights/workspaces/<ワークスペース名>'
param dataFlows_streams string = 'Custom-demoStream'//ここにストリーム名
param dataFlows_destinations string = 'demo-workspace'//ワークスペース名
param timeformats string = 'ISO 8601'
param outputstreams string = 'Custom-demo_CL'//ここにワークスペース名


resource dataCollectionRules 'Microsoft.Insights/dataCollectionRules@2022-06-01' = {
  name: name
  location: location
  kind: kind 
  properties: {
    dataCollectionEndpointId: dataCollectionEndpointId
    streamDeclarations: {
      'Custom-demoStream': {
        columns: [
          {
            name: 'Time'
            type: 'datetime'
          }
          {
            name: 'Row1'
            type: 'string'
          }
          {
            name: 'Row2'
            type: 'string'
          }
          {
            name: 'Row3'
            type: 'string'
          }
        ]
      }
    }
    dataSources: {
      logFiles: [
        {
          name: dataSources_name 
          streams: [
            streams
          ]
          filePatterns: [
            filePatterns
          ]
          format: format
          settings: {
            text: {
              recordStartTimestampFormat: timeformats
            }
          }
        }
      ]
    }
    destinations: {
      logAnalytics: [
        {
          name: destinations_name
          workspaceResourceId: workspace 
       }
      ]
    }
    dataFlows: [
      {
        streams: [
          dataFlows_streams
        ]
        destinations: [
          dataFlows_destinations
        ]
        transformKql:transformKql
        outputStream:outputstreams
      }
    ]
  }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?