|
| 1 | +--- |
| 2 | +title: Intercepting Messages |
| 3 | +displayed_sidebar: docsSidebar |
| 4 | +--- |
| 5 | +<!-- |
| 6 | +Copyright (c) 2005-2025 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 7 | +
|
| 8 | +All rights reserved. This program and the accompanying materials |
| 9 | +are made available under the terms of the under the Apache License, |
| 10 | +Version 2.0 (the "License”); you may not use this file except in compliance |
| 11 | +with the License. You may obtain a copy of the License at |
| 12 | +
|
| 13 | +https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | +Unless required by applicable law or agreed to in writing, software |
| 16 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +See the License for the specific language governing permissions and |
| 19 | +limitations under the License. |
| 20 | +--> |
| 21 | + |
| 22 | +# Intercepting Messages |
| 23 | + |
| 24 | +## Overview |
| 25 | + |
| 26 | +RabbitMQ provides a generic mechanism to intercept messages on the broker. |
| 27 | +Interception can occur at two stages: |
| 28 | +1. **Incoming messages** – intercepted when a message enters RabbitMQ, just before it is routed to [queues](./queues). |
| 29 | +2. **Outgoing messages** – intercepted when RabbitMQ delivers a message to a client, just before it is [converted](./conversions) to the target protocol. |
| 30 | + |
| 31 | +Interceptors are executed by one of the following [Erlang processes](https://www.erlang.org/doc/system/ref_man_processes.html): |
| 32 | +* AMQP 1.0 session |
| 33 | +* AMQP 0.9.1 [channel](./channels) |
| 34 | +* MQTT connection |
| 35 | + |
| 36 | +Messages sent over the [RabbitMQ Streams protocol](https://github.com/rabbitmq/rabbitmq-server/blob/main/deps/rabbitmq_stream/docs/PROTOCOL.adoc) are not intercepted. |
| 37 | + |
| 38 | +A message interceptor is an [Erlang module](https://www.erlang.org/doc/system/modules.html) that implements the [rabbit_msg_interceptor](https://github.com/rabbitmq/rabbitmq-server/blob/main/deps/rabbit/src/rabbit_msg_interceptor.erl) behaviour. |
| 39 | +What the interceptor does is entirely up to its implementation - it can validate message metadata, add annotations, or perform arbitrary side effects. |
| 40 | + |
| 41 | +Custom interceptors can be developed and integrated via [plugins](./plugins). |
| 42 | + |
| 43 | +RabbitMQ ships with several built-in message interceptors. |
| 44 | +Below are examples of how to configure them using the [rabbitmq.conf](./configure#config-file) file. |
| 45 | + |
| 46 | +## Incoming Message Interceptors |
| 47 | + |
| 48 | +### Timestamp |
| 49 | + |
| 50 | +This interceptor adds a timestamp to each incoming message: |
| 51 | + |
| 52 | +```ini |
| 53 | +message_interceptors.incoming.set_header_timestamp.overwrite = true |
| 54 | +``` |
| 55 | + |
| 56 | +* AMQP 1.0 and Streams clients receive a message annotation: `x-opt-rabbitmq-received-time` (timestamp in milliseconds since January 1, 1970 UTC). |
| 57 | +* AMQP 0.9.1 clients receive: |
| 58 | + * `timestamp_in_ms` header (milliseconds) for compatibility with the former [Message Timestamp Plugin](https://github.com/rabbitmq/rabbitmq-message-timestamp) |
| 59 | + * `timestamp` property (seconds) |
| 60 | + |
| 61 | +To preserve an existing `timestamp_in_ms` header, set `overwrite` to `false`: |
| 62 | +```ini |
| 63 | +message_interceptors.incoming.set_header_timestamp.overwrite = false |
| 64 | +``` |
| 65 | + |
| 66 | +### Routing Node |
| 67 | + |
| 68 | +This interceptor adds a message annotation `x-routed-by` indicating which RabbitMQ [node](./clustering#node-names) received and routed the message: |
| 69 | +```ini |
| 70 | +message_interceptors.incoming.set_header_routing_node.overwrite = true |
| 71 | +``` |
| 72 | + |
| 73 | +Set `overwrite` to `false` to preserve an existing value: |
| 74 | +```ini |
| 75 | +message_interceptors.incoming.set_header_routing_node.overwrite = false |
| 76 | +``` |
| 77 | + |
| 78 | +### MQTT client ID |
| 79 | + |
| 80 | +If the [MQTT plugin](./mqtt) is enabled, RabbitMQ can annotate incoming messages with the [client ID](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059) of the publishing MQTT client. |
| 81 | +This is done by adding a message annotation with the key `x-opt-mqtt-client-id`. |
| 82 | + |
| 83 | +```init |
| 84 | +mqtt.message_interceptors.incoming.set_client_id_annotation.enabled = true |
| 85 | +``` |
| 86 | + |
| 87 | +This annotation is visible to AMQP 1.0, AMQP 0.9.1, and Streams consumers. |
| 88 | +However, MQTT clients will not receive this annotation, as the MQTT spec does not allow arbitrary broker-added annotations. |
| 89 | + |
| 90 | +## Outgoing Message Interceptors |
| 91 | + |
| 92 | +### Timestamp |
| 93 | + |
| 94 | +This interceptor timestamps messages when they are sent to clients: |
| 95 | + |
| 96 | +```ini |
| 97 | +message_interceptors.outgoing.timestamp.enabled = true |
| 98 | +``` |
| 99 | + |
| 100 | +The annotation key is `x-opt-rabbitmq-sent-time`, and its value is a timestamp in milliseconds since January 1, 1970 UTC. |
0 commit comments