1 -module(beam_stats_consumer_statsd).
3 -include("include/beam_stats.hrl").
4 -include("beam_stats_logging.hrl").
6 -behaviour(beam_stats_consumer).
20 {consumption_interval , non_neg_integer()}
21 | {dst_host , inet:ip_address() | inet:hostname()}
22 | {dst_port , inet:port_number()}
23 | {src_port , inet:port_number()}
26 -define(DEFAULT_DST_HOST, "localhost").
27 -define(DEFAULT_DST_PORT, 8125).
28 -define(DEFAULT_SRC_PORT, 8124).
30 -type metric_type() ::
31 % TODO: Add other metric types
36 , value :: non_neg_integer()
37 , type :: metric_type()
44 { sock :: hope_option:t(gen_udp:socket())
45 , dst_host :: inet:ip_address() | inet:hostname()
46 , dst_port :: inet:port_number()
47 , src_port :: inet:port_number()
53 -define(PATH_PREFIX, "beam_stats").
55 %% ============================================================================
56 %% Consumer implementation
57 %% ============================================================================
59 -spec init([option()]) ->
60 {non_neg_integer(), state()}.
62 ConsumptionInterval = hope_kv_list:get(Options, consumption_interval, 60000),
63 DstHost = hope_kv_list:get(Options, dst_host, ?DEFAULT_DST_HOST),
64 DstPort = hope_kv_list:get(Options, dst_port, ?DEFAULT_DST_PORT),
65 SrcPort = hope_kv_list:get(Options, src_port, ?DEFAULT_SRC_PORT),
72 {ConsumptionInterval, State}.
74 -spec consume(beam_stats_consumer:queue(), state()) ->
76 consume(Q, #state{}=State1) ->
77 Payload = beam_stats_queue_to_binary(Q),
78 State2 = try_to_connect_if_no_socket(State1),
79 try_to_send(State2, Payload).
81 -spec terminate(state()) ->
83 terminate(#state{sock=SockOpt}) ->
84 ok = hope_option:iter(SockOpt, fun gen_udp:close/1),
87 %% ============================================================================
89 %% ============================================================================
91 -spec try_to_send(state(), binary()) ->
93 try_to_send(#state{sock=none}=State, _) ->
94 ?log_error("Sending failed. No socket in state."),
95 % TODO: Maybe schedule retry?
105 case gen_udp:send(Sock, DstHost, DstPort, Payload)
108 ; {error, _}=Error ->
110 "gen_udp:send(~p, ~p, ~p, ~p) -> ~p",
111 [Sock, DstHost, DstPort, Error]
113 % TODO: Do something with unsent messages?
114 ok = gen_udp:close(Sock),
115 State#state{sock=none}
118 -spec try_to_connect_if_no_socket(state()) ->
120 try_to_connect_if_no_socket(#state{sock={some, _}}=State) ->
122 try_to_connect_if_no_socket(#state{sock=none, src_port=SrcPort}=State) ->
123 case gen_udp:open(SrcPort)
125 State#state{sock = {some, Sock}}
126 ; {error, _}=Error ->
127 ?log_error("gen_udp:open(~p) -> ~p", [SrcPort, Error]),
128 State#state{sock = none}
131 %% ============================================================================
133 %% ============================================================================
135 -spec beam_stats_queue_to_binary(beam_stats_consumer:queue()) ->
137 beam_stats_queue_to_binary(Q) ->
138 iolist_to_binary([beam_stats_to_bins(B) || B <- queue:to_list(Q)]).
140 -spec beam_stats_to_bins(beam_stats:t()) ->
142 beam_stats_to_bins(#beam_stats
145 , io_bytes_in = IOBytesIn
146 , io_bytes_out = IOBytesOut
147 , context_switches = ContextSwitches
148 , reductions = Reductions
149 , run_queue = RunQueue
152 NodeIDBin = node_id_to_bin(NodeID),
154 [ io_bytes_in_to_msg(IOBytesIn)
155 , io_bytes_out_to_msg(IOBytesOut)
156 , context_switches_to_msg(ContextSwitches)
157 , reductions_to_msg(Reductions)
158 , run_queue_to_msg(RunQueue)
159 | memory_to_msgs(Memory)
161 Msgs2 = [statsd_msg_add_name_prefix(M, NodeIDBin) || M <- Msgs1],
162 [statsd_msg_to_bin(M) || M <- Msgs2].
164 -spec run_queue_to_msg(non_neg_integer()) ->
166 run_queue_to_msg(RunQueue) ->
168 { name = <<"run_queue">>
173 -spec reductions_to_msg(non_neg_integer()) ->
175 reductions_to_msg(Reductions) ->
177 { name = <<"reductions">>
182 -spec context_switches_to_msg(non_neg_integer()) ->
184 context_switches_to_msg(ContextSwitches) ->
186 { name = <<"context_switches">>
187 , value = ContextSwitches
191 -spec io_bytes_in_to_msg(non_neg_integer()) ->
193 io_bytes_in_to_msg(IOBytesIn) ->
195 { name = <<"io.bytes_in">>
200 -spec io_bytes_out_to_msg(non_neg_integer()) ->
202 io_bytes_out_to_msg(IOBytesOut) ->
204 { name = <<"io.bytes_out">>
209 -spec memory_to_msgs([{atom(), non_neg_integer()}]) ->
211 memory_to_msgs(Memory) ->
212 [memory_component_to_statsd_msg(MC) || MC <- Memory].
214 -spec memory_component_to_statsd_msg({atom(), non_neg_integer()}) ->
216 memory_component_to_statsd_msg({MemType, MemSize}) when MemSize >= 0 ->
217 MemTypeBin = atom_to_binary(MemType, latin1),
219 { name = <<"memory.", MemTypeBin/binary>>
224 -spec statsd_msg_add_name_prefix(statsd_msg(), binary()) ->
226 statsd_msg_add_name_prefix(#statsd_msg{name=Name1}=Msg, <<NodeID/binary>>) ->
227 Prefix = <<?PATH_PREFIX, ".", NodeID/binary, ".">>,
228 Name2 = <<Prefix/binary, Name1/binary>>,
229 Msg#statsd_msg{name=Name2}.
231 -spec statsd_msg_to_bin(statsd_msg()) ->
235 { name = <<Name/binary>>
237 , type = Type = gauge
240 TypeBin = metric_type_to_bin(Type),
241 ValueBin = integer_to_binary(Value),
250 -spec metric_type_to_bin(metric_type()) ->
252 metric_type_to_bin(gauge) ->
255 -spec node_id_to_bin(node()) ->
257 node_id_to_bin(NodeID) ->
258 NodeIDBin = atom_to_binary(NodeID, utf8),
259 re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).