Skip to content
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

Automated logging towards GRiSP.io #18

Merged
merged 22 commits into from
Apr 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/grisp_io_logger_bin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@

-define(DEFAULT_CALL_TIMEOUT, 5000).

% FixMe:
% Sending over ~30_000 bytes over WS breaks rtems I/O driver.
% We want avoid to return chunks that are bigger then that.
% -define(MAX_CHUNK_BYTES, 30_000).
-define(MAX_LOG_BYTES, 30_000).

%--- API -----------------------------------------------------------------------

sync(Seq, DroppedConfirmed) when
Expand Down Expand Up @@ -87,7 +93,16 @@ format(Event, Config) ->
_Else ->
ok
end,
base64:encode(term_to_binary(Event)).
Encoded = base64:encode(term_to_binary(Event)),
EncodedSize = byte_size(Encoded),
case EncodedSize > ?MAX_LOG_BYTES of
true ->
NewLog = log_discard(EncodedSize),
io:format("Replacing big log with ~p~n",[NewLog]),
base64:encode(term_to_binary(NewLog));
false ->
Encoded
end.

%--- logger_h_common Callbacks -------------------------------------------------

Expand Down Expand Up @@ -201,9 +216,22 @@ insert_drop_event({Events, 0}) ->
insert_drop_event({Events, Dropped}) ->
{[drop_event(Dropped) | Events], Dropped}.

log_discard(Size) ->
Meta = simulate_log_metadata(),
Report = {report, #{
event => log_discarded,
reason => too_long,
size => Size}
},
#{level => warning, meta => Meta, msg => Report}.
ziopio marked this conversation as resolved.
Show resolved Hide resolved

ziopio marked this conversation as resolved.
Show resolved Hide resolved
drop_event(Count) ->
% Create a fake logger event mimicking a real log event as much as possible
Meta = simulate_log_metadata(),
Report = {report, #{event => log_drop, count => Count}},
{null, format(#{level => warning, meta => Meta, msg => Report}, #{})}.

simulate_log_metadata() ->
% See internal function logger:add_default_metadata/2
DefaultMetadata = #{
pid => self(),
Expand All @@ -221,13 +249,10 @@ drop_event(Count) ->
_ -> #{}
end,

Meta = lists:foldl(
lists:foldl(
fun(M, MAcc) ->
maps:merge(MAcc, M)
end,
DefaultMetadata,
[?LOCATION, PrimaryMetadata, ProcessMetadata]
),

Report = {report, #{event => log_drop, count => Count}},
{null, format(#{level => warning, meta => Meta, msg => Report}, #{})}.
).
Loading