feat: aggregate anonymous process data by origin
[beam_stats.git] / test / beam_stats_consumer_statsd_SUITE.erl
CommitLineData
b0ab6ee2
SK
1-module(beam_stats_consumer_statsd_SUITE).
2
da5f2544 3-include_lib("beam_stats/include/beam_stats.hrl").
a8d431d1 4-include_lib("beam_stats/include/beam_stats_ets_table.hrl").
69ebab97
SK
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").
da5f2544 8
b0ab6ee2
SK
9-export(
10 [ all/0
11 , groups/0
12 ]).
13
14%% Test cases
15-export(
f4dcce84 16 [ t_full_cycle/1
b0ab6ee2
SK
17 ]).
18
5681263f 19-define(GROUP, beam_stats_consumer_statsd).
b0ab6ee2
SK
20
21%% ============================================================================
22%% Common Test callbacks
23%% ============================================================================
24
25all() ->
26 [{group, ?GROUP}].
27
28groups() ->
29 Tests =
f4dcce84 30 [ t_full_cycle
b0ab6ee2
SK
31 ],
32 Properties = [],
33 [{?GROUP, Properties, Tests}].
34
69ebab97 35%% ============================================================================
b0ab6ee2 36%% Test cases
69ebab97 37%% ============================================================================
b0ab6ee2 38
f4dcce84
SK
39t_full_cycle(_Cfg) ->
40 meck:new(beam_stats_source),
41 BEAMStatsExpected = meck_expect_beam_stats(),
42 BEAMStatsComputed = beam_stats_state:export(beam_stats_state:new()),
43 ct:log("BEAMStatsExpected: ~p~n", [BEAMStatsExpected]),
44 ct:log("BEAMStatsComputed: ~p~n", [BEAMStatsComputed]),
45 BEAMStatsExpected = BEAMStatsComputed,
46
47 {ok,[hope,beam_stats]} = application:ensure_all_started(beam_stats),
48 ct:log("beam_stats started~n"),
da5f2544
SK
49 ServerPort = 8125,
50 {ok, ServerSocket} = gen_udp:open(ServerPort, [binary, {active, false}]),
f4dcce84
SK
51 ct:log("UDP server started started~n"),
52 {ok, _} = beam_stats_consumer:add(beam_stats_consumer_statsd,
53 [ {consumption_interval , 60000}
54 , {dst_host , "localhost"}
55 , {dst_port , ServerPort}
56 , {src_port , 8124}
57 , {num_msgs_per_packet , 10}
58 ]
59 ),
60 ct:log("consumer added~n"),
61 _ = meck_expect_beam_stats(
62 % Double the original values, so that deltas will equal originals after
63 % 1 update of new beam_stats_state:t()
64 [ {io_bytes_in , 6}
65 , {io_bytes_out , 14}
66 , {context_switches , 10}
67 ]
68 ),
69 ct:log("meck_expect_beam_stats ok~n"),
70 {} = beam_stats_producer:sync_produce_consume(),
71 ct:log("produced and consumed~n"),
72 ok = application:stop(beam_stats),
73 ct:log("beam_stats stopped~n"),
74
4d24f3b7 75 ResultOfReceive1 = gen_udp:recv(ServerSocket, 0),
4d24f3b7 76 ResultOfReceive2 = gen_udp:recv(ServerSocket, 0),
69ebab97
SK
77 ResultOfReceive3 = gen_udp:recv(ServerSocket, 0),
78 ResultOfReceive4 = gen_udp:recv(ServerSocket, 0),
da5f2544 79 ok = gen_udp:close(ServerSocket),
69ebab97
SK
80 {ok, {_, _, PacketReceived1}} = ResultOfReceive1,
81 {ok, {_, _, PacketReceived2}} = ResultOfReceive2,
82 {ok, {_, _, PacketReceived3}} = ResultOfReceive3,
83 {ok, {_, _, PacketReceived4}} = ResultOfReceive4,
4d24f3b7
SK
84 ct:log("PacketReceived1: ~n~s~n", [PacketReceived1]),
85 ct:log("PacketReceived2: ~n~s~n", [PacketReceived2]),
69ebab97
SK
86 ct:log("PacketReceived3: ~n~s~n", [PacketReceived3]),
87 ct:log("PacketReceived4: ~n~s~n", [PacketReceived4]),
88 PacketsCombined =
89 << PacketReceived1/binary
90 , PacketReceived2/binary
91 , PacketReceived3/binary
92 , PacketReceived4/binary
93 >>,
4d24f3b7
SK
94 ct:log("PacketsCombined: ~n~s~n", [PacketsCombined]),
95 MsgsExpected =
96 [ <<"beam_stats.node_foo_host_bar.io.bytes_in:3|g">>
97 , <<"beam_stats.node_foo_host_bar.io.bytes_out:7|g">>
98 , <<"beam_stats.node_foo_host_bar.context_switches:5|g">>
99 , <<"beam_stats.node_foo_host_bar.reductions:9|g">>
100 , <<"beam_stats.node_foo_host_bar.run_queue:17|g">>
101 , <<"beam_stats.node_foo_host_bar.memory.mem_type_foo:1|g">>
102 , <<"beam_stats.node_foo_host_bar.memory.mem_type_bar:2|g">>
103 , <<"beam_stats.node_foo_host_bar.memory.mem_type_baz:3|g">>
104 , <<"beam_stats.node_foo_host_bar.ets_table.size.foo.foo:5|g">>
f4dcce84 105 , <<"beam_stats.node_foo_host_bar.ets_table.memory.foo.foo:40|g">>
4d24f3b7 106 , <<"beam_stats.node_foo_host_bar.ets_table.size.bar.37:8|g">>
f4dcce84 107 , <<"beam_stats.node_foo_host_bar.ets_table.memory.bar.37:64|g">>
69ebab97
SK
108
109 % Processes totals
697c496d 110 , <<"beam_stats.node_foo_host_bar.processes_count_all:4|g">>
69ebab97
SK
111 , <<"beam_stats.node_foo_host_bar.processes_count_exiting:0|g">>
112 , <<"beam_stats.node_foo_host_bar.processes_count_garbage_collecting:0|g">>
113 , <<"beam_stats.node_foo_host_bar.processes_count_registered:1|g">>
114 , <<"beam_stats.node_foo_host_bar.processes_count_runnable:0|g">>
115 , <<"beam_stats.node_foo_host_bar.processes_count_running:3|g">>
116 , <<"beam_stats.node_foo_host_bar.processes_count_suspended:0|g">>
697c496d 117 , <<"beam_stats.node_foo_host_bar.processes_count_waiting:1|g">>
69ebab97
SK
118
119 % Process 1
f65a3845
SK
120 , <<"beam_stats.node_foo_host_bar.process_memory.named--reg_name_foo:15|g">>
121 , <<"beam_stats.node_foo_host_bar.process_total_heap_size.named--reg_name_foo:25|g">>
122 , <<"beam_stats.node_foo_host_bar.process_stack_size.named--reg_name_foo:10|g">>
123 , <<"beam_stats.node_foo_host_bar.process_message_queue_len.named--reg_name_foo:0|g">>
69ebab97
SK
124
125 % Process 2
f65a3845
SK
126 , <<"beam_stats.node_foo_host_bar.process_memory.spawned-via--bar_mod-bar_fun-1--NONE--NONE:25|g">>
127 , <<"beam_stats.node_foo_host_bar.process_total_heap_size.spawned-via--bar_mod-bar_fun-1--NONE--NONE:35|g">>
128 , <<"beam_stats.node_foo_host_bar.process_stack_size.spawned-via--bar_mod-bar_fun-1--NONE--NONE:40|g">>
129 , <<"beam_stats.node_foo_host_bar.process_message_queue_len.spawned-via--bar_mod-bar_fun-1--NONE--NONE:5|g">>
69ebab97 130
697c496d
SK
131 % Process 3 and 4, aggregated by origin
132 , <<"beam_stats.node_foo_host_bar.process_memory.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0:30|g">>
133 , <<"beam_stats.node_foo_host_bar.process_total_heap_size.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0:45|g">>
134 , <<"beam_stats.node_foo_host_bar.process_stack_size.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0:55|g">>
f65a3845 135 , <<"beam_stats.node_foo_host_bar.process_message_queue_len.spawned-via--baz_mod-baz_fun-3--baz_otp_mod-baz_otp_fun-2--0_0_0-0_1_0:1|g">>
4d24f3b7
SK
136 ],
137 MsgsReceived = binary:split(PacketsCombined, <<"\n">>, [global, trim]),
138 RemoveExpectedFromReceived =
139 fun (Expected, Received) ->
140 ct:log(
141 "Looking for expected msg ~p in remaining received ~p~n",
142 [Expected, Received]
143 ),
144 true = lists:member(Expected, Received),
145 Received -- [Expected]
146 end,
f4dcce84
SK
147 [] = lists:foldl(RemoveExpectedFromReceived, MsgsReceived, MsgsExpected),
148 meck:unload(beam_stats_source).
149
150meck_expect_beam_stats() ->
151 meck_expect_beam_stats([]).
7786d23b 152
f4dcce84
SK
153meck_expect_beam_stats(Overrides) ->
154 IOBytesIn = hope_kv_list:get(Overrides, io_bytes_in , 3),
155 IOBytesOut = hope_kv_list:get(Overrides, io_bytes_out, 7),
156 ContextSwitches = hope_kv_list:get(Overrides, context_switches, 5),
7786d23b
SK
157 Pid0 = list_to_pid("<0.0.0>"),
158 Pid1 = list_to_pid("<0.1.0>"),
159 Pid2 = list_to_pid("<0.2.0>"),
160 Pid3 = list_to_pid("<0.3.0>"),
697c496d 161 Pid4 = list_to_pid("<0.4.0>"),
7786d23b
SK
162 Process1 =
163 #beam_stats_process
164 { pid = Pid1
165 , registered_name = {some, reg_name_foo}
166 , ancestry =
167 #beam_stats_process_ancestry
168 { raw_initial_call = {foo_mod, foo_fun, 2}
169 , otp_initial_call = none
170 , otp_ancestors = none
171 }
172 , status = running
173 , memory = 15
174 , total_heap_size = 25
175 , stack_size = 10
176 , message_queue_len = 0
177 },
178 Process2 =
179 #beam_stats_process
180 { pid = Pid2
181 , registered_name = none
182 , ancestry =
183 #beam_stats_process_ancestry
184 { raw_initial_call = {bar_mod, bar_fun, 1}
185 , otp_initial_call = none
186 , otp_ancestors = none
187 }
188 , status = running
189 , memory = 25
190 , total_heap_size = 35
191 , stack_size = 40
192 , message_queue_len = 5
193 },
194 Process3 =
195 #beam_stats_process
196 { pid = Pid3
197 , registered_name = none
198 , ancestry =
199 #beam_stats_process_ancestry
200 { raw_initial_call = {baz_mod, baz_fun, 3}
201 , otp_initial_call = {some, {baz_otp_mod, baz_otp_fun, 2}}
202 , otp_ancestors = {some, [Pid0, Pid1]}
203 }
204 , status = running
205 , memory = 25
206 , total_heap_size = 35
207 , stack_size = 40
208 , message_queue_len = 1
209 },
697c496d
SK
210 Process4 =
211 #beam_stats_process
212 { pid = Pid4
213 , registered_name = none
214 , ancestry =
215 #beam_stats_process_ancestry
216 { raw_initial_call = {baz_mod, baz_fun, 3}
217 , otp_initial_call = {some, {baz_otp_mod, baz_otp_fun, 2}}
218 , otp_ancestors = {some, [Pid0, Pid1]}
219 }
220 , status = waiting
221 , memory = 5
222 , total_heap_size = 10
223 , stack_size = 15
224 , message_queue_len = 0
225 },
7786d23b
SK
226 Processes =
227 #beam_stats_processes
228 { individual_stats =
229 [ Process1
230 , Process2
231 , Process3
697c496d 232 , Process4
7786d23b 233 ]
697c496d 234 , count_all = 4
7786d23b
SK
235 , count_exiting = 0
236 , count_garbage_collecting = 0
237 , count_registered = 1
238 , count_runnable = 0
239 , count_running = 3
240 , count_suspended = 0
697c496d 241 , count_waiting = 1
7786d23b
SK
242 },
243 ETSTableStatsFoo =
244 #beam_stats_ets_table
245 { id = foo
246 , name = foo
247 , size = 5
248 , memory = 40
249 },
250 ETSTableStatsBar =
251 #beam_stats_ets_table
252 { id = 37
253 , name = bar
254 , size = 8
255 , memory = 64
256 },
7786d23b
SK
257 meck:expect(beam_stats_source, erlang_memory,
258 fun () -> [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}] end),
259 meck:expect(beam_stats_source, erlang_node,
260 fun () -> 'node_foo@host_bar' end),
261 meck:expect(beam_stats_source, erlang_registered,
262 fun () -> [reg_name_foo] end),
263 meck:expect(beam_stats_source, erlang_statistics,
f4dcce84
SK
264 fun (io ) -> {{input, IOBytesIn}, {output, IOBytesOut}}
265 ; (context_switches) -> {ContextSwitches, 0}
7786d23b
SK
266 ; (reductions ) -> {0, 9} % 1st element is unused
267 ; (run_queue ) -> 17
268 end
269 ),
270 meck:expect(beam_stats_source, ets_all,
271 fun () -> [foo, 37] end),
272 meck:expect(beam_stats_source, erlang_system_info,
273 fun (wordsize) -> 8 end),
274 meck:expect(beam_stats_source, ets_info,
275 fun (foo, memory) -> 5
276 ; (foo, name ) -> foo
277 ; (foo, size ) -> 5
278 ; (37 , memory) -> 8
279 ; (37 , name ) -> bar
280 ; (37 , size ) -> 8
281 end
282 ),
283 meck:expect(beam_stats_source, erlang_processes,
697c496d 284 fun () -> [Pid1, Pid2, Pid3, Pid4] end),
7786d23b
SK
285 meck:expect(beam_stats_source, os_timestamp,
286 fun () -> {1, 2, 3} end),
287 meck:expect(beam_stats_source, erlang_process_info,
288 fun (P, K) when P == Pid1 ->
289 case K
290 of dictionary -> {K, []}
291 ; initial_call -> {K, {foo_mod, foo_fun, 2}}
292 ; registered_name -> {K, reg_name_foo}
293 ; status -> {K, running}
294 ; memory -> {K, 15}
295 ; total_heap_size -> {K, 25}
296 ; stack_size -> {K, 10}
297 ; message_queue_len -> {K, 0}
298 end
299 ; (P, K) when P == Pid2 ->
300 case K
301 of dictionary -> {K, []}
302 ; initial_call -> {K, {bar_mod, bar_fun, 1}}
303 ; registered_name -> []
304 ; status -> {K, running}
305 ; memory -> {K, 25}
306 ; total_heap_size -> {K, 35}
307 ; stack_size -> {K, 40}
308 ; message_queue_len -> {K, 5}
309 end
310 ; (P, K) when P == Pid3 ->
311 Dict =
312 [ {'$initial_call', {baz_otp_mod, baz_otp_fun, 2}}
313 , {'$ancestors' , [Pid0, Pid1]}
314 ],
315 case K
316 of dictionary -> {K, Dict}
317 ; initial_call -> {K, {baz_mod, baz_fun, 3}}
318 ; registered_name -> []
319 ; status -> {K, running}
320 ; memory -> {K, 25}
321 ; total_heap_size -> {K, 35}
322 ; stack_size -> {K, 40}
323 ; message_queue_len -> {K, 1}
324 end
697c496d
SK
325 ; (P, K) when P == Pid4 ->
326 Dict =
327 [ {'$initial_call', {baz_otp_mod, baz_otp_fun, 2}}
328 , {'$ancestors' , [Pid0, Pid1]}
329 ],
330 case K
331 of dictionary -> {K, Dict}
332 ; initial_call -> {K, {baz_mod, baz_fun, 3}}
333 ; registered_name -> []
334 ; status -> {K, waiting}
335 ; memory -> {K, 5}
336 ; total_heap_size -> {K, 10}
337 ; stack_size -> {K, 15}
338 ; message_queue_len -> {K, 0}
339 end
7786d23b
SK
340 end
341 ),
f4dcce84
SK
342 #beam_stats
343 { timestamp = {1, 2, 3}
344 , node_id = 'node_foo@host_bar'
345 , memory = [{mem_type_foo, 1}, {mem_type_bar, 2}, {mem_type_baz, 3}]
346 , io_bytes_in = IOBytesIn
347 , io_bytes_out = IOBytesOut
348 , context_switches = ContextSwitches
349 , reductions = 9
350 , run_queue = 17
351 , ets = [ETSTableStatsFoo, ETSTableStatsBar]
352 , processes = Processes
353 }.
This page took 0.044734 seconds and 4 git commands to generate.