Filtering Guide

Filters are required when using the EventProcessor.processor() decorator. They’re used to determine which processor is responsible for processing which event.

General Usage

Filters are just python dictionaries that you need to pass to the decorator when decorating processors. Here’s an example:

from typing import Any, Dict

from event_processor import EventProcessor, EventProcessorInvocationException


event_processor = EventProcessor()


@event_processor.processor({"top.mid.low": "nice", "top.other": Any})
def my_processor(event: Dict):
    return True

print(event_processor.invoke({"top": {"mid": {"low": "nice"}, "other": 1234}}))
True

You can specify any number of filters in the decorator, but there is an implicit AND between them, so ALL the filters need to match a particular event for your processor to be called.

Filter Keys

The key for a filter is just a string representation of a path inside the input event. This is best explained with an example, so imagine you have the following input event:

{"key": {"deeper": {"deepest": "something"}}}

The path to something is key.deeper.deepest. So, if you want to filter on the value associated with deepest, you can use this filter:

# Note that "interesting" should be the value that
# you expect to find at key.deeper.deepest.
{"key.deeper.deepest": "interesting"}

Filter Values

Once you can refer to a value in the event, it’s useful to make assertions on what that value should be. If you don’t care what the value is, but you just want the key to exist in the input dict, you can use the value typing.Any in your filter.

For example, the following filter will match any event that has a value at its specified key, regardless of what that value is:

from typing import Any

{"user.email": Any}

You might also want to filter on the actual value. In that case, just specify the value that should be present for your processor to be called like this:

{"user.role": "user"}

This filter could be used to only process events generated by users that have the “user” role, for example.

Match Everything

It can be really useful to have a default processor for any kind of event. For example, it can be used if an unexpected event is sent to be processed, and you don’t want to miss events. To do this, simply use an empty filter. For example, the following filter will match any event:

{}