This post shows off a subtle, but very helpful new feature in Logic Apps that enhances the filtering of events from Azure Event Grid.
Background
When an Event Grid subscription is created, it has the option to apply filters against the subject and eventType properties of the message. At the time that events are published, Event Grid will evaluate the filters for each subscriber and subsequently send the event if there is a match. For reference, the structure of the Event Grid schema is as follows:
[ { "topic": string, "subject": string, "id": string, "eventType": string, "eventTime": string, "data":{ object-unique-to-each-publisher }, "dataVersion": string, "metadataVersion": string } ]
The subject field has two filter options:
- Prefix filter – This is a literal string match applied to the prefix of the subject field. A common example is subscribing to events from a storage account for a specific container (i.e. images or orders). The value for such a filter might look something like /blobServices/default/containers/images.
- Suffix filter – A literal string match applied to the suffix of the subject field. In the storage account example, this is most commonly used for file extensions, such as .png or .pdf.
The other property in the payload that can be evaluated for event subscriptions is eventType. This value will always be contextual to the topic that publishes the event. For example, storage accounts currently support two types: Microsoft.Storage.BlobCreated and Microsoft.Storage.BlobDeleted. As a result, event types will vary for each topic.
Filtering Event Types in Logic Apps
The latest addition to the Event Grid trigger in Logic Apps is filtering support for the event types directly within the designer. Let’s see how this will be used with a custom topic. For this scenario, we will assume that the payload resembles (taken from the custom events quickstart) the following:
[{ "id": "1807", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "2017-08-10T21:03:07+00:00", "data": { "make": "Ducati", "model": "Monster" }, "dataVersion": "1.0", "metadataVersion": "1", "topic": "/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.EventGrid/topics/{topic}" }]
Thanks to this new addition, if the workflow we want to invoke is only interested in the recordInserted type, then we can simply add the value to the list:
Going back to our storage account example. If the selected topic (resource type) is Microsoft.Storage.StorageAccounts, then the list of available types is populated for us. The screenshot below showcases the list of possible event types for a storage account as well as an example of how to apply relevant values to the prefix and suffix filters of the subject field:
While this might seem like a trivial feature, to truly appreciate it, we have to consider a few things:
- Previously, since there wasn’t a way to apply an event type filter to the trigger; we had to add additional conditions to the workflow for each event type that was applicable.
- Since there wasn’t a way to create a trigger that performed a match against the event type, it meant that our Logic Apps were being triggered for all the event types. Which could be considered wasteful if it only needed to be triggered for a subset of types.
Wrapping it up
Let’s add a few more steps to the Logic App to make it a bit more meaningful. In the end, I just want to add messages to a queue to inspect later. I’m a big fan of the Parse JSON action since it allows you to transform the data passed in into dynamic content that we can use throughout the application.
The action works great when you are able to provide it a sample payload. If this is feasible, it will take the sample, generate a schema, and provide you with the dynamic content. We can add this step right after the trigger and use the following sample payload to the generate the schema:
{ "make": "Ducati", "model": "Monster" }
Then, we can put a new message on a queue and reference the dynamic content. The end result looks like:
Great work by the Logic Apps team to add this much needed and requested feature. Thank you!
NICE!