1 -module(beam_stats_consumer_statsd).
3 -include("include/beam_stats.hrl").
4 -include("include/beam_stats_ets_table.hrl").
5 -include("beam_stats_logging.hrl").
7 -behaviour(beam_stats_consumer).
21 {consumption_interval , non_neg_integer()}
22 | {dst_host , inet:ip_address() | inet:hostname()}
23 | {dst_port , inet:port_number()}
24 | {src_port , inet:port_number()}
25 | {num_msgs_per_packet , non_neg_integer()}
28 -define(DEFAULT_DST_HOST, "localhost").
29 -define(DEFAULT_DST_PORT, 8125).
30 -define(DEFAULT_SRC_PORT, 8124).
32 -type metric_type() ::
33 % TODO: Add other metric types
38 , value :: non_neg_integer()
39 , type :: metric_type()
46 { sock :: hope_option:t(gen_udp:socket())
47 , dst_host :: inet:ip_address() | inet:hostname()
48 , dst_port :: inet:port_number()
49 , src_port :: inet:port_number()
50 , num_msgs_per_packet :: non_neg_integer()
56 -define(PATH_PREFIX, "beam_stats").
58 %% ============================================================================
59 %% Consumer implementation
60 %% ============================================================================
62 -spec init([option()]) ->
63 {non_neg_integer(), state()}.
65 ConsumptionInterval = hope_kv_list:get(Options, consumption_interval, 60000),
66 DstHost = hope_kv_list:get(Options, dst_host, ?DEFAULT_DST_HOST),
67 DstPort = hope_kv_list:get(Options, dst_port, ?DEFAULT_DST_PORT),
68 SrcPort = hope_kv_list:get(Options, src_port, ?DEFAULT_SRC_PORT),
69 NumMsgsPerPacket = hope_kv_list:get(Options, num_msgs_per_packet, 10),
75 , num_msgs_per_packet = NumMsgsPerPacket
77 {ConsumptionInterval, State}.
79 -spec consume(beam_stats_consumer:queue(), state()) ->
81 consume(Q, #state{num_msgs_per_packet=NumMsgsPerPacket}=State) ->
82 Packets = beam_stats_queue_to_packets(Q, NumMsgsPerPacket),
83 lists:foldl(fun try_to_connect_and_send/2, State, Packets).
85 -spec terminate(state()) ->
87 terminate(#state{sock=SockOpt}) ->
88 ok = hope_option:iter(SockOpt, fun gen_udp:close/1),
91 %% ============================================================================
93 %% ============================================================================
95 -spec try_to_connect_and_send(binary(), state()) ->
97 try_to_connect_and_send(<<Payload/binary>>, #state{}=State1) ->
98 State2 = try_to_connect_if_no_socket(State1),
99 try_to_send(State2, Payload).
101 -spec try_to_send(state(), binary()) ->
103 try_to_send(#state{sock=none}=State, _) ->
104 ?log_error("Sending failed. No socket in state."),
105 % TODO: Maybe schedule retry?
109 { sock = {some, Sock}
115 case gen_udp:send(Sock, DstHost, DstPort, Payload)
118 ; {error, _}=Error ->
120 "gen_udp:send(~p, ~p, ~p, ~p) -> ~p",
121 [Sock, DstHost, DstPort, Error]
123 % TODO: Do something with unsent messages?
124 ok = gen_udp:close(Sock),
125 State#state{sock=none}
128 -spec try_to_connect_if_no_socket(state()) ->
130 try_to_connect_if_no_socket(#state{sock={some, _}}=State) ->
132 try_to_connect_if_no_socket(#state{sock=none, src_port=SrcPort}=State) ->
133 case gen_udp:open(SrcPort)
135 State#state{sock = {some, Sock}}
136 ; {error, _}=Error ->
137 ?log_error("gen_udp:open(~p) -> ~p", [SrcPort, Error]),
138 State#state{sock = none}
141 %% ============================================================================
143 %% ============================================================================
145 -spec beam_stats_queue_to_packets(beam_stats_consumer:queue(), non_neg_integer()) ->
147 beam_stats_queue_to_packets(Q, NumMsgsPerPacket) ->
148 MsgBins = lists:append([beam_stats_to_bins(B) || B <- queue:to_list(Q)]),
149 MsgBinsChucks = hope_list:divide(MsgBins, NumMsgsPerPacket),
150 lists:map(fun erlang:iolist_to_binary/1, MsgBinsChucks).
152 -spec beam_stats_to_bins(beam_stats:t()) ->
154 beam_stats_to_bins(#beam_stats
157 , io_bytes_in = IOBytesIn
158 , io_bytes_out = IOBytesOut
159 , context_switches = ContextSwitches
160 , reductions = Reductions
161 , run_queue = RunQueue
165 NodeIDBin = node_id_to_bin(NodeID),
167 [ io_bytes_in_to_msg(IOBytesIn)
168 , io_bytes_out_to_msg(IOBytesOut)
169 , context_switches_to_msg(ContextSwitches)
170 , reductions_to_msg(Reductions)
171 , run_queue_to_msg(RunQueue)
172 | memory_to_msgs(Memory)
175 Msgs2 = [statsd_msg_add_name_prefix(M, NodeIDBin) || M <- Msgs1],
176 [statsd_msg_to_bin(M) || M <- Msgs2].
178 -spec run_queue_to_msg(non_neg_integer()) ->
180 run_queue_to_msg(RunQueue) ->
182 { name = <<"run_queue">>
187 -spec reductions_to_msg(non_neg_integer()) ->
189 reductions_to_msg(Reductions) ->
191 { name = <<"reductions">>
196 -spec context_switches_to_msg(non_neg_integer()) ->
198 context_switches_to_msg(ContextSwitches) ->
200 { name = <<"context_switches">>
201 , value = ContextSwitches
205 -spec io_bytes_in_to_msg(non_neg_integer()) ->
207 io_bytes_in_to_msg(IOBytesIn) ->
209 { name = <<"io.bytes_in">>
214 -spec io_bytes_out_to_msg(non_neg_integer()) ->
216 io_bytes_out_to_msg(IOBytesOut) ->
218 { name = <<"io.bytes_out">>
223 -spec ets_to_msgs(beam_stats_ets:t()) ->
225 ets_to_msgs(PerTableStats) ->
226 NestedMsgs = lists:map(fun ets_table_to_msgs/1, PerTableStats),
227 lists:append(NestedMsgs).
229 -spec ets_table_to_msgs(beam_stats_ets_table:t()) ->
231 ets_table_to_msgs(#beam_stats_ets_table
238 IDBin = beam_stats_ets_table:id_to_bin(ID),
239 NameBin = atom_to_binary(Name, latin1),
240 NameAndID = <<NameBin/binary, ".", IDBin/binary>>,
243 { name = <<"ets_table.size.", NameAndID/binary>>
249 { name = <<"ets_table.memory.", NameAndID/binary>>
253 [SizeMsg, MemoryMsg].
255 -spec memory_to_msgs([{atom(), non_neg_integer()}]) ->
257 memory_to_msgs(Memory) ->
258 [memory_component_to_statsd_msg(MC) || MC <- Memory].
260 -spec memory_component_to_statsd_msg({atom(), non_neg_integer()}) ->
262 memory_component_to_statsd_msg({MemType, MemSize}) when MemSize >= 0 ->
263 MemTypeBin = atom_to_binary(MemType, latin1),
265 { name = <<"memory.", MemTypeBin/binary>>
270 -spec statsd_msg_add_name_prefix(statsd_msg(), binary()) ->
272 statsd_msg_add_name_prefix(#statsd_msg{name=Name1}=Msg, <<NodeID/binary>>) ->
273 Prefix = <<?PATH_PREFIX, ".", NodeID/binary, ".">>,
274 Name2 = <<Prefix/binary, Name1/binary>>,
275 Msg#statsd_msg{name=Name2}.
277 -spec statsd_msg_to_bin(statsd_msg()) ->
281 { name = <<Name/binary>>
283 , type = Type = gauge
286 TypeBin = metric_type_to_bin(Type),
287 ValueBin = integer_to_binary(Value),
296 -spec metric_type_to_bin(metric_type()) ->
298 metric_type_to_bin(gauge) ->
301 -spec node_id_to_bin(node()) ->
303 node_id_to_bin(NodeID) ->
304 NodeIDBin = atom_to_binary(NodeID, utf8),
305 re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).