emqx_plugin_libs_metrics_SUITE.erl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_plugin_libs_metrics_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. all() ->
  22. emqx_common_test_helpers:all(?MODULE).
  23. suite() ->
  24. [{ct_hooks, [cth_surefire]}, {timetrap, {seconds, 30}}].
  25. -define(NAME, ?MODULE).
  26. init_per_suite(Config) ->
  27. emqx_common_test_helpers:start_apps([emqx_conf]),
  28. {ok, _} = emqx_plugin_libs_metrics:start_link(?NAME),
  29. Config.
  30. end_per_suite(_Config) ->
  31. catch emqx_plugin_libs_metrics:stop(?NAME),
  32. emqx_common_test_helpers:stop_apps([emqx_conf]),
  33. ok.
  34. init_per_testcase(_, Config) ->
  35. catch emqx_plugin_libs_metrics:stop(?NAME),
  36. {ok, _} = emqx_plugin_libs_metrics:start_link(?NAME),
  37. Config.
  38. end_per_testcase(_, _Config) ->
  39. ok.
  40. t_get_metrics(_) ->
  41. Metrics = [a, b, c],
  42. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics),
  43. %% all the metrics are set to zero at start
  44. ?assertMatch(#{
  45. rate := #{
  46. a := #{current := 0.0, max := 0.0, last5m := 0.0},
  47. b := #{current := 0.0, max := 0.0, last5m := 0.0},
  48. c := #{current := 0.0, max := 0.0, last5m := 0.0}
  49. },
  50. counters := #{
  51. a := 0,
  52. b := 0,
  53. c := 0
  54. }
  55. }, emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
  56. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
  57. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
  58. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
  59. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
  60. ct:sleep(1500),
  61. ?LET(#{
  62. rate := #{
  63. a := #{current := CurrA, max := MaxA, last5m := _},
  64. b := #{current := CurrB, max := MaxB, last5m := _},
  65. c := #{current := CurrC, max := MaxC, last5m := _}
  66. },
  67. counters := #{
  68. a := 1,
  69. b := 1,
  70. c := 2
  71. }
  72. }, emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>),
  73. {?assert(CurrA > 0), ?assert(CurrB > 0), ?assert(CurrC > 0),
  74. ?assert(MaxA > 0), ?assert(MaxB > 0), ?assert(MaxC > 0)}),
  75. ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
  76. t_get_metrics_2(_) ->
  77. Metrics = [a, b, c],
  78. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, Metrics,
  79. [a]),
  80. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
  81. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
  82. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
  83. ?assertMatch(#{
  84. rate := Rate = #{
  85. a := #{current := _, max := _, last5m := _}
  86. },
  87. counters := #{
  88. a := 1,
  89. b := 1,
  90. c := 1
  91. }
  92. } when map_size(Rate) =:= 1, emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
  93. ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
  94. t_recreate_metrics(_) ->
  95. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, [a]),
  96. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, a),
  97. ?assertMatch(#{
  98. rate := R = #{
  99. a := #{current := _, max := _, last5m := _}
  100. },
  101. counters := C = #{
  102. a := 1
  103. }
  104. } when map_size(R) == 1 andalso map_size(C) == 1,
  105. emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
  106. %% we create the metrics again, to add some counters
  107. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"testid">>, [a, b, c]),
  108. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, b),
  109. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"testid">>, c),
  110. ?assertMatch(#{
  111. rate := R = #{
  112. a := #{current := _, max := _, last5m := _},
  113. b := #{current := _, max := _, last5m := _},
  114. c := #{current := _, max := _, last5m := _}
  115. },
  116. counters := C = #{
  117. a := 1, b := 1, c := 1
  118. }
  119. } when map_size(R) == 3 andalso map_size(C) == 3,
  120. emqx_plugin_libs_metrics:get_metrics(?NAME, <<"testid">>)),
  121. ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"testid">>).
  122. t_inc_matched(_) ->
  123. Metrics = ['rules.matched'],
  124. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule1">>, Metrics),
  125. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule2">>, Metrics),
  126. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
  127. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule2">>, 'rules.matched'),
  128. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule2">>, 'rules.matched'),
  129. ?assertEqual(1, emqx_plugin_libs_metrics:get(?NAME, <<"rule1">>, 'rules.matched')),
  130. ?assertEqual(2, emqx_plugin_libs_metrics:get(?NAME, <<"rule2">>, 'rules.matched')),
  131. ?assertEqual(0, emqx_plugin_libs_metrics:get(?NAME, <<"rule3">>, 'rules.matched')),
  132. ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule1">>),
  133. ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule2">>).
  134. t_rate(_) ->
  135. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule1">>, ['rules.matched']),
  136. ok = emqx_plugin_libs_metrics:create_metrics(?NAME, <<"rule:2">>, ['rules.matched']),
  137. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
  138. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule1">>, 'rules.matched'),
  139. ok = emqx_plugin_libs_metrics:inc(?NAME, <<"rule:2">>, 'rules.matched'),
  140. ?assertEqual(2, emqx_plugin_libs_metrics:get(?NAME, <<"rule1">>, 'rules.matched')),
  141. ct:sleep(1000),
  142. ?LET(#{'rules.matched' := #{max := Max, current := Current}},
  143. emqx_plugin_libs_metrics:get_rate(?NAME, <<"rule1">>),
  144. {?assert(Max =< 2),
  145. ?assert(Current =< 2)}),
  146. ct:sleep(2100),
  147. ?LET(#{'rules.matched' := #{max := Max, current := Current, last5m := Last5Min}}, emqx_plugin_libs_metrics:get_rate(?NAME, <<"rule1">>),
  148. {?assert(Max =< 2),
  149. ?assert(Current == 0),
  150. ?assert(Last5Min =< 0.67)}),
  151. ct:sleep(3000),
  152. ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule1">>),
  153. ok = emqx_plugin_libs_metrics:clear_metrics(?NAME, <<"rule:2">>).