Skip to content

Commit

Permalink
Test log handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ziopio committed Feb 16, 2024
1 parent eccadf9 commit 5541484
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/grisp_io.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
{connect, true}, % keeps a constant connection with grisp.io
{ntp, false}, % if set to true, starts the NTP client
{ws_requests_timeout, 5_000},
{ws_logs_interval, 2_000},
{ws_logs_batch_size, 100},
{logger, [
% Enable our own default handler,
% which will receive all events from boot
{handler,
grisp_io,
grisp_io_log_handler,
grisp_io_logger_bin,
#{formatter => {grisp_io_logger_bin, #{}}}}
]}
Expand Down
63 changes: 63 additions & 0 deletions src/grisp_io_logtest.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
% @doc temporary module to test logging
-module(grisp_io_logtest).

-export([start_link/0]).

-behaviour(gen_server).

-export([init/1]).
-export([handle_call/3]).
-export([handle_cast/2]).
-export([handle_info/2]).

-record(state, {
timer
}).

% -include_lib("kernel/include/logger.hrl").

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).


% Callbacks


init(_) ->
{ok, Interval} = application:get_env(grisp_io, ws_logs_interval),
Timer = erlang:start_timer(Interval, self(), logs),
{ok, #state{timer = Timer}}.

handle_call(_Request, _From, _State) ->
error(unknown_request).

handle_cast(_Msg, _State) ->
error(unknown_cast).

handle_info({timeout, OldRef, logs}, #state{timer = OldRef} = S)->
{ok, Size} = application:get_env(grisp_io, ws_logs_batch_size),
{ok, Interval} = application:get_env(grisp_io, ws_logs_interval),
{Events, Dropped} = Chunk = grisp_io_logger_bin:chunk(Size),
io:format("Sending logs... events = ~p, dropped = ~p\n",
[length(Events), Dropped]),
case send_logs(Chunk) of
ok -> ok;
{ok, #{<<"seq">> := Seq, <<"dropped">> := ServerDropped}} ->
dab_logger_bin:sync(Seq, ServerDropped);
E ->
io:format("Error sending logs = ~p\n",[E])
end,
erlang:cancel_timer(OldRef),
NewTimer = erlang:start_timer(Interval, self(), logs),
{noreply, S#state{timer = NewTimer}}.

%--- Internals -----------------------------------------------------------------

send_logs({[], _Dropped}) ->
ok;
send_logs({Events, Dropped}) ->
LogUpdate = #{
events => [[Seq, E] || {Seq, E} <- Events],
dropped => Dropped
},
grisp_io_ws:request(post, logs, LogUpdate).
3 changes: 2 additions & 1 deletion src/grisp_io_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ init([]) ->
end,
ChildSpecs = NTP ++ [
worker(grisp_io_ws, []),
worker(grisp_io_connection, [])
worker(grisp_io_connection, []),
worker(grisp_io_logtest, [])
],
{ok, {SupFlags, ChildSpecs}}.

Expand Down
6 changes: 4 additions & 2 deletions src/grisp_io_ws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ request(Method, Type, Params) ->
{?FUNCTION_NAME, Method, Type, Params},
?call_timeout).

% gen_server callbacks ---------------------------------------------------------
% gen_server callbacks -----------------------------

init([]) -> {ok, #state{}}.

Expand All @@ -75,7 +75,9 @@ handle_cast({connect, Server, Port}, #state{gun_pid = undefined} = S) ->
{noreply, S}
end;
handle_cast({connect, _Server, _Port}, S) ->
{noreply, S}.
{noreply, S};
handle_cast(_Msg, _S) ->
error(unhandled_cast).

handle_info({gun_up, GunPid, _}, #state{gun_pid = GunPid} = S) ->
?LOG_INFO(#{event => connection_enstablished}),
Expand Down

0 comments on commit 5541484

Please sign in to comment.