| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- %%--------------------------------------------------------------------
- %% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
- %%
- %% Licensed under the Apache License, Version 2.0 (the "License");
- %% you may not use this file except in compliance with the License.
- %% You may obtain a copy of the License at
- %%
- %% http://www.apache.org/licenses/LICENSE-2.0
- %%
- %% Unless required by applicable law or agreed to in writing, software
- %% distributed under the License is distributed on an "AS IS" BASIS,
- %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- %% See the License for the specific language governing permissions and
- %% limitations under the License.
- %%--------------------------------------------------------------------
- -module(emqx_plugin_libs_metrics_SUITE).
- -compile(export_all).
- -compile(nowarn_export_all).
- -include_lib("eunit/include/eunit.hrl").
- -include_lib("common_test/include/ct.hrl").
- all() ->
- emqx_common_test_helpers:all(?MODULE).
- suite() ->
- [{ct_hooks, [cth_surefire]}, {timetrap, {seconds, 30}}].
- -define(NAME, ?MODULE).
- init_per_suite(Config) ->
- emqx_common_test_helpers:start_apps([emqx_conf]),
- {ok, _} = emqx_plugin_libs_metrics:start_link(?NAME),
- Config.
- end_per_suite(_Config) ->
- catch emqx_plugin_libs_metrics:stop(?NAME),
- emqx_common_test_helpers:stop_apps([emqx_conf]),
- ok.
- init_per_testcase(_, Config) ->
- catch emqx_plugin_libs_metrics:stop(?NAME),
- {ok, _} = emqx_plugin_libs_metrics:start_link(?NAME),
- Config.
- end_per_testcase(_, _Config) ->
- ok.
- t_get_metrics(_) ->
- Metrics = [a, b, c],
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics),
- %% all the metrics are set to zero at start
- ?assertMatch(#{
- rate := #{
- a := #{current := 0.0, max := 0.0, last5m := 0.0},
- b := #{current := 0.0, max := 0.0, last5m := 0.0},
- c := #{current := 0.0, max := 0.0, last5m := 0.0}
- },
- counters := #{
- a := 0,
- b := 0,
- c := 0
- }
- }, emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
- ct:sleep(1500),
- ?LET(#{
- rate := #{
- a := #{current := CurrA, max := MaxA, last5m := _},
- b := #{current := CurrB, max := MaxB, last5m := _},
- c := #{current := CurrC, max := MaxC, last5m := _}
- },
- counters := #{
- a := 1,
- b := 1,
- c := 2
- }
- }, emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>),
- {?assert(CurrA > 0), ?assert(CurrB > 0), ?assert(CurrC > 0),
- ?assert(MaxA > 0), ?assert(MaxB > 0), ?assert(MaxC > 0)}),
- ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
- t_get_metrics_2(_) ->
- Metrics = [a, b, c],
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics,
- [a]),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
- ?assertMatch(#{
- rate := Rate = #{
- a := #{current := _, max := _, last5m := _}
- },
- counters := #{
- a := 1,
- b := 1,
- c := 1
- }
- } when map_size(Rate) =:= 1, emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
- ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
- t_recreate_metrics(_) ->
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, [a]),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
- ?assertMatch(#{
- rate := R = #{
- a := #{current := _, max := _, last5m := _}
- },
- counters := C = #{
- a := 1
- }
- } when map_size(R) == 1 andalso map_size(C) == 1,
- emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
- %% we create the metrics again, to add some counters
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, [a, b, c]),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
- ?assertMatch(#{
- rate := R = #{
- a := #{current := _, max := _, last5m := _},
- b := #{current := _, max := _, last5m := _},
- c := #{current := _, max := _, last5m := _}
- },
- counters := C = #{
- a := 1, b := 1, c := 1
- }
- } when map_size(R) == 3 andalso map_size(C) == 3,
- emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
- ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
- t_inc_matched(_) ->
- Metrics = ['rules.matched'],
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule1">>, Metrics),
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule2">>, Metrics),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule2">>, 'rules.matched'),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule2">>, 'rules.matched'),
- ?assertEqual(1, emqx_plugin_libs_metrics:get(?NAME, <<"rule1">>, 'rules.matched')),
- ?assertEqual(2, emqx_plugin_libs_metrics:get(?NAME, <<"rule2">>, 'rules.matched')),
- ?assertEqual(0, emqx_plugin_libs_metrics:get(?NAME, <<"rule3">>, 'rules.matched')),
- ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule1">>),
- ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule2">>).
- t_rate(_) ->
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule1">>, ['rules.matched']),
- ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule:2">>, ['rules.matched']),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
- ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule:2">>, 'rules.matched'),
- ?assertEqual(2, emqx_plugin_libs_metrics:get(?NAME, <<"rule1">>, 'rules.matched')),
- ct:sleep(1000),
- ?LET(#{'rules.matched' := #{max := Max, current := Current}},
- emqx_plugin_libs_metrics:get_rate(?NAME, <<"rule1">>),
- {?assert(Max =< 2),
- ?assert(Current =< 2)}),
- ct:sleep(2100),
- ?LET(#{'rules.matched' := #{max := Max, current := Current, last5m := Last5Min}}, emqx_plugin_libs_metrics:get_rate(?NAME, <<"rule1">>),
- {?assert(Max =< 2),
- ?assert(Current == 0),
- ?assert(Last5Min =< 0.67)}),
- ct:sleep(3000),
- ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule1">>),
- ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule:2">>).
|