refactor(emqx_plugin_libs_pool): structured logging

This commit is contained in:
Zaiming (Stone) Shi 2022-01-04 00:24:11 +01:00
parent 719800914a
commit 6c99b64e4c
2 changed files with 23 additions and 10 deletions

View File

@ -34,6 +34,8 @@
, on_jsonify/1 , on_jsonify/1
]). ]).
%% ecpool callback
-export([connect/1]). -export([connect/1]).
-export([roots/0, fields/1]). -export([roots/0, fields/1]).
@ -125,7 +127,7 @@ on_start(InstId, Config = #{mongo_type := Type,
{options, init_topology_options(maps:to_list(Topology), [])}, {options, init_topology_options(maps:to_list(Topology), [])},
{worker_options, init_worker_options(maps:to_list(NConfig), SslOpts)}], {worker_options, init_worker_options(maps:to_list(NConfig), SslOpts)}],
PoolName = emqx_plugin_libs_pool:pool_name(InstId), PoolName = emqx_plugin_libs_pool:pool_name(InstId),
_ = emqx_plugin_libs_pool:start_pool(PoolName, ?MODULE, Opts), ok = emqx_plugin_libs_pool:start_pool(PoolName, ?MODULE, Opts),
{ok, #{poolname => PoolName, type => Type}}. {ok, #{poolname => PoolName, type => Type}}.
on_stop(InstId, #{poolname := PoolName}) -> on_stop(InstId, #{poolname := PoolName}) ->
@ -177,18 +179,22 @@ health_check(PoolName) ->
%% =================================================================== %% ===================================================================
%% TODO: log reasons
check_worker_health(Worker) -> check_worker_health(Worker) ->
case ecpool_worker:client(Worker) of case ecpool_worker:client(Worker) of
{ok, Conn} -> {ok, Conn} ->
%% we don't care if this returns something or not, we just to test the connection %% we don't care if this returns something or not, we just to test the connection
try mongo_api:find_one(Conn, <<"foo">>, #{}, #{}) of try mongo_api:find_one(Conn, <<"foo">>, #{}, #{}) of
{error, _} -> false; {error, _Reason} ->
false;
_ -> _ ->
true true
catch catch
_Class:_Error -> false _ : _ ->
false
end; end;
_ -> false _ ->
false
end. end.
connect(Opts) -> connect(Opts) ->

View File

@ -22,26 +22,33 @@
, health_check/3 , health_check/3
]). ]).
-include_lib("emqx/include/logger.hrl").
pool_name(ID) when is_binary(ID) -> pool_name(ID) when is_binary(ID) ->
list_to_atom(binary_to_list(ID)). list_to_atom(binary_to_list(ID)).
start_pool(Name, Mod, Options) -> start_pool(Name, Mod, Options) ->
case ecpool:start_sup_pool(Name, Mod, Options) of case ecpool:start_sup_pool(Name, Mod, Options) of
{ok, _} -> logger:log(info, "Initiated ~0p Successfully", [Name]); {ok, _} ->
?SLOG(info, #{msg => "start_ecpool_ok", pool_name => Name});
{error, {already_started, _Pid}} -> {error, {already_started, _Pid}} ->
stop_pool(Name), stop_pool(Name),
start_pool(Name, Mod, Options); start_pool(Name, Mod, Options);
{error, Reason} -> {error, Reason} ->
logger:log(error, "Initiate ~0p failed ~0p", [Name, Reason]), ?SLOG(error, #{msg => "start_ecpool_error", pool_name => Name,
reason => Reason}),
error({start_pool_failed, Name}) error({start_pool_failed, Name})
end. end.
stop_pool(Name) -> stop_pool(Name) ->
case ecpool:stop_sup_pool(Name) of case ecpool:stop_sup_pool(Name) of
ok -> logger:log(info, "Destroyed ~0p Successfully", [Name]); ok ->
{error, not_found} -> ok; ?SLOG(info, #{msg => "stop_ecpool_ok", pool_name => Name});
{error, not_found} ->
ok;
{error, Reason} -> {error, Reason} ->
logger:log(error, "Destroy ~0p failed, ~0p", [Name, Reason]), ?SLOG(error, #{msg => "stop_ecpool_failed", pool_name => Name,
reason => Reason}),
error({stop_pool_failed, Name}) error({stop_pool_failed, Name})
end. end.