針對事件格線篩選事件

本文示範如何在建立事件格線訂用帳戶期間篩選事件。 若要了解適用於事件篩選的選項,請參閱了解適用於事件格線訂用帳戶的事件篩選

依事件類型進行篩選

建立事件格線訂用帳戶時,您可以指定要將哪些事件類型傳送至端點。 本節中的範例會為資源群組建立事件訂閱,但會限制傳送至 Microsoft.Resources.ResourceWriteFailureMicrosoft.Resources.ResourceWriteSuccess 的事件。 如果您在依事件類型篩選事件時需要更多彈性,請參閱依運算子和資料進行篩選

Azure PowerShell

針對 PowerShell,請在建立訂閱時使用 -IncludedEventType 參數。

$includedEventTypes = "Microsoft.Resources.ResourceWriteFailure", "Microsoft.Resources.ResourceWriteSuccess"

New-AzEventGridSubscription `
  -EventSubscriptionName demoSubToResourceGroup `
  -ResourceGroupName myResourceGroup `
  -Endpoint <endpoint-URL> `
  -IncludedEventType $includedEventTypes

Azure CLI

針對 Azure CLI,請使用 --included-event-types 參數。 下列範例會在 Bash 殼層中使用 Azure CLI:

includedEventTypes="Microsoft.Resources.ResourceWriteFailure Microsoft.Resources.ResourceWriteSuccess"

az eventgrid event-subscription create \
  --name demoSubToResourceGroup \
  --resource-group myResourceGroup \
  --endpoint <endpoint-URL> \
  --included-event-types $includedEventTypes

Azure 入口網站

在建立系統主題的事件訂用帳戶時,請使用下拉式清單選取事件類型,如下圖所示。

Screenshot showing the Create Subscription page with event types selected.

針對系統主題的現有訂用帳戶,請使用 [事件訂用帳戶] 頁面的 [篩選] 索引標籤,如下圖所示。

Screenshot showing the Event Subscription page with the Filters tab selected.

您可以透過選取 [新增事件類型] 連結,在建立 [自訂主題] 時指定篩選,如下圖所示。

Screenshot showing the Create Event Subscription page for a custom topic.

若要指定自訂主題現有訂用帳戶的篩選,請使用 [事件訂用帳戶] 頁面中的 [篩選] 索引標籤。

Screenshot of the Event Subscription page with Add Event Type button selected.

Azure Resource Manager 範本

針對 Resource Manager 範本,請使用 includedEventTypes 屬性。

"resources": [
  {
    "type": "Microsoft.EventGrid/eventSubscriptions",
    "name": "[parameters('eventSubName')]",
    "apiVersion": "2018-09-15-preview",
    "properties": {
      "destination": {
        "endpointType": "WebHook",
        "properties": {
          "endpointUrl": "[parameters('endpoint')]"
        }
      },
      "filter": {
        "subjectBeginsWith": "",
        "subjectEndsWith": "",
        "isSubjectCaseSensitive": false,
        "includedEventTypes": [
          "Microsoft.Resources.ResourceWriteFailure",
          "Microsoft.Resources.ResourceWriteSuccess"
        ]
      }
    }
  }
]

注意

若要深入了解這些篩選條件 (事件類型、主旨和進階),請參閱了解事件方格訂用帳戶的事件篩選

依主旨進行篩選

您可以依事件資料中的主旨來篩選事件。 您可以指定要符合主旨開頭或結尾的值。 如果您在依主旨篩選事件時需要更多彈性,請參閱依運算子和資料進行篩選

在下列 PowerShell 範例中,您會建立能依主旨開頭進行篩選的事件訂閱。 您會使用 -SubjectBeginsWith 參數來將事件限制為適用於特定資源的事件。 您會傳遞網路安全性群組的資源識別碼。

Azure PowerShell

$resourceId = (Get-AzResource -ResourceName demoSecurityGroup -ResourceGroupName myResourceGroup).ResourceId

New-AzEventGridSubscription `
  -Endpoint <endpoint-URL> `
  -EventSubscriptionName demoSubscriptionToResourceGroup `
  -ResourceGroupName myResourceGroup `
  -SubjectBeginsWith $resourceId

下一個 PowerShell 範例會建立適用於 Blob 儲存體的訂閱。 它會將事件限制為具有以 .jpg 為結尾之主旨的事件。

$storageId = (Get-AzStorageAccount -ResourceGroupName myResourceGroup -AccountName $storageName).Id

New-AzEventGridSubscription `
  -EventSubscriptionName demoSubToStorage `
  -Endpoint <endpoint-URL> `
  -ResourceId $storageId `
  -SubjectEndsWith ".jpg"

Azure CLI

在下列 Azure CLI 範例中,您會建立能依主旨開頭進行篩選的事件訂閱。 您會使用 --subject-begins-with 參數來將事件限制為適用於特定資源的事件。 您會傳遞網路安全性群組的資源識別碼。

resourceId=$(az network nsg show -g myResourceGroup -n demoSecurityGroup --query id --output tsv)

az eventgrid event-subscription create \
  --name demoSubscriptionToResourceGroup \
  --resource-group myResourceGroup \
  --endpoint <endpoint-URL> \
  --subject-begins-with $resourceId

下一個 Azure CLI 範例會建立適用於 Blob 儲存體的訂閱。 它會將事件限制為具有以 .jpg 為結尾之主旨的事件。

storageid=$(az storage account show --name $storageName --resource-group myResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --resource-id $storageid \
  --name demoSubToStorage \
  --endpoint <endpoint-URL> \
  --subject-ends-with ".jpg"

Azure 入口網站

針對現有的事件訂用帳戶:

  1. 在 [事件訂閱] 頁面上,選取 [啟用主旨篩選]

  2. 輸入下列一或多個欄位的值:[主旨開頭為] 和 [主旨結尾為]。 在下列選項中,會選取這兩個選項。

    Screenshot of Event Subscription page with subject filtering example.

  3. 如果您希望事件的主旨符合指定的篩選大小寫,請選取 [區分大小寫的主旨比對] 選項。

建立事件訂用帳戶時,使用建立精靈上的 [篩選] 索引標籤。

Screenshot of Create Event Subscription page with the Filters tab selected.

Azure Resource Manager 範本

在下列 Resource Manager 範本範例中,您會建立能依主旨開頭進行篩選的事件訂閱。 您會使用 subjectBeginsWith 屬性來將事件限制為適用於特定資源的事件。 您會傳遞網路安全性群組的資源識別碼。

"resources": [
  {
    "type": "Microsoft.EventGrid/eventSubscriptions",
    "name": "[parameters('eventSubName')]",
    "apiVersion": "2018-09-15-preview",
    "properties": {
      "destination": {
        "endpointType": "WebHook",
        "properties": {
          "endpointUrl": "[parameters('endpoint')]"
        }
      },
      "filter": {
        "subjectBeginsWith": "[resourceId('Microsoft.Network/networkSecurityGroups','demoSecurityGroup')]",
        "subjectEndsWith": "",
        "isSubjectCaseSensitive": false,
        "includedEventTypes": [ "All" ]
      }
    }
  }
]

下一個 Resource Manager 範本範例會建立適用於 Blob 儲存體的訂閱。 它會將事件限制為具有以 .jpg 為結尾之主旨的事件。

"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts/providers/eventSubscriptions",
    "name": "[concat(parameters('storageName'), '/Microsoft.EventGrid/', parameters('eventSubName'))]",
    "apiVersion": "2018-09-15-preview",
    "properties": {
      "destination": {
        "endpointType": "WebHook",
        "properties": {
          "endpointUrl": "[parameters('endpoint')]"
        }
      },
      "filter": {
        "subjectEndsWith": ".jpg",
        "subjectBeginsWith": "",
        "isSubjectCaseSensitive": false,
        "includedEventTypes": [ "All" ]
      }
    }
  }
]

注意

若要深入了解這些篩選條件 (事件類型、主旨和進階),請參閱了解事件方格訂用帳戶的事件篩選

依運算子和資料進行篩選

若要以更有彈性的方式進行篩選,您可以使用運算子和資料屬性來篩選事件。

搭配進階篩選進行訂閱

若要了解可用來進行進階篩選的運算子和索引鍵,請參閱進階篩選

這些範例會建立自訂主題。 它們會訂閱自訂主題,並依資料物件中的值進行篩選。 系統會將色彩屬性設為藍色、紅色或綠色的事件傳送到訂閱。

Azure PowerShell

對於 PowerShell,請使用:

$topicName = <your-topic-name>
$endpointURL = <endpoint-URL>

New-AzResourceGroup -Name gridResourceGroup -Location eastus2
New-AzEventGridTopic -ResourceGroupName gridResourceGroup -Location eastus2 -Name $topicName

$topicid = (Get-AzEventGridTopic -ResourceGroupName gridResourceGroup -Name $topicName).Id

$expDate = '<mm/dd/yyyy hh:mm:ss>' | Get-Date
$AdvFilter1=@{operator="StringIn"; key="Data.color"; Values=@('blue', 'red', 'green')}

New-AzEventGridSubscription `
  -ResourceId $topicid `
  -EventSubscriptionName <event_subscription_name> `
  -Endpoint $endpointURL `
  -ExpirationDate $expDate `
  -AdvancedFilter @($AdvFilter1)

Azure CLI

對於 Azure CLI,請使用:

topicName=<your-topic-name>
endpointURL=<endpoint-URL>

az group create -n gridResourceGroup -l eastus2
az eventgrid topic create --name $topicName -l eastus2 -g gridResourceGroup

topicid=$(az eventgrid topic show --name $topicName -g gridResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --source-resource-id $topicid \
  -n demoAdvancedSub \
  --advanced-filter data.color stringin blue red green \
  --endpoint $endpointURL \
  --expiration-date "<yyyy-mm-dd>"

請留意到已針對訂閱設定到期日

Azure 入口網站

  1. 在 [事件訂閱] 頁面上,選取 [進階篩選條件] 區段中的 [新增篩選條件]

    Screenshot showing the Event Subscription page with Add new filter link highlighted.

  2. 指定要比較的索引鍵、運算子和一或多個值。 在下列範例中,data.color 當作索引鍵使用,String is in 當作運算子使用,而 [藍色]、[紅色] 和 [綠色] 值則是指定的值。

    Screenshot showing an example of an advanced filter.

    注意

    若要深入了解進階篩選條件,請參閱了解適用於事件方格訂用帳戶的事件篩選

測試篩選條件

若要測試篩選,請以將色彩欄位設為綠色的方式傳送事件。 因為綠色是篩選的其中一個值,所以會將事件傳遞到端點。

Azure PowerShell

對於 PowerShell,請使用:

$endpoint = (Get-AzEventGridTopic -ResourceGroupName gridResourceGroup -Name $topicName).Endpoint
$keys = Get-AzEventGridTopicKey -ResourceGroupName gridResourceGroup -Name $topicName

$eventID = Get-Random 99999
$eventDate = Get-Date -Format s

$htbody = @{
    id= $eventID
    eventType="recordInserted"
    subject="myapp/vehicles/cars"
    eventTime= $eventDate
    data= @{
        model="SUV"
        color="green"
    }
    dataVersion="1.0"
}

$body = "["+(ConvertTo-Json $htbody)+"]"

Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $keys.Key1}

若要測試事件未傳送的案例,請以將色彩欄位設為黃色的方式傳送該事件。 由於訂閱中並沒有指定黃色的值,因此系統並不會將該事件傳送至您的訂用帳戶。

$htbody = @{
    id= $eventID
    eventType="recordInserted"
    subject="myapp/vehicles/cars"
    eventTime= $eventDate
    data= @{
        model="SUV"
        color="yellow"
    }
    dataVersion="1.0"
}

$body = "["+(ConvertTo-Json $htbody)+"]"

Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $keys.Key1}

Azure CLI

對於 Azure CLI,請使用:

topicEndpoint=$(az eventgrid topic show --name $topicName -g gridResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $topicName -g gridResourceGroup --query "key1" --output tsv)

event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/cars", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "model": "SUV", "color": "green"},"dataVersion": "1.0"} ]'

curl -X POST -H "aeg-sas-key: $key" -d "$event" $topicEndpoint

若要測試事件未傳送的案例,請以將色彩欄位設為黃色的方式傳送該事件。 由於訂閱中並沒有指定黃色的值,因此系統並不會將該事件傳送至您的訂用帳戶。

對於 Azure CLI,請使用:

event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/cars", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "model": "SUV", "color": "yellow"},"dataVersion": "1.0"} ]'

curl -X POST -H "aeg-sas-key: $key" -d "$event" $topicEndpoint

下一步

若要深入了解篩選條件 (事件類型、主旨和進階),請參閱了解事件方格訂用帳戶的事件篩選