chore: add tag to resouce log

This commit is contained in:
zhongwencool 2024-07-17 09:14:58 +08:00
parent e148d903e8
commit f29988ed8e
2 changed files with 64 additions and 45 deletions

View File

@ -159,6 +159,11 @@
%% See `hocon_tconf` %% See `hocon_tconf`
-define(TEST_ID_PREFIX, "t_probe_"). -define(TEST_ID_PREFIX, "t_probe_").
-define(RES_METRICS, resource_metrics). -define(RES_METRICS, resource_metrics).
-define(LOG_LEVEL(_L_), case _L_ of true -> info; false -> warning end). -define(LOG_LEVEL(_L_),
case _L_ of
true -> info;
false -> warning
end
).
-define(RESOURCE_ALLOCATION_TAB, emqx_resource_allocations). -define(RESOURCE_ALLOCATION_TAB, emqx_resource_allocations).

View File

@ -689,13 +689,12 @@ handle_event(EventType, EventData, State, Data) ->
#{ #{
msg => "ignore_all_other_events", msg => "ignore_all_other_events",
resource_id => Data#data.id, resource_id => Data#data.id,
group => Data#data.group,
type => Data#data.type,
event_type => EventType, event_type => EventType,
event_data => EventData, event_data => EventData,
state => State, state => State,
data => emqx_utils:redact(Data) data => emqx_utils:redact(Data)
} },
#{tag => tag(Data#data.group, Data#data.type)}
), ),
keep_state_and_data. keep_state_and_data.
@ -770,13 +769,15 @@ start_resource(Data, From) ->
{next_state, ?state_connecting, update_state(UpdatedData2, Data), Actions}; {next_state, ?state_connecting, update_state(UpdatedData2, Data), Actions};
{error, Reason} = Err -> {error, Reason} = Err ->
IsDryRun = emqx_resource:is_dry_run(ResId), IsDryRun = emqx_resource:is_dry_run(ResId),
?SLOG(log_level(IsDryRun), #{ ?SLOG(
msg => "start_resource_failed", log_level(IsDryRun),
resource_id => ResId, #{
group => Group, msg => "start_resource_failed",
type => Type, resource_id => ResId,
reason => Reason reason => Reason
}), },
#{tag => tag(Group, Type)}
),
_ = maybe_alarm(?status_disconnected, IsDryRun, ResId, Err, Data#data.error), _ = maybe_alarm(?status_disconnected, IsDryRun, ResId, Err, Data#data.error),
%% Add channels and raise alarms %% Add channels and raise alarms
NewData1 = channels_health_check(?status_disconnected, add_channels(Data)), NewData1 = channels_health_check(?status_disconnected, add_channels(Data)),
@ -836,14 +837,16 @@ add_channels_in_list([{ChannelID, ChannelConfig} | Rest], Data) ->
add_channels_in_list(Rest, NewData); add_channels_in_list(Rest, NewData);
{error, Reason} = Error -> {error, Reason} = Error ->
IsDryRun = emqx_resource:is_dry_run(ResId), IsDryRun = emqx_resource:is_dry_run(ResId),
?SLOG(log_level(IsDryRun), #{ ?SLOG(
msg => "add_channel_failed", log_level(IsDryRun),
resource_id => ResId, #{
type => Type, msg => "add_channel_failed",
group => Group, resource_id => ResId,
channel_id => ChannelID, channel_id => ChannelID,
reason => Reason reason => Reason
}), },
#{tag => tag(Group, Type)}
),
AddedChannelsMap = Data#data.added_channels, AddedChannelsMap = Data#data.added_channels,
NewAddedChannelsMap = maps:put( NewAddedChannelsMap = maps:put(
ChannelID, ChannelID,
@ -915,14 +918,18 @@ remove_channels_in_list([ChannelID | Rest], Data, KeepInChannelMap) ->
}, },
remove_channels_in_list(Rest, NewData, KeepInChannelMap); remove_channels_in_list(Rest, NewData, KeepInChannelMap);
{error, Reason} -> {error, Reason} ->
?SLOG(log_level(IsDryRun), #{ ?SLOG(
msg => "remove_channel_failed", log_level(IsDryRun),
resource_id => ResId, #{
group => Group, msg => "remove_channel_failed",
type => Type, resource_id => ResId,
channel_id => ChannelID, group => Group,
reason => Reason type => Type,
}), channel_id => ChannelID,
reason => Reason
},
#{tag => tag(Group, Type)}
),
NewData = Data#data{ NewData = Data#data{
added_channels = NewAddedChannelsMap added_channels = NewAddedChannelsMap
}, },
@ -1043,14 +1050,16 @@ handle_remove_channel_exists(From, ChannelId, Data) ->
{keep_state, update_state(UpdatedData, Data), [{reply, From, ok}]}; {keep_state, update_state(UpdatedData, Data), [{reply, From, ok}]};
{error, Reason} = Error -> {error, Reason} = Error ->
IsDryRun = emqx_resource:is_dry_run(Id), IsDryRun = emqx_resource:is_dry_run(Id),
?SLOG(log_level(IsDryRun), #{ ?SLOG(
msg => "remove_channel_failed", log_level(IsDryRun),
resource_id => Id, #{
group => Group, msg => "remove_channel_failed",
type => Type, resource_id => Id,
channel_id => ChannelId, channel_id => ChannelId,
reason => Reason reason => Reason
}), },
#{tag => tag(Group, Type)}
),
{keep_state_and_data, [{reply, From, Error}]} {keep_state_and_data, [{reply, From, Error}]}
end. end.
@ -1158,13 +1167,15 @@ continue_resource_health_check_connected(NewStatus, Data0) ->
_ -> _ ->
#data{id = ResId, group = Group, type = Type} = Data0, #data{id = ResId, group = Group, type = Type} = Data0,
IsDryRun = emqx_resource:is_dry_run(ResId), IsDryRun = emqx_resource:is_dry_run(ResId),
?SLOG(log_level(IsDryRun), #{ ?SLOG(
msg => "health_check_failed", log_level(IsDryRun),
resource_id => ResId, #{
group => Group, msg => "health_check_failed",
type => Type, resource_id => ResId,
status => NewStatus status => NewStatus
}), },
#{tag => tag(Group, Type)}
),
%% Note: works because, coincidentally, channel/resource status is a %% Note: works because, coincidentally, channel/resource status is a
%% subset of resource manager state... But there should be a conversion %% subset of resource manager state... But there should be a conversion
%% between the two here, as resource manager also has `stopped', which is %% between the two here, as resource manager also has `stopped', which is
@ -1669,10 +1680,9 @@ parse_health_check_result({error, Error}, Data) ->
#{ #{
msg => "health_check_exception", msg => "health_check_exception",
resource_id => Data#data.id, resource_id => Data#data.id,
type => Data#data.type,
group => Data#data.group,
reason => Error reason => Error
} },
#{tag => tag(Data#data.group, Data#data.type)}
), ),
{?status_disconnected, Data#data.state, {error, Error}}. {?status_disconnected, Data#data.state, {error, Error}}.
@ -1835,3 +1845,7 @@ state_to_status(?state_disconnected) -> ?status_disconnected.
log_level(true) -> info; log_level(true) -> info;
log_level(false) -> warning. log_level(false) -> warning.
tag(Group, Type) ->
Str = emqx_utils_conv:str(Group) ++ "/" ++ emqx_utils_conv:str(Type),
string:uppercase(Str).