Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Azure Event Grid WebHooks (Part 2): Filtering

DZone's Guide to

Azure Event Grid WebHooks (Part 2): Filtering

In our next lesson on Azure Event Grid's WebHooks, we see how to use filtering to ensure subscribers only get the alerts they want.

· Cloud Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

In my previous post, I introduced Azure Event Grid. I demonstrated how simple it is to use Event Grid to push hundreds of events to subscribers using WebHooks.

In today's post, I'll show off a powerful capability of Event Grid — filters.

What Are Filters?

Subscribing to a topic means that all events pushed to this topic will be pushed to the subscriber. But what if the subscriber is only interested in a subset of the events? For example, in my previous post, I created a blog topic, and all subscribers to that topic received notifications about new and updated blog posts, new comments, etc. But some subscribers might be interested only in posts and might want to ignore comments. Instead of creating multiple topics for each type of event, which will require separate subscriptions, Event Grid has the concept of filters. Filters are applied to the event content and will only be pushed to subscribers with matching filters.

The following diagram demonstrates this capability:

Image title

Azure Event Grid supports two types of filters:

  • Subject prefix and suffix filters.
  • Event type filters.

Subject Prefix and Suffix Filters

In this example, I'll use a prefix filter to receive only events with the subject starting with "post" using the --subject-begins-with post parameter.

az eventgrid topic event-subscription create --name postsreceiver --subject-begins-with post --endpoint https://twzm3c5ry2.execute-api.ap-southeast-2.amazonaws.com/prod/post -g rg --topic-name blog


Similarly:

az eventgrid topic event-subscription create --name commenstreceiver --subject-begins-with comment  --endpoint https://twzm3c5ry2.execute-api.ap-southeast-2.amazonaws.com/prod/comment -g rg --topic-name blog


An event that looks like...

[
    {
        "id": "2134",
        "eventType": "new",
        "subject": "comments",
        "eventTime": "2017-08-20T23:14:22+1000",
        "data":{
            "content": "Azure Event Grid",
            "postId": "123"
        }
    }
]


...will only be pushed to the second subscriber because it matches the filter.

Filtering Based on Event Type

Another way for the subscriber to filter the pushed message is by specifying event types. By default, when a new subscription is added the subscriber filter data looks like

"filter": {                       
    "includedEventTypes": [         
        "All"                         
    ],                              
    "isSubjectCaseSensitive": null, 
    "subjectBeginsWith": "", 
    "subjectEndsWith": ""


The includedEventTypes attribute is equal to "All", which means that the subscriber will get all events regardless the type.

You can filter multiple event types as space separated values using the --included-event-types parameter:

az eventgrid topic event-subscription create --name newupdatedreceiver --included-event-types new updated --endpoint https://twzm3c5ry2.execute-api.ap-southeast-2.amazonaws.com/prod/newupdated -g rg --topic-name blog


For example, that results in:

"filter": {                     
    "includedEventTypes": [       
        "new",                      
        "updated"                   
    ],                            
    "isSubjectCaseSensitive": null,
    "subjectBeginsWith": "",      
    "subjectEndsWith": ""  


Which means that only events with type "new" or "updated" will be pushed to this subscriber. This event won't be pushed:

[
    {
        "id": "123456",
        "eventType": "deleted",
        "subject": "posts",
        "eventTime": "2017-08-20T23:14:22+1000",
        "data":{
            "postId": "123"
        }
    }
]

Summary

Enabling the subscriber to have control over which events it will receive based on subject prefix, suffix, or event type (and a mix of these options) is a powerful capability of Azure Event Grid. Routing events in a declarative way without writing any logic in the event source side significantly simplifies this scenario.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:
webhooks ,azure event grid ,microsoft azure ,filters ,cloud ,tutorial

Published at DZone with permission of Hesham Amin. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}