Skip to main content

switch

Conditionally processes messages based on their contents.

# Config fields, showing default values
label: ""
switch: []

For each switch case a Bloblang query is checked and, if the result is true (or the check is empty) the child processors are executed on the message.

Fields

[].check

A Bloblang query that should return a boolean value indicating whether a message should have the processors of this case executed on it. If left empty the case always passes. If the check mapping throws an error the message will be flagged as having failed and will not be tested against any other cases.

Type: string
Default: ""

# Examples
check: this.type == "foo"
check: this.contents.urls.contains("https://benthos.dev/")

[].processors

A list of processors to execute on a message.

Type: array
Default: []

[].fallthrough

Indicates whether, if this case passes for a message, the next case should also be executed.

Type: bool
Default: false

Examples

We have a system where we're counting a metric for all messages that pass through our system. However, occasionally we get messages from George where he's rambling about dumb stuff we don't care about.

For Georges messages we want to instead emit a metric that gauges how angry he is about being ignored and then we drop it.

pipeline:
processors:
- switch:
- check: this.user.name.first != "George"
processors:
- metric:
type: counter
name: MessagesWeCareAbout
- processors:
- metric:
type: gauge
name: GeorgesAnger
value: ${! json("user.anger") }
- bloblang: root = deleted()

Batching

When a switch processor executes on a batch of messages they are checked individually and can be matched independently against cases. During processing the messages matched against a case are processed as a batch, although the ordering of messages during case processing cannot be guaranteed to match the order as received.

At the end of switch processing the resulting batch will follow the same ordering as the batch was received. If any child processors have split or otherwise grouped messages this grouping will be lost as the result of a switch is always a single batch. In order to perform conditional grouping and/or splitting use the group_by processor.