-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Incoming message interceptors #13641
Open
LoisSotoLopez
wants to merge
11
commits into
rabbitmq:main
Choose a base branch
from
cloudamqp:message-interceptors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+308
−75
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bc566d3
Add incoming message interceptors
LoisSotoLopez d7877d4
Remove behaviour & use context map
LoisSotoLopez 359b60a
Adapt rabbit.snippets to new config format
LoisSotoLopez c52fb11
Fix amqp_client & rabbit.snippet tests
LoisSotoLopez da7e9c4
Apply PR suggestions
LoisSotoLopez 78d80ec
Merge branch 'main' into message-interceptors
LoisSotoLopez 27621c5
Add schema tests
LoisSotoLopez 11a14e5
Put proto value in interceptor ctx map
LoisSotoLopez e9042b0
Build msg interceptor ctx on msg arrival for mqtt
LoisSotoLopez 38a2188
Revert changes to rabbit_reader.erl
LoisSotoLopez 1cc8697
Minor fixes on ctx map protocol field
LoisSotoLopez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,49 @@ | ||
%% This Source Code Form is subject to the terms of the Mozilla Public | ||
%% License, v. 2.0. If a copy of the MPL was not distributed with this | ||
%% file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
%% | ||
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
|
||
%% This module exists since 3.12 replacing plugins rabbitmq-message-timestamp | ||
%% and rabbitmq-routing-node-stamp. Instead of using these plugins, RabbitMQ core can | ||
%% now be configured to add such headers. This enables non-AMQP 0.9.1 protocols (that | ||
%% do not use rabbit_channel) to also add AMQP 0.9.1 headers to incoming messages. | ||
-module(rabbit_message_interceptor). | ||
-include("mc.hrl"). | ||
|
||
-export([intercept/1]). | ||
-export([intercept/3, | ||
set_msg_annotation/4]). | ||
|
||
-type protocol() :: amqp091 | amqp10 | mqtt310 | mqtt311 | mqtt50. | ||
|
||
-define(HEADER_TIMESTAMP, <<"timestamp_in_ms">>). | ||
-define(HEADER_ROUTING_NODE, <<"x-routed-by">>). | ||
-type msg_interceptor_ctx() :: #{protocol := protocol(), | ||
vhost := binary(), | ||
username := binary(), | ||
conn_name => binary(), | ||
atom() => term()}. | ||
|
||
-spec intercept(mc:state()) -> mc:state(). | ||
intercept(Msg) -> | ||
Interceptors = persistent_term:get(incoming_message_interceptors, []), | ||
lists:foldl(fun({InterceptorName, Overwrite}, M) -> | ||
intercept(M, InterceptorName, Overwrite) | ||
end, Msg, Interceptors). | ||
-callback intercept(Msg, MsgInterceptorCtx, Group, Config) -> Msg when | ||
Msg :: mc:state(), | ||
MsgInterceptorCtx :: msg_interceptor_ctx(), | ||
Group :: incoming_message_interceptors | outgoing_message_interceptors, | ||
Config :: #{atom() := term()}. | ||
|
||
intercept(Msg, set_header_routing_node, Overwrite) -> | ||
Node = atom_to_binary(node()), | ||
set_annotation(Msg, ?HEADER_ROUTING_NODE, Node, Overwrite); | ||
intercept(Msg0, set_header_timestamp, Overwrite) -> | ||
Ts = mc:get_annotation(?ANN_RECEIVED_AT_TIMESTAMP, Msg0), | ||
Msg = set_annotation(Msg0, ?HEADER_TIMESTAMP, Ts, Overwrite), | ||
set_timestamp(Msg, Ts, Overwrite). | ||
-spec intercept(Msg, MsgInterceptorCtx, Group) -> Msg when | ||
Msg :: mc:state(), | ||
MsgInterceptorCtx :: map(), | ||
Group :: incoming_message_interceptors | outgoing_message_interceptors. | ||
intercept(Msg, MsgInterceptorCtx, Group) -> | ||
Interceptors = persistent_term:get(Group, []), | ||
lists:foldl(fun({Module, Config}, Msg0) -> | ||
try | ||
Module:intercept(Msg0, | ||
MsgInterceptorCtx, | ||
Group, | ||
Config) | ||
catch | ||
error:undef -> | ||
Msg0 | ||
end | ||
end, Msg , Interceptors). | ||
|
||
-spec set_annotation(mc:state(), mc:ann_key(), mc:ann_value(), boolean()) -> mc:state(). | ||
set_annotation(Msg, Key, Value, Overwrite) -> | ||
-spec set_msg_annotation(mc:state(), | ||
mc:ann_key(), | ||
mc:ann_value(), | ||
boolean() | ||
) -> mc:state(). | ||
set_msg_annotation(Msg, Key, Value, Overwrite) -> | ||
case {mc:x_header(Key, Msg), Overwrite} of | ||
{Val, false} when Val =/= undefined -> | ||
Msg; | ||
_ -> | ||
mc:set_annotation(Key, Value, Msg) | ||
end. | ||
|
||
-spec set_timestamp(mc:state(), pos_integer(), boolean()) -> mc:state(). | ||
set_timestamp(Msg, Timestamp, Overwrite) -> | ||
case {mc:timestamp(Msg), Overwrite} of | ||
{Ts, false} when is_integer(Ts) -> | ||
Msg; | ||
_ -> | ||
mc:set_annotation(?ANN_TIMESTAMP, Timestamp, Msg) | ||
end. |
16 changes: 16 additions & 0 deletions
16
deps/rabbit/src/rabbit_message_interceptor_routing_node.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-module(rabbit_message_interceptor_routing_node). | ||
-behaviour(rabbit_message_interceptor). | ||
|
||
-define(HEADER_ROUTING_NODE, <<"x-routed-by">>). | ||
|
||
-export([ | ||
intercept/4 | ||
]). | ||
|
||
intercept(Msg, _MsgInterceptorCtx, _Group, Config) -> | ||
Node = atom_to_binary(node()), | ||
Overwrite = maps:get(overwrite, Config, false), | ||
rabbit_message_interceptor:set_msg_annotation(Msg, | ||
?HEADER_ROUTING_NODE, | ||
Node, | ||
Overwrite). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-module(rabbit_message_interceptor_timestamp). | ||
-behaviour(rabbit_message_interceptor). | ||
|
||
-include("mc.hrl"). | ||
|
||
-define(HEADER_TIMESTAMP, <<"timestamp_in_ms">>). | ||
|
||
-export([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
intercept/4 | ||
]). | ||
|
||
intercept(Msg0, _MsgInterceptorCtx, _Group, Config) -> | ||
Ts = mc:get_annotation(?ANN_RECEIVED_AT_TIMESTAMP, Msg0), | ||
Overwrite = maps:get(overwrite, Config, false), | ||
Msg = rabbit_message_interceptor:set_msg_annotation( | ||
Msg0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. select this funcction and hit |
||
?HEADER_TIMESTAMP, | ||
Ts, | ||
Overwrite), | ||
set_msg_timestamp(Msg, Ts, Overwrite). | ||
|
||
set_msg_timestamp(Msg, Timestamp, Overwrite) -> | ||
case {mc:timestamp(Msg), Overwrite} of | ||
{Ts, false} when is_integer(Ts) -> | ||
Msg; | ||
_ -> | ||
mc:set_annotation(?ANN_TIMESTAMP, Timestamp, Msg) | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
select and hit
=
in nvim