-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Incoming message interceptors #13641
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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. |
14 changes: 14 additions & 0 deletions
14
deps/rabbit/src/rabbit_message_interceptor_routing_node.erl
This file contains hidden or 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,14 @@ | ||
-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 hidden or 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,26 @@ | ||
-module(rabbit_message_interceptor_timestamp). | ||
-behaviour(rabbit_message_interceptor). | ||
|
||
-include("mc.hrl"). | ||
|
||
-define(HEADER_TIMESTAMP, <<"timestamp_in_ms">>). | ||
|
||
-export([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, | ||
?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 hidden or 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.
this mapping should be removed, it is superseded by the explicit
message_interceptors.incoming.set_header_timestamp.overwrite
andmessage_interceptors.incoming.rabbit_message_interceptor_routing_node.overwrite