Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/telemetry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
%%
%% See {@link execute/3} to learn how the handlers are invoked.
%%
%% <b>Note:</b> due to how anonymous functions are implemented in the Erlang VM, it is best to use
%% function captures (i.e. `fun mod:fun/4' in Erlang or `&Mod.fun/4' in Elixir) as event handlers
%% to achieve maximum performance. In other words, avoid using literal anonymous functions
%% (`fun(...) -> ... end' or `fn ... -> ... end') or local function captures (`fun handle_event/4'
%% or `&handle_event/4' ) as event handlers.
%% `function` must be a 4 arity function. Due to how anonymous functions are implemented
%% in the Erlang VM, it is best to use function captures (i.e. `fun mod:fun/4' in Erlang or
%% `&Mod.fun/4' in Elixir) as event handlers to achieve maximum performance. In other words,
%% avoid using literal anonymous functions (`fun(...) -> ... end' or `fn ... -> ... end') or
%% local function captures (`fun handle_event/4' or `&handle_event/4' ) as event handlers.
%%
%% All the handlers are executed by the process dispatching event. If the function fails (raises,
%% exits or throws) then the handler is removed and a failure event is emitted.
Expand Down Expand Up @@ -103,8 +103,9 @@ attach(HandlerId, EventName, Function, Config) ->
EventName :: event_name(),
Function :: handler_function(),
Config :: handler_config().
attach_many(HandlerId, EventNames, Function, Config) when is_function(Function, 4) ->
attach_many(HandlerId, EventNames, Function, Config) ->
assert_event_names(EventNames),
assert_function(Function),
case erlang:fun_info(Function, type) of
{type, external} ->
ok;
Expand Down Expand Up @@ -369,6 +370,12 @@ assert_event_names(List) when is_list(List) ->
assert_event_names(Term) ->
erlang:error(badarg, Term).

-spec assert_function(term()) -> ok.
assert_function(Function) when is_function(Function, 4) ->
ok;
assert_function(Function) ->
erlang:error(badarg, Function).

-spec assert_event_prefix(term()) -> ok.
assert_event_prefix(List) when is_list(List) ->
case lists:all(fun erlang:is_atom/1, List) of
Expand Down
9 changes: 8 additions & 1 deletion test/telemetry_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ all() ->
handler_on_multiple_events, remove_all_handler_on_failure,
list_handler_on_many, detach_from_all, old_execute, default_metadata,
off_execute, invoke_successful_span_handlers, invoke_exception_span_handlers,
spans_generate_unique_default_contexts, logs_on_local_function].
spans_generate_unique_default_contexts, logs_on_local_function, badarg_on_wrong_arity_function].

init_per_suite(Config) ->
application:ensure_all_started(telemetry),
Expand Down Expand Up @@ -426,6 +426,13 @@ spans_generate_unique_default_contexts(Config) ->
1000 -> ct:fail(timeout_receive_echo)
end.

badarg_on_wrong_arity_function(Config) ->
HandlerId = ?config(id, Config),
Event = [a, first, event],
HandlerFun = fun ?MODULE:wrong_arity_function/3,

?assertError(badarg, telemetry:attach(HandlerId, Event, HandlerFun, nil)).

logs_on_local_function(Config) ->
HandlerId = ?config(id, Config),
Event = [some, action],
Expand Down