Skip to content

Commit

Permalink
Fix types, add newer OTP releases
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Jul 15, 2024
1 parent e50e33d commit 0f3e78d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
otp: [22.3, 23.3, 24.2]
otp: [24.3, 25, 26]
rebar: [3.18.0]
steps:
- uses: actions/checkout@v2
Expand Down
27 changes: 0 additions & 27 deletions .github/workflows/hex.yaml

This file was deleted.

27 changes: 17 additions & 10 deletions src/logstasher.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,33 @@ start_link() ->
%% Supervisor callbacks
%%==============================================================================

-spec init(term()) -> {ok, maps:map()} | {stop, maps:map()}.
-type state() :: #{
transport := module(),
host := string() | binary() | tuple(),
port := pos_integer(),
socket => gen_udp:socket() | gen_tcp:socket() | undefined
}.

-spec init(_) -> {ok, state()}.
init(_) ->
Transport = application:get_env(?MODULE, transport, ?LOGSTASH_TRANSPORT),
Host = application:get_env(?MODULE, host, ?LOGSTASH_HOST),
Port = application:get_env(?MODULE, port, ?LOGSTASH_PORT),
Opts = #{transport => Transport, host => Host, port => Port},
State = Opts#{socket => connect(Opts)},
Opts = #{ transport => Transport, host => Host, port => Port },
State = Opts#{ socket => connect(Opts) },
{ok, State}.

-spec handle_call({send, binary()}, any(), maps:map()) ->
{reply, ok | {error, atom() | {timeout, binary()}}, maps:map()}.
-spec handle_call({send, binary()}, _, state()) ->
{reply, ok | {error, atom() | {timeout, binary()}}, state()}.
handle_call({send, Data}, _, State) ->
Result = maybe_send(Data, State),
{reply, Result, State}.

-spec handle_cast(term(), maps:map()) -> {noreply, maps:map()}.
-spec handle_cast(_, state()) -> {noreply, state()}.
handle_cast(_, State) ->
{noreply, State}.

-spec terminate(term(), maps:map()) -> ok.
-spec terminate(_, state()) -> ok.
terminate(_, #{transport := tcp, socket := Socket}) ->
gen_tcp:close(Socket);
terminate(_, #{transport := udp, socket := Socket}) ->
Expand All @@ -90,7 +97,7 @@ terminate(_, #{transport := console}) ->
%% Internal functions
%%==============================================================================

-spec connect(maps:map()) -> gen_udp:socket() | gen_tcp:socket() | undefined.
-spec connect(state()) -> gen_udp:socket() | gen_tcp:socket() | undefined.
connect(#{transport := tcp, host := Host, port := Port}) ->
Opts = [binary, {active, false}, {keepalive, true}],
case gen_tcp:connect(Host, Port, Opts, ?TCP_CONNECT_TIMEOUT) of
Expand All @@ -112,7 +119,7 @@ connect(#{transport := udp}) ->
connect(#{transport := console}) ->
undefined.

-spec maybe_send(binary(), maps:map()) -> ok | {error, atom()}.
-spec maybe_send(binary(), map()) -> ok | {error, atom()}.
maybe_send(Data, #{transport := console} = State) ->
send(Data, State);
maybe_send(Data, #{socket := undefined} = State) ->
Expand All @@ -124,7 +131,7 @@ maybe_send(Data, State) ->
{error, _} = Error -> Error
end.

-spec send(binary(), maps:map()) -> ok | {error, atom()}.
-spec send(binary(), map()) -> ok | {error, atom()}.
send(Data, #{transport := console}) ->
io:put_chars([ Data, "\n"]);
send(_Data, #{socket := undefined}) ->
Expand Down
11 changes: 3 additions & 8 deletions src/logstasher_h.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-export([log_data/1]).

%% Xref ignores
-ignore_xref([log/2]).
-ignore_xref([log/2, log_data/1]).

%% Truncate binary values beyond this size.
-define(LOG_BINARY_SIZE, 2000).
Expand Down Expand Up @@ -49,11 +49,6 @@ log_data(#{level := Level, msg := EventData, meta := Meta}) ->

%% @doc If there is no message, try to extract the 'text' fields from the message fields
%% and use that as the message.
-spec maybe_extract_message(Message, MsgFields) -> {Message1, MsgFields1} when
Message :: null | binary(),
Message1 :: null | binary(),
MsgFields :: map(),
MsgFields1 :: map().
maybe_extract_message(null, #{ text := Text } = MsgFields) when is_binary(Text) ->
{Text, maps:remove(text, MsgFields)};
maybe_extract_message(Msg, MsgFields) ->
Expand Down Expand Up @@ -195,11 +190,11 @@ is_proplist(_) -> false.
% Simple ASCII character string, typically SQL statements, filenames or literal texts.
is_ascii_list([]) -> true;
is_ascii_list([ C | T ]) when C >= 32, C =< 127 -> is_ascii_list(T);
is_ascii_list([ C | T ]) when C =:= $\n, C =:= $\t -> is_ascii_list(T);
is_ascii_list([ C | T ]) when C =:= $\n; C =:= $\t -> is_ascii_list(T);
is_ascii_list(_) -> false.

maybe_truncate(Bin) when size(Bin) >= ?LOG_BINARY_SIZE ->
<<Truncated:?LOG_BINARY_SIZE/binary, _/binary>> = Bin,
<<Truncated, "...">>;
<<Truncated/binary, "...">>;
maybe_truncate(Bin) ->
Bin.

0 comments on commit 0f3e78d

Please sign in to comment.