From ada3b03f6ea2d75ea3b1d0d0781f51cb855473a0 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Mon, 25 Apr 2022 11:15:59 -0300 Subject: [PATCH] fix(telemetry): do not crash if bridge app is not ready yet --- apps/emqx_bridge/src/emqx_bridge.erl | 41 ++++++++++++++++------------ 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/apps/emqx_bridge/src/emqx_bridge.erl b/apps/emqx_bridge/src/emqx_bridge.erl index 4727a1dc8..b2939f9a9 100644 --- a/apps/emqx_bridge/src/emqx_bridge.erl +++ b/apps/emqx_bridge/src/emqx_bridge.erl @@ -431,23 +431,30 @@ if_only_to_toggle_enable(OldConf, Conf) -> } } when BridgeType :: atom(). get_basic_usage_info() -> - lists:foldl( - fun(#{resource_data := #{config := #{enable := false}}}, Acc) -> - Acc; - (#{type := BridgeType}, Acc) -> - NumBridges = maps:get(num_bridges, Acc), - CountByType0 = maps:get(count_by_type, Acc), - CountByType = maps:update_with( - binary_to_atom(BridgeType, utf8), - fun(X) -> X + 1 end, - 1, - CountByType0), - Acc#{ num_bridges => NumBridges + 1 - , count_by_type => CountByType - } - end, - #{num_bridges => 0, count_by_type => #{}}, - list()). + InitialAcc = #{num_bridges => 0, count_by_type => #{}}, + try + lists:foldl( + fun(#{resource_data := #{config := #{enable := false}}}, Acc) -> + Acc; + (#{type := BridgeType}, Acc) -> + NumBridges = maps:get(num_bridges, Acc), + CountByType0 = maps:get(count_by_type, Acc), + CountByType = maps:update_with( + binary_to_atom(BridgeType, utf8), + fun(X) -> X + 1 end, + 1, + CountByType0), + Acc#{ num_bridges => NumBridges + 1 + , count_by_type => CountByType + } + end, + InitialAcc, + list()) + catch + %% for instance, when the bridge app is not ready yet. + _:_ -> + InitialAcc + end. bin(Bin) when is_binary(Bin) -> Bin; bin(Str) when is_list(Str) -> list_to_binary(Str);