1 -module(beam_stats_consumer_statsd).
3 -include("include/beam_stats.hrl").
4 -include("include/beam_stats_ets_table.hrl").
5 -include("include/beam_stats_process.hrl").
6 -include("include/beam_stats_process_ancestry.hrl").
7 -include("include/beam_stats_processes.hrl").
8 -include("beam_stats_logging.hrl").
10 -behaviour(beam_stats_consumer).
24 {consumption_interval , non_neg_integer()}
25 | {dst_host , inet:ip_address() | inet:hostname()}
26 | {dst_port , inet:port_number()}
27 | {src_port , inet:port_number()}
28 | {num_msgs_per_packet , non_neg_integer()}
31 -define(DEFAULT_DST_HOST, "localhost").
32 -define(DEFAULT_DST_PORT, 8125).
33 -define(DEFAULT_SRC_PORT, 8124).
35 -type metric_type() ::
36 % TODO: Add other metric types
41 , value :: non_neg_integer()
42 , type :: metric_type()
49 { sock :: hope_option:t(gen_udp:socket())
50 , dst_host :: inet:ip_address() | inet:hostname()
51 , dst_port :: inet:port_number()
52 , src_port :: inet:port_number()
53 , num_msgs_per_packet :: non_neg_integer()
59 -define(PATH_PREFIX, "beam_stats").
61 %% ============================================================================
62 %% Consumer implementation
63 %% ============================================================================
65 -spec init([option()]) ->
66 {non_neg_integer(), state()}.
68 ConsumptionInterval = hope_kv_list:get(Options, consumption_interval, 60000),
69 DstHost = hope_kv_list:get(Options, dst_host, ?DEFAULT_DST_HOST),
70 DstPort = hope_kv_list:get(Options, dst_port, ?DEFAULT_DST_PORT),
71 SrcPort = hope_kv_list:get(Options, src_port, ?DEFAULT_SRC_PORT),
72 NumMsgsPerPacket = hope_kv_list:get(Options, num_msgs_per_packet, 10),
78 , num_msgs_per_packet = NumMsgsPerPacket
80 {ConsumptionInterval, State}.
82 -spec consume(beam_stats_consumer:queue(), state()) ->
84 consume(Q, #state{num_msgs_per_packet=NumMsgsPerPacket}=State) ->
85 Packets = beam_stats_queue_to_packets(Q, NumMsgsPerPacket),
86 lists:foldl(fun try_to_connect_and_send/2, State, Packets).
88 -spec terminate(state()) ->
90 terminate(#state{sock=SockOpt}) ->
91 ok = hope_option:iter(SockOpt, fun gen_udp:close/1),
94 %% ============================================================================
96 %% ============================================================================
98 -spec try_to_connect_and_send(binary(), state()) ->
100 try_to_connect_and_send(<<Payload/binary>>, #state{}=State1) ->
101 State2 = try_to_connect_if_no_socket(State1),
102 try_to_send(State2, Payload).
104 -spec try_to_send(state(), binary()) ->
106 try_to_send(#state{sock=none}=State, _) ->
107 ?log_error("Sending failed. No socket in state."),
108 % TODO: Maybe schedule retry?
112 { sock = {some, Sock}
118 case gen_udp:send(Sock, DstHost, DstPort, Payload)
121 ; {error, _}=Error ->
123 "gen_udp:send(~p, ~p, ~p, ~p) -> ~p",
124 [Sock, DstHost, DstPort, Error]
126 % TODO: Do something with unsent messages?
127 ok = gen_udp:close(Sock),
128 State#state{sock=none}
131 -spec try_to_connect_if_no_socket(state()) ->
133 try_to_connect_if_no_socket(#state{sock={some, _}}=State) ->
135 try_to_connect_if_no_socket(#state{sock=none, src_port=SrcPort}=State) ->
136 case gen_udp:open(SrcPort)
138 State#state{sock = {some, Sock}}
139 ; {error, _}=Error ->
140 ?log_error("gen_udp:open(~p) -> ~p", [SrcPort, Error]),
141 State#state{sock = none}
144 %% ============================================================================
146 %% ============================================================================
148 -spec beam_stats_queue_to_packets(beam_stats_consumer:queue(), non_neg_integer()) ->
150 beam_stats_queue_to_packets(Q, NumMsgsPerPacket) ->
151 MsgBins = lists:append([beam_stats_to_bins(B) || B <- queue:to_list(Q)]),
152 MsgBinsChucks = hope_list:divide(MsgBins, NumMsgsPerPacket),
153 lists:map(fun erlang:iolist_to_binary/1, MsgBinsChucks).
155 -spec beam_stats_to_bins(beam_stats:t()) ->
157 beam_stats_to_bins(#beam_stats
160 , io_bytes_in = IOBytesIn
161 , io_bytes_out = IOBytesOut
162 , context_switches = ContextSwitches
163 , reductions = Reductions
164 , run_queue = RunQueue
166 , processes = Processes
169 NodeIDBin = node_id_to_bin(NodeID),
171 [ io_bytes_in_to_msg(IOBytesIn)
172 , io_bytes_out_to_msg(IOBytesOut)
173 , context_switches_to_msg(ContextSwitches)
174 , reductions_to_msg(Reductions)
175 , run_queue_to_msg(RunQueue)
176 | memory_to_msgs(Memory)
179 ++ procs_to_msgs(Processes),
180 Msgs2 = [statsd_msg_add_name_prefix(M, NodeIDBin) || M <- Msgs1],
181 [statsd_msg_to_bin(M) || M <- Msgs2].
183 -spec run_queue_to_msg(non_neg_integer()) ->
185 run_queue_to_msg(RunQueue) ->
187 { name = <<"run_queue">>
192 -spec reductions_to_msg(non_neg_integer()) ->
194 reductions_to_msg(Reductions) ->
196 { name = <<"reductions">>
201 -spec context_switches_to_msg(non_neg_integer()) ->
203 context_switches_to_msg(ContextSwitches) ->
205 { name = <<"context_switches">>
206 , value = ContextSwitches
210 -spec io_bytes_in_to_msg(non_neg_integer()) ->
212 io_bytes_in_to_msg(IOBytesIn) ->
214 { name = <<"io.bytes_in">>
219 -spec io_bytes_out_to_msg(non_neg_integer()) ->
221 io_bytes_out_to_msg(IOBytesOut) ->
223 { name = <<"io.bytes_out">>
228 -spec procs_to_msgs(beam_stats_processes:t()) ->
231 #beam_stats_processes
232 { individual_stats = Procs
233 , count_all = CountAll
234 , count_exiting = CountExiting
235 , count_garbage_collecting = CountGarbageCollecting
236 , count_registered = CountRegistered
237 , count_runnable = CountRunnable
238 , count_running = CountRunning
239 , count_suspended = CountSuspended
240 , count_waiting = CountWaiting
243 [ gauge(<<"processes_count_all">> , CountAll)
244 , gauge(<<"processes_count_exiting">> , CountExiting)
245 , gauge(<<"processes_count_garbage_collecting">>, CountGarbageCollecting)
246 , gauge(<<"processes_count_registered">> , CountRegistered)
247 , gauge(<<"processes_count_runnable">> , CountRunnable)
248 , gauge(<<"processes_count_running">> , CountRunning)
249 , gauge(<<"processes_count_suspended">> , CountSuspended)
250 , gauge(<<"processes_count_waiting">> , CountWaiting)
251 | lists:append([proc_to_msgs(P) || P <- Procs])
254 -spec proc_to_msgs(beam_stats_process:t()) ->
260 , total_heap_size = TotalHeapSize
261 , stack_size = StackSize
262 , message_queue_len = MsgQueueLen
265 Origin = beam_stats_process:get_best_known_origin(Process),
266 OriginBin = proc_origin_to_bin(Origin),
267 PidBin = pid_to_bin(Pid),
268 OriginDotPid = <<OriginBin/binary, ".", PidBin/binary>>,
269 [ gauge(<<"process_memory." , OriginDotPid/binary>>, Memory)
270 , gauge(<<"process_total_heap_size." , OriginDotPid/binary>>, TotalHeapSize)
271 , gauge(<<"process_stack_size." , OriginDotPid/binary>>, StackSize)
272 , gauge(<<"process_message_queue_len." , OriginDotPid/binary>>, MsgQueueLen)
275 -spec proc_origin_to_bin(beam_stats_process:best_known_origin()) ->
277 proc_origin_to_bin({registered_name, Name}) ->
278 atom_to_binary(Name, utf8);
279 proc_origin_to_bin({ancestry, Ancestry}) ->
280 #beam_stats_process_ancestry
281 { raw_initial_call = InitCallRaw
282 , otp_initial_call = InitCallOTPOpt
283 , otp_ancestors = AncestorsOpt
286 InitCallOTPBinOpt = hope_option:map(InitCallOTPOpt , fun mfa_to_bin/1),
287 InitCallOTPBin = hope_option:get(InitCallOTPBinOpt, Blank),
288 AncestorsBinOpt = hope_option:map(AncestorsOpt , fun ancestors_to_bin/1),
289 AncestorsBin = hope_option:get(AncestorsBinOpt , Blank),
290 InitCallRawBin = mfa_to_bin(InitCallRaw),
291 << InitCallRawBin/binary
293 , InitCallOTPBin/binary
295 , AncestorsBin/binary
298 ancestors_to_bin([]) ->
300 ancestors_to_bin([A | Ancestors]) ->
301 ABin = ancestor_to_bin(A),
302 case ancestors_to_bin(Ancestors)
305 ; <<AncestorsBin/binary>> ->
306 <<ABin/binary, "-", AncestorsBin/binary>>
309 ancestor_to_bin(A) when is_atom(A) ->
310 atom_to_binary(A, utf8);
311 ancestor_to_bin(A) when is_pid(A) ->
315 PidList = erlang:pid_to_list(Pid),
316 PidBin = re:replace(PidList, "[\.]", "_", [global, {return, binary}]),
317 re:replace(PidBin , "[><]", "" , [global, {return, binary}]).
319 -spec mfa_to_bin(mfa()) ->
321 mfa_to_bin({Module, Function, Arity}) ->
322 ModuleBin = atom_to_binary(Module , utf8),
323 FunctionBin = atom_to_binary(Function, utf8),
324 ArityBin = erlang:integer_to_binary(Arity),
325 <<ModuleBin/binary, "-", FunctionBin/binary, "-", ArityBin/binary>>.
328 -spec gauge(binary(), integer()) ->
330 gauge(<<Name/binary>>, Value) when is_integer(Value) ->
337 -spec ets_to_msgs(beam_stats_ets:t()) ->
339 ets_to_msgs(PerTableStats) ->
340 NestedMsgs = lists:map(fun ets_table_to_msgs/1, PerTableStats),
341 lists:append(NestedMsgs).
343 -spec ets_table_to_msgs(beam_stats_ets_table:t()) ->
345 ets_table_to_msgs(#beam_stats_ets_table
352 IDBin = beam_stats_ets_table:id_to_bin(ID),
353 NameBin = atom_to_binary(Name, latin1),
354 NameAndID = <<NameBin/binary, ".", IDBin/binary>>,
357 { name = <<"ets_table.size.", NameAndID/binary>>
363 { name = <<"ets_table.memory.", NameAndID/binary>>
367 [SizeMsg, MemoryMsg].
369 -spec memory_to_msgs([{atom(), non_neg_integer()}]) ->
371 memory_to_msgs(Memory) ->
372 [memory_component_to_statsd_msg(MC) || MC <- Memory].
374 -spec memory_component_to_statsd_msg({atom(), non_neg_integer()}) ->
376 memory_component_to_statsd_msg({MemType, MemSize}) when MemSize >= 0 ->
377 MemTypeBin = atom_to_binary(MemType, latin1),
379 { name = <<"memory.", MemTypeBin/binary>>
384 -spec statsd_msg_add_name_prefix(statsd_msg(), binary()) ->
386 statsd_msg_add_name_prefix(#statsd_msg{name=Name1}=Msg, <<NodeID/binary>>) ->
387 Prefix = <<?PATH_PREFIX, ".", NodeID/binary, ".">>,
388 Name2 = <<Prefix/binary, Name1/binary>>,
389 Msg#statsd_msg{name=Name2}.
391 -spec statsd_msg_to_bin(statsd_msg()) ->
395 { name = <<Name/binary>>
397 , type = Type = gauge
400 TypeBin = metric_type_to_bin(Type),
401 ValueBin = integer_to_binary(Value),
410 -spec metric_type_to_bin(metric_type()) ->
412 metric_type_to_bin(gauge) ->
415 -spec node_id_to_bin(node()) ->
417 node_id_to_bin(NodeID) ->
418 NodeIDBin = atom_to_binary(NodeID, utf8),
419 re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).