Просмотр исходного кода

Merge pull request #7406 from zhongwencool/monitor-api-crash

fix: dashboard_monitor crash
zhongwencool 3 лет назад
Родитель
Сommit
aa831d6c3f

+ 1 - 1
apps/emqx/rebar.config

@@ -30,7 +30,7 @@
     {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.9.1"}}},
     {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.9.1"}}},
     {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.12.3"}}},
     {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.12.3"}}},
     {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.8.1"}}},
     {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.8.1"}}},
-    {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.26.3"}}},
+    {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.26.4"}}},
     {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}},
     {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}},
     {recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}},
     {recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}},
     {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "0.18.0"}}}
     {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "0.18.0"}}}

+ 2 - 2
apps/emqx/src/emqx_metrics.erl

@@ -328,14 +328,14 @@ all() ->
     ].
     ].
 
 
 %% @doc Get metric value
 %% @doc Get metric value
--spec val(metric_name()) -> maybe(non_neg_integer()).
+-spec val(metric_name()) -> non_neg_integer().
 val(Name) ->
 val(Name) ->
     case ets:lookup(?TAB, Name) of
     case ets:lookup(?TAB, Name) of
         [#metric{idx = Idx}] ->
         [#metric{idx = Idx}] ->
             CRef = persistent_term:get(?MODULE),
             CRef = persistent_term:get(?MODULE),
             counters:get(CRef, Idx);
             counters:get(CRef, Idx);
         [] ->
         [] ->
-            undefined
+            0
     end.
     end.
 
 
 %% @doc Increase counter
 %% @doc Increase counter

+ 2 - 2
apps/emqx/src/emqx_stats.erl

@@ -164,11 +164,11 @@ getstats() ->
     end.
     end.
 
 
 %% @doc Get stats by name.
 %% @doc Get stats by name.
--spec getstat(atom()) -> maybe(non_neg_integer()).
+-spec getstat(atom()) -> non_neg_integer().
 getstat(Name) ->
 getstat(Name) ->
     case ets:lookup(?TAB, Name) of
     case ets:lookup(?TAB, Name) of
         [{Name, Val}] -> Val;
         [{Name, Val}] -> Val;
-        [] -> undefined
+        [] -> 0
     end.
     end.
 
 
 %% @doc Set stats
 %% @doc Set stats

+ 1 - 1
apps/emqx/test/emqx_stats_SUITE.erl

@@ -36,7 +36,7 @@ t_get_error_state(_) ->
 
 
 t_get_state(_) ->
 t_get_state(_) ->
     with_proc(fun() ->
     with_proc(fun() ->
-        ?assertEqual(undefined, emqx_stats:getstat('notExist')),
+        ?assertEqual(0, emqx_stats:getstat('notExist')),
         SetConnsCount = emqx_stats:statsfun('connections.count'),
         SetConnsCount = emqx_stats:statsfun('connections.count'),
         SetConnsCount(1),
         SetConnsCount(1),
         ?assertEqual(1, emqx_stats:getstat('connections.count')),
         ?assertEqual(1, emqx_stats:getstat('connections.count')),

+ 3 - 0
apps/emqx_dashboard/src/emqx_dashboard_monitor.erl

@@ -242,6 +242,9 @@ format(Data) ->
 format(TimeStamp, Data, All) ->
 format(TimeStamp, Data, All) ->
     [Data#{time_stamp => TimeStamp} | All].
     [Data#{time_stamp => TimeStamp} | All].
 
 
+cal_rate(_Now, undefined) ->
+    AllSamples = ?GAUGE_SAMPLER_LIST ++ maps:values(?DELTA_SAMPLER_RATE_MAP),
+    lists:foldl(fun(Key, Acc) -> Acc#{Key => 0} end, #{}, AllSamples);
 cal_rate( #emqx_monit{data = NowData, time = NowTime}
 cal_rate( #emqx_monit{data = NowData, time = NowTime}
         , #emqx_monit{data = LastData, time = LastTime}) ->
         , #emqx_monit{data = LastData, time = LastTime}) ->
     TimeDelta = NowTime - LastTime,
     TimeDelta = NowTime - LastTime,

+ 22 - 0
apps/emqx_dashboard/test/emqx_dashboard_monitor_SUITE.erl

@@ -102,6 +102,15 @@ t_monitor_current_api(_) ->
         || Key <- maps:values(?DELTA_SAMPLER_RATE_MAP) ++ ?GAUGE_SAMPLER_LIST],
         || Key <- maps:values(?DELTA_SAMPLER_RATE_MAP) ++ ?GAUGE_SAMPLER_LIST],
     ok.
     ok.
 
 
+t_monitor_reset(_) ->
+    restart_monitor(),
+    {ok, Rate} = request(["monitor_current"]),
+    [?assert(maps:is_key(atom_to_binary(Key, utf8), Rate))
+        || Key <- maps:values(?DELTA_SAMPLER_RATE_MAP) ++ ?GAUGE_SAMPLER_LIST],
+    {ok, Samplers} = request(["monitor"], "latest=1"),
+    ?assertEqual(1, erlang:length(Samplers)),
+    ok.
+
 t_monitor_api_error(_) ->
 t_monitor_api_error(_) ->
     {error, {400, #{<<"code">> := <<"BAD_RPC">>}}} =
     {error, {400, #{<<"code">> := <<"BAD_RPC">>}}} =
         request(["monitor", "nodes", 'emqx@127.0.0.2']),
         request(["monitor", "nodes", 'emqx@127.0.0.2']),
@@ -147,3 +156,16 @@ do_request_api(Method, Request)->
 auth_header_() ->
 auth_header_() ->
     Basic = binary_to_list(base64:encode(<<"admin:public">>)),
     Basic = binary_to_list(base64:encode(<<"admin:public">>)),
     {"Authorization", "Basic " ++ Basic}.
     {"Authorization", "Basic " ++ Basic}.
+
+restart_monitor() ->
+    erlang:exit(erlang:whereis(emqx_dashboard_monitor), killed),
+    ?assertEqual(ok, wait_new_monitor(10)).
+
+wait_new_monitor(Count) when Count =< 0 -> timeout;
+wait_new_monitor(Count) ->
+    case is_pid(erlang:whereis(emqx_dashboard_monitor)) of
+        true -> ok;
+        false ->
+            timer:sleep(100),
+            wait_new_monitor(Count - 1)
+    end.

+ 1 - 1
apps/emqx_prometheus/rebar.config

@@ -4,7 +4,7 @@
  [ {emqx, {path, "../emqx"}},
  [ {emqx, {path, "../emqx"}},
    %% FIXME: tag this as v3.1.3
    %% FIXME: tag this as v3.1.3
    {prometheus, {git, "https://github.com/deadtrickster/prometheus.erl", {tag, "v4.8.1"}}},
    {prometheus, {git, "https://github.com/deadtrickster/prometheus.erl", {tag, "v4.8.1"}}},
-   {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.26.3"}}}
+   {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.26.4"}}}
  ]}.
  ]}.
 
 
 {edoc_opts, [{preprocess, true}]}.
 {edoc_opts, [{preprocess, true}]}.

+ 1 - 1
mix.exs

@@ -68,7 +68,7 @@ defmodule EMQXUmbrella.MixProject do
       # in conflict by emqtt and hocon
       # in conflict by emqtt and hocon
       {:getopt, "1.0.2", override: true},
       {:getopt, "1.0.2", override: true},
       {:snabbkaffe, github: "kafka4beam/snabbkaffe", tag: "0.18.0", override: true},
       {:snabbkaffe, github: "kafka4beam/snabbkaffe", tag: "0.18.0", override: true},
-      {:hocon, github: "emqx/hocon", tag: "0.26.3", override: true},
+      {:hocon, github: "emqx/hocon", tag: "0.26.4", override: true},
       {:emqx_http_lib, github: "emqx/emqx_http_lib", tag: "0.4.1", override: true},
       {:emqx_http_lib, github: "emqx/emqx_http_lib", tag: "0.4.1", override: true},
       {:esasl, github: "emqx/esasl", tag: "0.2.0"},
       {:esasl, github: "emqx/esasl", tag: "0.2.0"},
       {:jose, github: "potatosalad/erlang-jose", tag: "1.11.2"},
       {:jose, github: "potatosalad/erlang-jose", tag: "1.11.2"},

+ 1 - 1
rebar.config

@@ -66,7 +66,7 @@
     , {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.2"}}}
     , {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.2"}}}
     , {getopt, "1.0.2"}
     , {getopt, "1.0.2"}
     , {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "0.18.0"}}}
     , {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "0.18.0"}}}
-    , {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.26.3"}}}
+    , {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.26.4"}}}
     , {emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.4.1"}}}
     , {emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.4.1"}}}
     , {esasl, {git, "https://github.com/emqx/esasl", {tag, "0.2.0"}}}
     , {esasl, {git, "https://github.com/emqx/esasl", {tag, "0.2.0"}}}
     , {jose, {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.2"}}}
     , {jose, {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.2"}}}