1 -module(beam_stats_consumer_statsd_SUITE).
3 -include_lib("beam_stats/include/beam_stats.hrl").
4 -include_lib("beam_stats/include/beam_stats_ets_table.hrl").
5 -include_lib("beam_stats/include/beam_stats_process.hrl").
6 -include_lib("beam_stats/include/beam_stats_process_ancestry.hrl").
7 -include_lib("beam_stats/include/beam_stats_processes.hrl").
20 -define(GROUP, beam_stats_consumer_statsd).
22 %% ============================================================================
23 %% Common Test callbacks
24 %% ============================================================================
35 [{?GROUP, Properties, Tests}].
37 %% ============================================================================
39 %% ============================================================================
42 Pid0 = list_to_pid("<0.0.0>"),
43 Pid1 = list_to_pid("<0.1.0>"),
44 Pid2 = list_to_pid("<0.2.0>"),
45 Pid3 = list_to_pid("<0.3.0>"),
49 , registered_name = {some, reg_name_foo}
51 #beam_stats_process_ancestry
52 { raw_initial_call = {foo_mod, foo_fun, 2}
53 , otp_initial_call = none
54 , otp_ancestors = none
58 , total_heap_size = 25
60 , message_queue_len = 0
65 , registered_name = none
67 #beam_stats_process_ancestry
68 { raw_initial_call = {bar_mod, bar_fun, 1}
69 , otp_initial_call = none
70 , otp_ancestors = none
74 , total_heap_size = 35
76 , message_queue_len = 5
81 , registered_name = none
83 #beam_stats_process_ancestry
84 { raw_initial_call = {baz_mod, baz_fun, 3}
85 , otp_initial_call = {some, {baz_otp_mod, baz_otp_fun, 2}}
86 , otp_ancestors = {some, [Pid0, Pid1]}
90 , total_heap_size = 35
92 , message_queue_len = 1
103 , count_garbage_collecting = 0
104 , count_registered = 1
107 , count_suspended = 0
111 #beam_stats_ets_table
118 #beam_stats_ets_table
124 % TODO: Indent #beam_stats as #beam_stats_ets_table
125 BEAMStats = #beam_stats
126 { timestamp = {1, 2, 3}
127 , node_id = 'node_foo@host_bar'
128 , memory = [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}]
131 , context_switches = 5
134 , ets = [ETSTableStatsFoo, ETSTableStatsBar]
135 , processes = Processes
138 {ok, ServerSocket} = gen_udp:open(ServerPort, [binary, {active, false}]),
139 BEAMStatsQ = queue:in(BEAMStats, queue:new()),
140 Options = [{dst_port, ServerPort}],
141 {_, State1} = beam_stats_consumer_statsd:init(Options),
142 State2 = beam_stats_consumer_statsd:consume(BEAMStatsQ, State1),
143 {} = beam_stats_consumer_statsd:terminate(State2),
144 ResultOfReceive1 = gen_udp:recv(ServerSocket, 0),
145 ResultOfReceive2 = gen_udp:recv(ServerSocket, 0),
146 ResultOfReceive3 = gen_udp:recv(ServerSocket, 0),
147 ResultOfReceive4 = gen_udp:recv(ServerSocket, 0),
148 ok = gen_udp:close(ServerSocket),
149 {ok, {_, _, PacketReceived1}} = ResultOfReceive1,
150 {ok, {_, _, PacketReceived2}} = ResultOfReceive2,
151 {ok, {_, _, PacketReceived3}} = ResultOfReceive3,
152 {ok, {_, _, PacketReceived4}} = ResultOfReceive4,
153 ct:log("PacketReceived1: ~n~s~n", [PacketReceived1]),
154 ct:log("PacketReceived2: ~n~s~n", [PacketReceived2]),
155 ct:log("PacketReceived3: ~n~s~n", [PacketReceived3]),
156 ct:log("PacketReceived4: ~n~s~n", [PacketReceived4]),
158 << PacketReceived1/binary
159 , PacketReceived2/binary
160 , PacketReceived3/binary
161 , PacketReceived4/binary
163 ct:log("PacketsCombined: ~n~s~n", [PacketsCombined]),
165 [ <<"beam_stats.node_foo_host_bar.io.bytes_in:3|g">>
166 , <<"beam_stats.node_foo_host_bar.io.bytes_out:7|g">>
167 , <<"beam_stats.node_foo_host_bar.context_switches:5|g">>
168 , <<"beam_stats.node_foo_host_bar.reductions:9|g">>
169 , <<"beam_stats.node_foo_host_bar.run_queue:17|g">>
170 , <<"beam_stats.node_foo_host_bar.memory.mem_type_foo:1|g">>
171 , <<"beam_stats.node_foo_host_bar.memory.mem_type_bar:2|g">>
172 , <<"beam_stats.node_foo_host_bar.memory.mem_type_baz:3|g">>
173 , <<"beam_stats.node_foo_host_bar.ets_table.size.foo.foo:5|g">>
174 , <<"beam_stats.node_foo_host_bar.ets_table.memory.foo.foo:25|g">>
175 , <<"beam_stats.node_foo_host_bar.ets_table.size.bar.37:8|g">>
176 , <<"beam_stats.node_foo_host_bar.ets_table.memory.bar.37:38|g">>
179 , <<"beam_stats.node_foo_host_bar.processes_count_all:3|g">>
180 , <<"beam_stats.node_foo_host_bar.processes_count_exiting:0|g">>
181 , <<"beam_stats.node_foo_host_bar.processes_count_garbage_collecting:0|g">>
182 , <<"beam_stats.node_foo_host_bar.processes_count_registered:1|g">>
183 , <<"beam_stats.node_foo_host_bar.processes_count_runnable:0|g">>
184 , <<"beam_stats.node_foo_host_bar.processes_count_running:3|g">>
185 , <<"beam_stats.node_foo_host_bar.processes_count_suspended:0|g">>
186 , <<"beam_stats.node_foo_host_bar.processes_count_waiting:0|g">>
189 , <<"beam_stats.node_foo_host_bar.process_memory.reg_name_foo.0_1_0:15|g">>
190 , <<"beam_stats.node_foo_host_bar.process_total_heap_size.reg_name_foo.0_1_0:25|g">>
191 , <<"beam_stats.node_foo_host_bar.process_stack_size.reg_name_foo.0_1_0:10|g">>
192 , <<"beam_stats.node_foo_host_bar.process_message_queue_len.reg_name_foo.0_1_0:0|g">>
195 , <<"beam_stats.node_foo_host_bar.process_memory.bar_mod-bar_fun-1--NONE--NONE.0_2_0:25|g">>
196 , <<"beam_stats.node_foo_host_bar.process_total_heap_size.bar_mod-bar_fun-1--NONE--NONE.0_2_0:35|g">>
197 , <<"beam_stats.node_foo_host_bar.process_stack_size.bar_mod-bar_fun-1--NONE--NONE.0_2_0:40|g">>
198 , <<"beam_stats.node_foo_host_bar.process_message_queue_len.bar_mod-bar_fun-1--NONE--NONE.0_2_0:5|g">>
201 , <<"beam_stats.node_foo_host_bar.process_memory.baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0.0_3_0:25|g">>
202 , <<"beam_stats.node_foo_host_bar.process_total_heap_size.baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0.0_3_0:35|g">>
203 , <<"beam_stats.node_foo_host_bar.process_stack_size.baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0.0_3_0:40|g">>
204 , <<"beam_stats.node_foo_host_bar.process_message_queue_len.baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0.0_3_0:1|g">>
206 MsgsReceived = binary:split(PacketsCombined, <<"\n">>, [global, trim]),
207 RemoveExpectedFromReceived =
208 fun (Expected, Received) ->
210 "Looking for expected msg ~p in remaining received ~p~n",
213 true = lists:member(Expected, Received),
214 Received -- [Expected]
216 [] = lists:foldl(RemoveExpectedFromReceived, MsgsReceived, MsgsExpected).
219 Pid0 = list_to_pid("<0.0.0>"),
220 Pid1 = list_to_pid("<0.1.0>"),
221 Pid2 = list_to_pid("<0.2.0>"),
222 Pid3 = list_to_pid("<0.3.0>"),
226 , registered_name = {some, reg_name_foo}
228 #beam_stats_process_ancestry
229 { raw_initial_call = {foo_mod, foo_fun, 2}
230 , otp_initial_call = none
231 , otp_ancestors = none
235 , total_heap_size = 25
237 , message_queue_len = 0
242 , registered_name = none
244 #beam_stats_process_ancestry
245 { raw_initial_call = {bar_mod, bar_fun, 1}
246 , otp_initial_call = none
247 , otp_ancestors = none
251 , total_heap_size = 35
253 , message_queue_len = 5
258 , registered_name = none
260 #beam_stats_process_ancestry
261 { raw_initial_call = {baz_mod, baz_fun, 3}
262 , otp_initial_call = {some, {baz_otp_mod, baz_otp_fun, 2}}
263 , otp_ancestors = {some, [Pid0, Pid1]}
267 , total_heap_size = 35
269 , message_queue_len = 1
272 #beam_stats_processes
280 , count_garbage_collecting = 0
281 , count_registered = 1
284 , count_suspended = 0
288 #beam_stats_ets_table
295 #beam_stats_ets_table
301 meck:new(beam_stats_source),
302 meck:expect(beam_stats_source, erlang_memory,
303 fun () -> [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}] end),
304 meck:expect(beam_stats_source, erlang_node,
305 fun () -> 'node_foo@host_bar' end),
306 meck:expect(beam_stats_source, erlang_registered,
307 fun () -> [reg_name_foo] end),
308 meck:expect(beam_stats_source, erlang_statistics,
309 fun (io ) -> {{input, 3}, {output, 7}}
310 ; (context_switches) -> {5, 0}
311 ; (reductions ) -> {0, 9} % 1st element is unused
315 meck:expect(beam_stats_source, ets_all,
316 fun () -> [foo, 37] end),
317 meck:expect(beam_stats_source, erlang_system_info,
318 fun (wordsize) -> 8 end),
319 meck:expect(beam_stats_source, ets_info,
320 fun (foo, memory) -> 5
321 ; (foo, name ) -> foo
324 ; (37 , name ) -> bar
328 meck:expect(beam_stats_source, erlang_processes,
329 fun () -> [Pid1, Pid2, Pid3] end),
330 meck:expect(beam_stats_source, os_timestamp,
331 fun () -> {1, 2, 3} end),
332 meck:expect(beam_stats_source, erlang_process_info,
333 fun (P, K) when P == Pid1 ->
335 of dictionary -> {K, []}
336 ; initial_call -> {K, {foo_mod, foo_fun, 2}}
337 ; registered_name -> {K, reg_name_foo}
338 ; status -> {K, running}
340 ; total_heap_size -> {K, 25}
341 ; stack_size -> {K, 10}
342 ; message_queue_len -> {K, 0}
344 ; (P, K) when P == Pid2 ->
346 of dictionary -> {K, []}
347 ; initial_call -> {K, {bar_mod, bar_fun, 1}}
348 ; registered_name -> []
349 ; status -> {K, running}
351 ; total_heap_size -> {K, 35}
352 ; stack_size -> {K, 40}
353 ; message_queue_len -> {K, 5}
355 ; (P, K) when P == Pid3 ->
357 [ {'$initial_call', {baz_otp_mod, baz_otp_fun, 2}}
358 , {'$ancestors' , [Pid0, Pid1]}
361 of dictionary -> {K, Dict}
362 ; initial_call -> {K, {baz_mod, baz_fun, 3}}
363 ; registered_name -> []
364 ; status -> {K, running}
366 ; total_heap_size -> {K, 35}
367 ; stack_size -> {K, 40}
368 ; message_queue_len -> {K, 1}
374 { timestamp = {1, 2, 3}
375 , node_id = 'node_foo@host_bar'
376 , memory = [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}]
379 , context_switches = 5
382 , ets = [ETSTableStatsFoo, ETSTableStatsBar]
383 , processes = Processes
385 BEAMStatsComputed = beam_stats_state:export(beam_stats_state:new()),
386 ct:log("BEAMStatsExpected: ~p~n", [BEAMStatsExpected]),
387 ct:log("BEAMStatsComputed: ~p~n", [BEAMStatsComputed]),
388 BEAMStatsExpected = BEAMStatsComputed,
389 meck:unload(beam_stats_source).