X-Git-Url: https://git.xandkar.net/?p=beam_stats.git;a=blobdiff_plain;f=src%2Fbeam_stats_consumer_csv.erl;h=6b271068f489064b7c576f335b74cd496b03538d;hp=c00c28fe1ba8872f4ddcb11ab0ce6c17e2fdf06d;hb=101874c36980f5c738575c0143ae673c0797b6d4;hpb=caf75ed8160362773766c6bde005cf5f33544392 diff --git a/src/beam_stats_consumer_csv.erl b/src/beam_stats_consumer_csv.erl index c00c28f..6b27106 100644 --- a/src/beam_stats_consumer_csv.erl +++ b/src/beam_stats_consumer_csv.erl @@ -1,6 +1,7 @@ -module(beam_stats_consumer_csv). -include("include/beam_stats.hrl"). +-include("beam_stats_logging.hrl"). -behaviour(beam_stats_consumer). @@ -15,8 +16,8 @@ ]). -type option() :: - {path , file:filename()} - | {consumption_interval , erlang:time()} + {consumption_interval , non_neg_integer()} + | {path , file:filename()} . -record(state, @@ -28,7 +29,7 @@ #state{}. -spec init([option()]) -> - {erlang:time(), state()}. + {non_neg_integer(), state()}. init(Options) -> ConsumptionInterval = hope_kv_list:get(Options, consumption_interval, 60000), {some, Path} = hope_kv_list:get(Options, path), @@ -56,14 +57,14 @@ terminate(#state{file=FileOpt}) -> -spec try_to_write(state(), binary()) -> state(). try_to_write(#state{file=none, path=Path}=State, _) -> - io:format("error: file closed: ~s~n", [Path]), + ?log_error("Writing to file (~p) failed: no file in state.", [Path]), State; try_to_write(#state{file={some, File}}=State, Payload) -> case file:write(File, Payload) of ok -> State ; {error, _}=Error -> - io:format("error: file:write/2 failed: ~p~n", [Error]), + ?log_error("file:write(~p, ~p) -> ~p", [File, Payload, Error]), % TODO: Maybe schedule retry? ok = file:close(File), State#state{file=none} @@ -74,11 +75,12 @@ try_to_write(#state{file={some, File}}=State, Payload) -> try_to_open_if_no_file(#state{file={some, _}}=State) -> State; try_to_open_if_no_file(#state{file=none, path=Path}=State) -> - case file:open(Path, [append]) + Options = [append], + case file:open(Path, Options) of {ok, File} -> State#state{file = {some, File}} ; {error, _}=Error -> - io:format("error: file:open/2 failed: ~p~n", [Error]), + ?log_error("file:open(~p, ~p) -> ~p", [Path, Options, Error]), State#state{file = none} end. @@ -99,9 +101,9 @@ beam_stats_to_bin(#beam_stats ) -> <> = timestamp_to_bin(Timestamp), <> = node_id_to_bin(NodeID), - PairToBin = make_pair_to_bin(NodeIDBin, TimestampBin), + MemoryPairToBin = make_pair_to_bin(NodeIDBin, TimestampBin, <<"memory">>), MemoryBinPairs = lists:map(fun atom_int_to_bin_bin/1, Memory), - MemoryBins = lists:map(PairToBin, MemoryBinPairs), + MemoryBins = lists:map(MemoryPairToBin, MemoryBinPairs), AllBins = [ MemoryBins ], @@ -129,14 +131,16 @@ timestamp_to_float({ComponentMega, ComponentWhole, ComponentMicro}) -> TotalMicroSeconds = (TotalWholeSeconds * OneMillion) + ComponentMicro, TotalMicroSeconds / OneMillion. --spec make_pair_to_bin(binary(), binary()) -> +-spec make_pair_to_bin(binary(), binary(), binary()) -> fun(({binary(), binary()}) -> binary()). -make_pair_to_bin(<>, <>) -> +make_pair_to_bin(<>, <>, <>) -> fun ({<>, <>}) -> << TimestampBin/binary , "|" , NodeID/binary , "|" + , Type/binary + , "|" , K/binary , "|" , V/binary