Skip to content

Commit

Permalink
feat: allows custom metadata at the endpoint level
Browse files Browse the repository at this point in the history
Also fixed an issue where nothing would be logged when the method was not allowed
  • Loading branch information
lafirest committed Oct 9, 2024
1 parent e3ff9aa commit 7f9d2b5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/minirest.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
function :: atom(),
filter :: fun(),
authorization :: {Module :: atom(), Function :: atom()} | undefined,
log :: fun() | undefined,
log_meta :: map(),
error_codes :: list(error_code())
}).

Expand Down
15 changes: 9 additions & 6 deletions src/minirest_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ init(Request0, State)->
{Code, Request1} = handle(Request0, State),
ReqEnd = erlang:monotonic_time(),
Meta = get_log_meta(),
run_log_hook(Meta, ReqStart, ReqEnd, Code, Request1),
run_log_hook(State, Meta, ReqStart, ReqEnd, Code, Request1),
{ok, Request1, State}.

%% In previous versions, the metadata was statically derived from the `authorize` method,
Expand All @@ -56,8 +56,8 @@ handle(Request, State) ->
StatusCode,
cowboy_req:reply(StatusCode, Headers, <<"">>, Request)
};
{ok, Handler = #handler{path = Path, log = Log, method = MethodAtom}} ->
init_log_meta(#{operation_id => list_to_binary(Path), log => Log, method => MethodAtom}),
{ok, Handler = #handler{path = Path, log_meta = LogMeta, method = MethodAtom}} ->
init_log_meta(LogMeta#{operation_id => list_to_binary(Path), method => MethodAtom}),
case do_authorize(Request, Handler) of
{ok, AuthMeta} ->
update_log_meta(AuthMeta),
Expand Down Expand Up @@ -255,11 +255,14 @@ maybe_ignore_code_check(400, 'BAD_REQUEST') -> true;
maybe_ignore_code_check(500, 'INTERNAL_ERROR') -> true;
maybe_ignore_code_check(_, _) -> false.

run_log_hook(#{log := Log} = Meta0, ReqStart, ReqEnd, Code, Req) when is_function(Log) ->
Meta = maps:without([log], Meta0),
run_log_hook(#{log := {M, F, InitMeta}}, Meta0, ReqStart, ReqEnd, Code, Req) ->
Meta = maps:merge(InitMeta, Meta0),
_ = M:F(Meta#{req_start => ReqStart, req_end => ReqEnd, code => Code}, Req),
ok;
run_log_hook(#{log := Log}, Meta, ReqStart, ReqEnd, Code, Req) when is_function(Log) ->
_ = Log(Meta#{req_start => ReqStart, req_end => ReqEnd, code => Code}, Req),
ok;
run_log_hook(_log_meta, _ReqStart, _ReqEnd, _Code, _Req) ->
run_log_hook(_State, _Meta, _ReqStart, _ReqEnd, _Code, _Req) ->
ok.

init_log_meta(Init) ->
Expand Down
4 changes: 2 additions & 2 deletions src/minirest_trails.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ trails_schemas(BasePath, Authorization, Log, Module, {Path, Metadata, Function,
function = Function,
authorization = maps:get(security, MethodDef, []) =/= [] andalso Authorization,
filter = maps:get(filter, Options, undefined),
log = Log,
log_meta = maps:get(log_meta, MethodDef, #{}),
error_codes = ErrorCodes
},
minirest_info_api:add_codes(ErrorCodes),
maps:put(binary_method(Method), HandlerState, HandlerStates)
end,
HandlerStates = maps:fold(Fun, #{}, Metadata),
HandlerStates = maps:fold(Fun, #{log => Log}, Metadata),
CompletePath = append_base_path(BasePath, Path),
trails:trail(CompletePath, ?HANDLER, HandlerStates, Metadata).

Expand Down

0 comments on commit 7f9d2b5

Please sign in to comment.