Skip to content

Message Interceptors #13760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 23, 2025
Merged

Message Interceptors #13760

merged 6 commits into from
Apr 23, 2025

Conversation

ansd
Copy link
Member

@ansd ansd commented Apr 16, 2025

This PR builds on top of #13641 contributed by @LoisSotoLopez and @gomoripeti.

This PR allows writing 3rd party plugins to intercept both incoming and outgoing messages for AMQP 1.0, AMQP 0.9.1, and MQTT.

This PR also ships two new optional built-in interceptors:

  1. Timestamps for outgoing messages.
  2. Setting client ID of publishing MQTT client.

Solves #10050
Solves #10051

Docs PR including exact description:
rabbitmq/rabbitmq-website#2246

Docs are live:
https://www.rabbitmq.com/docs/next/message-interceptors

@ansd ansd added this to the 4.2.0 milestone Apr 16, 2025
@ansd ansd mentioned this pull request Apr 16, 2025
12 tasks
@ansd ansd marked this pull request as ready for review April 16, 2025 11:03
@ansd ansd requested a review from kjnilsson April 16, 2025 11:04
@ansd ansd force-pushed the message-interceptors branch from 0a167d3 to 1565eab Compare April 16, 2025 11:42
@ansd ansd force-pushed the message-interceptors branch from 40a1659 to 12bf6ec Compare April 16, 2025 13:58
@ansd ansd requested a review from gomoripeti April 16, 2025 14:10
Copy link
Contributor

@gomoripeti gomoripeti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All look good to me!

("Only users with explicit access to this repository may approve ")

@ansd ansd force-pushed the message-interceptors branch from 12bf6ec to fe0401a Compare April 16, 2025 15:56
@ansd ansd marked this pull request as draft April 17, 2025 08:13
@mergify mergify bot added the make label Apr 17, 2025
@ansd ansd force-pushed the message-interceptors branch from eb5ec2a to d0b528d Compare April 17, 2025 16:27
ansd added a commit to rabbitmq/rabbitmq-website that referenced this pull request Apr 18, 2025
@ansd ansd marked this pull request as ready for review April 18, 2025 13:03
ansd added a commit to rabbitmq/rabbitmq-website that referenced this pull request Apr 22, 2025
LoisSotoLopez and others added 6 commits April 23, 2025 12:33
This commit enables users to provide custom message interceptor modules,
i.e. modules to process incoming and outgoing messages. The
`rabbit_message_interceptor` behaviour defines a `intercept/4` callback,
for those modules to implement.

Co-authored-by: Péter Gömöri <[email protected]>
1. Force the config for timestamp and routing node message interceptors
   to be configured with the overwrite boolean() to avoid defining
   multiple default values throughout the code.
2. Add type specs
3. Extend existing test case for new MQTT client ID interceptor
4. routing node and timestamp should only set the annotation for
   incoming_message_interceptors group
5. Fix `rabbitmq.conf`.
   Prior to this commit there were several issue:
   a.) Setting the right configuration was too user unfriendly, e.g. the user has to set
   ```
   message_interceptor.incoming.rabbit_mqtt_message_interceptor_client_id.annotation_key = x-opt-mqtt-client-id
   ```
   just to enable the MQTT message interceptor.
   b.) The code that parses was too difficult to understand
   c.) MQTT plugin was setting the env for app rabbit, which is an anti-pattern
   d.) disabling a plugin (e.g. MQTT), left its message interceptors still in place
   This is now all fixed, the user sets the rabbitmq.conf as follows:
   ```
   message_interceptors.incoming.set_header_timestamp.overwrite = true
   message_interceptors.incoming.set_header_routing_node.overwrite = false
   mqtt.message_interceptors.incoming.set_client_id_annotation.enabled = true
   ```
   Note that the first two lines use the same format as for RabbitMQ 4.0
   for backwards compatiblity. The last line (MQTT) follows a similar
   pattern.
It's a tradeoff between building the map for each incoming and outgoing
message (now that there are also outgoing interceptors) vs increased
memory usage for the MQTT proc state.

Connecting with MQTT 5.0 and client ID "xxxxxxxx", the number of words
are 201 before this commit vs 235 after this commit as determined by:
```
S = sys:get_state(MQTTConnectionPid),
erts_debug:size(S).
```
Therefore, this commit requires 34 word * 8 bytes = 272 bytes more per MQTT
connection, that is 272 MB more for 1,000,000 MQTT connections.
Intercept outgoing message just before conversion to target protocol as
this will give most flexibility to 3rd party plugins.
@ansd ansd force-pushed the message-interceptors branch from 80b0db8 to 69c2855 Compare April 23, 2025 10:33
@ansd ansd merged commit 77e73de into main Apr 23, 2025
273 checks passed
@ansd ansd deleted the message-interceptors branch April 23, 2025 12:01
ansd added a commit to rabbitmq/rabbitmq-website that referenced this pull request Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants