Skip to content

Commit 502ed0e

Browse files
authored
Merge pull request #4561 from esl/tomas/log-large-structs
Refactor logging to use `c2s_data` consistently in log messages
2 parents b13f111 + aa3217c commit 502ed0e

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

src/c2s/mongoose_c2s.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ handle_sasl_success(StateData = #c2s_data{jid = MaybeInitialJid, info = Info}, S
444444
info = maps:merge(Info, #{auth_module => AuthMod})},
445445
El = mongoose_c2s_stanzas:sasl_success_stanza(MaybeServerOut),
446446
send_acc_from_server_jid(StateData1, SaslAcc, El),
447-
?LOG_INFO(#{what => auth_success, text => <<"Accepted SASL authentication">>, c2s_state => StateData1}),
447+
?LOG_INFO(#{what => auth_success, text => <<"Accepted SASL authentication">>, c2s_data => StateData1}),
448448
{next_state, {wait_for_stream, authenticated}, StateData1, state_timeout(StateData1)};
449449
false ->
450450
c2s_stream_error(StateData, mongoose_xmpp_errors:invalid_from())
@@ -551,7 +551,7 @@ verify_user(wait_for_session_establishment, _, _, Acc) ->
551551
verify_user(session_established, HostType, #{c2s_data := StateData} = HookParams, Acc) ->
552552
case mongoose_c2s_hooks:user_open_session(HostType, Acc, HookParams) of
553553
{ok, Acc1} ->
554-
?LOG_INFO(#{what => c2s_opened_session, c2s_state => StateData}),
554+
?LOG_INFO(#{what => c2s_opened_session, c2s_data => StateData}),
555555
{ok, Acc1};
556556
{stop, Acc1} ->
557557
Jid = StateData#c2s_data.jid,
@@ -689,7 +689,7 @@ verify_process_alive(StateData, C2SState, Pid) ->
689689
true ->
690690
?LOG_WARNING(#{what => c2s_replaced_wait_timeout,
691691
text => <<"Some processes are not responding when handling replace messages">>,
692-
replaced_pid => Pid, state_name => C2SState, c2s_state => StateData})
692+
replaced_pid => Pid, state_name => C2SState, c2s_data => StateData})
693693
end.
694694

695695
-spec maybe_retry_state(state()) -> state() | {stop, term()}.
@@ -914,7 +914,7 @@ send_trailer(StateData) ->
914914

915915
-spec c2s_stream_error(data(), exml:element()) -> fsm_res().
916916
c2s_stream_error(StateData, Error) ->
917-
?LOG_DEBUG(#{what => c2s_stream_error, xml_error => Error, c2s_state => StateData}),
917+
?LOG_DEBUG(#{what => c2s_stream_error, xml_error => Error, c2s_data => StateData}),
918918
send_element_from_server_jid(StateData, Error),
919919
send_xml(StateData, ?XML_STREAM_TRAILER),
920920
{stop, {shutdown, stream_error}, StateData}.

src/logger/mongoose_log_filter.erl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,26 @@ fill_metadata_filter(Event, _) ->
2929
Event.
3030

3131
format_c2s_state_filter(Event=#{msg := {report, Msg=#{c2s_data := State}}}, _) ->
32-
StateMap = filter_undefined(c2s_data_to_map(State)),
33-
%% C2S fields have lower priority, if the field is already present in msg.
34-
Msg2 = maps:merge(StateMap, maps:remove(c2s_data, Msg)),
35-
Event#{msg => {report, Msg2}};
32+
do_format_c2s_state(Event, Msg, State, c2s_data);
33+
%% Backward compatibility for logs that incorrectly use c2s_state key for StateData
34+
format_c2s_state_filter(Event=#{msg := {report, Msg=#{c2s_state := State}}}, _) ->
35+
try
36+
do_format_c2s_state(Event, Msg, State, c2s_state)
37+
catch
38+
_:_ ->
39+
%% Not a c2s_data record, leave as is
40+
Event
41+
end;
3642
format_c2s_state_filter(Event, _) ->
3743
Event.
3844

45+
%% Helper function to format c2s state data
46+
do_format_c2s_state(Event, Msg, State, KeyToRemove) ->
47+
StateMap = filter_undefined(c2s_data_to_map(State)),
48+
%% C2S fields have lower priority, if the field is already present in msg.
49+
Msg2 = maps:merge(StateMap, maps:remove(KeyToRemove, Msg)),
50+
Event#{msg => {report, Msg2}}.
51+
3952
format_acc_filter(Event=#{msg := {report, Msg=#{acc := Acc}}}, _) ->
4053
FormattedAcc = format_acc(Acc),
4154
Msg2 = maps:merge(FormattedAcc, maps:remove(acc, Msg)),

src/mod_presence.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ presence_broadcast_to_trusted(Acc, FromJid, Presences, Packet) ->
507507

508508
-spec resend_offline_messages(mongoose_acc:t(), mongoose_c2s:data()) -> mongoose_acc:t().
509509
resend_offline_messages(Acc, StateData) ->
510-
?LOG_DEBUG(#{what => resend_offline_messages, acc => Acc, c2s_state => StateData}),
510+
?LOG_DEBUG(#{what => resend_offline_messages, acc => Acc, c2s_data => StateData}),
511511
Jid = mongoose_c2s:get_jid(StateData),
512512
Acc1 = mongoose_hooks:resend_offline_messages(Acc, Jid),
513513
Rs = mongoose_acc:get(offline, messages, [], Acc1),

src/stream_management/mod_stream_management.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ handle_stream_mgmt(Acc, _Params, _El) ->
454454
handle_r(Acc, #{c2s_data := StateData}) ->
455455
case get_mod_state(StateData) of
456456
{error, not_found} ->
457-
?LOG_WARNING(#{what => unexpected_r, c2s_state => StateData,
457+
?LOG_WARNING(#{what => unexpected_r, c2s_data => StateData,
458458
text => <<"received <r/> but stream management is off!">>}),
459459
{ok, Acc};
460460
#sm_state{counter_in = In} ->

test/mongoose_log_filter_SUITE.erl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-module(mongoose_log_filter_SUITE).
2+
3+
-include_lib("common_test/include/ct.hrl").
4+
-include_lib("eunit/include/eunit.hrl").
5+
6+
-export([all/0, init_per_testcase/2, end_per_testcase/2]).
7+
-export([
8+
format_c2s_state_with_invalid_c2s_state/1,
9+
format_c2s_state_with_no_c2s_keys/1
10+
]).
11+
12+
all() ->
13+
[
14+
format_c2s_state_with_invalid_c2s_state,
15+
format_c2s_state_with_no_c2s_keys
16+
].
17+
18+
init_per_testcase(_TestCase, Config) ->
19+
Config.
20+
21+
end_per_testcase(_TestCase, _Config) ->
22+
ok.
23+
24+
format_c2s_state_with_invalid_c2s_state(_Config) ->
25+
% Test the backward compatibility path with invalid c2s_state data
26+
% This tests the try-catch block that should prevent crashes
27+
Event = #{
28+
msg => {report, #{what => test_invalid, c2s_state => <<"not_a_record">>}}
29+
},
30+
31+
Result = mongoose_log_filter:format_c2s_state_filter(Event, no_state),
32+
33+
% The result should be unchanged when c2s_state contains invalid data
34+
?assertEqual(Event, Result).
35+
36+
format_c2s_state_with_no_c2s_keys(_Config) ->
37+
% Test when there are no c2s_data or c2s_state keys
38+
Event = #{
39+
msg => {report, #{what => test_no_c2s}}
40+
},
41+
42+
Result = mongoose_log_filter:format_c2s_state_filter(Event, no_state),
43+
44+
% The result should be unchanged
45+
?assertEqual(Event, Result).

0 commit comments

Comments
 (0)