feat(telemetry): add gateway info to reported data

This commit is contained in:
Thales Macedo Garitezi 2022-04-01 18:13:58 -03:00
parent 7d807ce6bb
commit af4ad5721f
No known key found for this signature in database
GPG Key ID: DD279F8152A9B6DD
2 changed files with 66 additions and 9 deletions

View File

@ -342,7 +342,8 @@ get_telemetry(State0 = #state{uuid = UUID}) ->
{vm_specs, vm_specs()}, {vm_specs, vm_specs()},
{mqtt_runtime_insights, MQTTRTInsights}, {mqtt_runtime_insights, MQTTRTInsights},
{advanced_mqtt_features, advanced_mqtt_features()}, {advanced_mqtt_features, advanced_mqtt_features()},
{authn_authz, get_authn_authz_info()} {authn_authz, get_authn_authz_info()},
{gateway, get_gateway_info()}
]}. ]}.
report_telemetry(State0 = #state{url = URL}) -> report_telemetry(State0 = #state{url = URL}) ->
@ -458,6 +459,15 @@ get_authn_authz_info() ->
authz => AuthzTypes authz => AuthzTypes
}. }.
get_gateway_info() ->
try
emqx_gateway:get_basic_usage_info()
catch
%% if gateway is not available, for instance
_:_ ->
#{}
end.
bin(L) when is_list(L) -> bin(L) when is_list(L) ->
list_to_binary(L); list_to_binary(L);
bin(A) when is_atom(A) -> bin(A) when is_atom(A) ->

View File

@ -81,6 +81,12 @@ init_per_testcase(t_get_telemetry, Config) ->
{ok, Rendered} {ok, Rendered}
end end
), ),
Lwm2mDataDir = emqx_common_test_helpers:deps_path(
emqx_gateway,
"test/emqx_gateway_SUITE_data"
),
ok = emqx_gateway_SUITE:setup_fake_usage_data(Lwm2mDataDir),
{ok, _} = application:ensure_all_started(emqx_gateway),
Config; Config;
init_per_testcase(t_advanced_mqtt_features, Config) -> init_per_testcase(t_advanced_mqtt_features, Config) ->
OldValues = emqx_modules:get_advanced_mqtt_features_in_use(), OldValues = emqx_modules:get_advanced_mqtt_features_in_use(),
@ -99,6 +105,16 @@ init_per_testcase(t_authn_authz_info, Config) ->
create_authn('ws:default', redis), create_authn('ws:default', redis),
create_authz(postgresql), create_authz(postgresql),
Config; Config;
init_per_testcase(t_enable, Config) ->
ok = meck:new(emqx_telemetry, [non_strict, passthrough, no_history, no_link]),
ok = meck:expect(emqx_telemetry, official_version, fun(_) -> true end),
mock_httpc(),
Config;
init_per_testcase(t_send_after_enable, Config) ->
ok = meck:new(emqx_telemetry, [non_strict, passthrough, no_history, no_link]),
ok = meck:expect(emqx_telemetry, official_version, fun(_) -> true end),
mock_httpc(),
Config;
init_per_testcase(_Testcase, Config) -> init_per_testcase(_Testcase, Config) ->
TestPID = self(), TestPID = self(),
ok = meck:new(httpc, [non_strict, passthrough, no_history, no_link]), ok = meck:new(httpc, [non_strict, passthrough, no_history, no_link]),
@ -111,6 +127,7 @@ init_per_testcase(_Testcase, Config) ->
end_per_testcase(t_get_telemetry, _Config) -> end_per_testcase(t_get_telemetry, _Config) ->
meck:unload([httpc, emqx_telemetry]), meck:unload([httpc, emqx_telemetry]),
application:stop(emqx_gateway),
ok; ok;
end_per_testcase(t_advanced_mqtt_features, Config) -> end_per_testcase(t_advanced_mqtt_features, Config) ->
OldValues = ?config(old_values, Config), OldValues = ?config(old_values, Config),
@ -128,6 +145,10 @@ end_per_testcase(t_authn_authz_info, _Config) ->
['mqtt:global', 'tcp:default', 'ws:default'] ['mqtt:global', 'tcp:default', 'ws:default']
), ),
ok; ok;
end_per_testcase(t_enable, _Config) ->
meck:unload([httpc, emqx_telemetry]);
end_per_testcase(t_send_after_enable, _Config) ->
meck:unload([httpc, emqx_telemetry]);
end_per_testcase(_Testcase, _Config) -> end_per_testcase(_Testcase, _Config) ->
meck:unload([httpc]), meck:unload([httpc]),
ok. ok.
@ -190,6 +211,30 @@ t_get_telemetry(_Config) ->
?assert(is_number(maps:get(messages_received_rate, MQTTRTInsights))), ?assert(is_number(maps:get(messages_received_rate, MQTTRTInsights))),
?assert(is_integer(maps:get(num_topics, MQTTRTInsights))), ?assert(is_integer(maps:get(num_topics, MQTTRTInsights))),
?assert(is_map(get_value(authn_authz, TelemetryData))), ?assert(is_map(get_value(authn_authz, TelemetryData))),
GatewayInfo = get_value(gateway, TelemetryData),
?assert(is_map(GatewayInfo)),
lists:foreach(
fun({GatewayType, GatewayData}) ->
?assertMatch(
#{
authn := GwAuthn,
num_clients := NClients,
listeners := Ls
} when
is_binary(GwAuthn) andalso
is_integer(NClients) andalso
is_list(Ls),
GatewayData,
#{gateway_type => GatewayType}
),
ListenersData = maps:get(listeners, GatewayData),
lists:foreach(
fun(L) -> assert_gateway_listener_shape(L, GatewayType) end,
ListenersData
)
end,
maps:to_list(GatewayInfo)
),
ok. ok.
t_advanced_mqtt_features(_) -> t_advanced_mqtt_features(_) ->
@ -238,15 +283,10 @@ t_authn_authz_info(_) ->
). ).
t_enable(_) -> t_enable(_) ->
ok = meck:new(emqx_telemetry, [non_strict, passthrough, no_history, no_link]),
ok = meck:expect(emqx_telemetry, official_version, fun(_) -> true end),
ok = emqx_telemetry:enable(), ok = emqx_telemetry:enable(),
ok = emqx_telemetry:disable(), ok = emqx_telemetry:disable().
meck:unload([emqx_telemetry]).
t_send_after_enable(_) -> t_send_after_enable(_) ->
ok = meck:new(emqx_telemetry, [non_strict, passthrough, no_history, no_link]),
ok = meck:expect(emqx_telemetry, official_version, fun(_) -> true end),
ok = emqx_telemetry:disable(), ok = emqx_telemetry:disable(),
ok = snabbkaffe:start_trace(), ok = snabbkaffe:start_trace(),
try try
@ -286,8 +326,7 @@ t_send_after_enable(_) ->
exit(telemetry_not_reported) exit(telemetry_not_reported)
end end
after after
ok = snabbkaffe:stop(), ok = snabbkaffe:stop()
meck:unload([emqx_telemetry])
end. end.
t_mqtt_runtime_insights(_) -> t_mqtt_runtime_insights(_) ->
@ -380,6 +419,14 @@ create_authz(postgresql) ->
} }
). ).
assert_gateway_listener_shape(ListenerData, GatewayType) ->
?assertMatch(
#{type := LType, authn := LAuthn} when
is_atom(LType) andalso is_binary(LAuthn),
ListenerData,
#{gateway_type => GatewayType}
).
set_special_configs(emqx_authz) -> set_special_configs(emqx_authz) ->
{ok, _} = emqx:update_config([authorization, cache, enable], false), {ok, _} = emqx:update_config([authorization, cache, enable], false),
{ok, _} = emqx:update_config([authorization, no_match], deny), {ok, _} = emqx:update_config([authorization, no_match], deny),