| {dst_host , inet:ip_address() | inet:hostname()}
| {dst_port , inet:port_number()}
| {src_port , inet:port_number()}
+ | {num_msgs_per_packet , non_neg_integer()}
.
-define(DEFAULT_DST_HOST, "localhost").
, dst_host :: inet:ip_address() | inet:hostname()
, dst_port :: inet:port_number()
, src_port :: inet:port_number()
+ , num_msgs_per_packet :: non_neg_integer()
}).
-type state() ::
DstHost = hope_kv_list:get(Options, dst_host, ?DEFAULT_DST_HOST),
DstPort = hope_kv_list:get(Options, dst_port, ?DEFAULT_DST_PORT),
SrcPort = hope_kv_list:get(Options, src_port, ?DEFAULT_SRC_PORT),
+ NumMsgsPerPacket = hope_kv_list:get(Options, num_msgs_per_packet, 10),
State = #state
{ sock = none
, dst_host = DstHost
, dst_port = DstPort
, src_port = SrcPort
+ , num_msgs_per_packet = NumMsgsPerPacket
},
{ConsumptionInterval, State}.
-spec consume(beam_stats_consumer:queue(), state()) ->
state().
-consume(Q, #state{}=State1) ->
- Payload = beam_stats_queue_to_binary(Q),
- State2 = try_to_connect_if_no_socket(State1),
- try_to_send(State2, Payload).
+consume(Q, #state{num_msgs_per_packet=NumMsgsPerPacket}=State) ->
+ Packets = beam_stats_queue_to_packets(Q, NumMsgsPerPacket),
+ lists:foldl(fun try_to_connect_and_send/2, State, Packets).
-spec terminate(state()) ->
{}.
%% Transport
%% ============================================================================
+-spec try_to_connect_and_send(binary(), state()) ->
+ state().
+try_to_connect_and_send(<<Payload/binary>>, #state{}=State1) ->
+ State2 = try_to_connect_if_no_socket(State1),
+ try_to_send(State2, Payload).
+
-spec try_to_send(state(), binary()) ->
state().
try_to_send(#state{sock=none}=State, _) ->
%% Serialization
%% ============================================================================
--spec beam_stats_queue_to_binary(beam_stats_consumer:queue()) ->
- binary().
-beam_stats_queue_to_binary(Q) ->
- iolist_to_binary([beam_stats_to_bins(B) || B <- queue:to_list(Q)]).
+-spec beam_stats_queue_to_packets(beam_stats_consumer:queue(), non_neg_integer()) ->
+ [binary()].
+beam_stats_queue_to_packets(Q, NumMsgsPerPacket) ->
+ MsgBins = lists:append([beam_stats_to_bins(B) || B <- queue:to_list(Q)]),
+ MsgBinsChucks = hope_list:divide(MsgBins, NumMsgsPerPacket),
+ lists:map(fun erlang:iolist_to_binary/1, MsgBinsChucks).
-spec beam_stats_to_bins(beam_stats:t()) ->
[binary()].
BEAMStats = #beam_stats
{ timestamp = {1, 2, 3}
, node_id = 'node_foo@host_bar'
- , memory = [{mem_type_foo, 1}]
+ , memory = [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}]
, io_bytes_in = 3
, io_bytes_out = 7
, context_switches = 5
{_, State1} = beam_stats_consumer_statsd:init(Options),
State2 = beam_stats_consumer_statsd:consume(BEAMStatsQ, State1),
{} = beam_stats_consumer_statsd:terminate(State2),
- ResultOfReceive = gen_udp:recv(ServerSocket, 0),
+ ResultOfReceive1 = gen_udp:recv(ServerSocket, 0),
+ {ok, {_, _, PacketReceived1}} = ResultOfReceive1,
+ ResultOfReceive2 = gen_udp:recv(ServerSocket, 0),
+ {ok, {_, _, PacketReceived2}} = ResultOfReceive2,
ok = gen_udp:close(ServerSocket),
- {ok, {_, _, Data}} = ResultOfReceive,
- ct:log("Packet: ~n~s~n", [Data]),
- << "beam_stats.node_foo_host_bar.io.bytes_in:3|g\n"
- , "beam_stats.node_foo_host_bar.io.bytes_out:7|g\n"
- , "beam_stats.node_foo_host_bar.context_switches:5|g\n"
- , "beam_stats.node_foo_host_bar.reductions:9|g\n"
- , "beam_stats.node_foo_host_bar.run_queue:17|g\n"
- , "beam_stats.node_foo_host_bar.memory.mem_type_foo:1|g\n"
- , "beam_stats.node_foo_host_bar.ets_table.size.foo.foo:5|g\n"
- , "beam_stats.node_foo_host_bar.ets_table.memory.foo.foo:25|g\n"
- , "beam_stats.node_foo_host_bar.ets_table.size.bar.37:8|g\n"
- , "beam_stats.node_foo_host_bar.ets_table.memory.bar.37:38|g\n"
- >> = Data.
+ ct:log("PacketReceived1: ~n~s~n", [PacketReceived1]),
+ ct:log("PacketReceived2: ~n~s~n", [PacketReceived2]),
+ PacketsCombined = <<PacketReceived1/binary, PacketReceived2/binary>>,
+ ct:log("PacketsCombined: ~n~s~n", [PacketsCombined]),
+ MsgsExpected =
+ [ <<"beam_stats.node_foo_host_bar.io.bytes_in:3|g">>
+ , <<"beam_stats.node_foo_host_bar.io.bytes_out:7|g">>
+ , <<"beam_stats.node_foo_host_bar.context_switches:5|g">>
+ , <<"beam_stats.node_foo_host_bar.reductions:9|g">>
+ , <<"beam_stats.node_foo_host_bar.run_queue:17|g">>
+ , <<"beam_stats.node_foo_host_bar.memory.mem_type_foo:1|g">>
+ , <<"beam_stats.node_foo_host_bar.memory.mem_type_bar:2|g">>
+ , <<"beam_stats.node_foo_host_bar.memory.mem_type_baz:3|g">>
+ , <<"beam_stats.node_foo_host_bar.ets_table.size.foo.foo:5|g">>
+ , <<"beam_stats.node_foo_host_bar.ets_table.memory.foo.foo:25|g">>
+ , <<"beam_stats.node_foo_host_bar.ets_table.size.bar.37:8|g">>
+ , <<"beam_stats.node_foo_host_bar.ets_table.memory.bar.37:38|g">>
+ ],
+ MsgsReceived = binary:split(PacketsCombined, <<"\n">>, [global, trim]),
+ RemoveExpectedFromReceived =
+ fun (Expected, Received) ->
+ ct:log(
+ "Looking for expected msg ~p in remaining received ~p~n",
+ [Expected, Received]
+ ),
+ true = lists:member(Expected, Received),
+ Received -- [Expected]
+ end,
+ [] = lists:foldl(RemoveExpectedFromReceived, MsgsReceived, MsgsExpected).