Skip to content

Commit

Permalink
Extract timestamped telemetry events to a helper module
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Dec 11, 2023
1 parent 9b6b214 commit 875a4fb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
10 changes: 4 additions & 6 deletions src/amoc_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,8 @@ handle_add(StartId, EndId, #state{last_user_id = LastId,
scenario = Scenario,
tref = TRef} = State) when StartId =< EndId,
LastId < StartId ->
TimeStamp = erlang:monotonic_time(),
telemetry:execute([amoc, controller, users], #{count => EndId - StartId + 1},
#{monotonic_time => TimeStamp, scenario => Scenario, type => add}),
amoc_telemetry:execute([controller, users], #{count => EndId - StartId + 1},
#{scenario => Scenario, type => add}),
NewUsers = lists:seq(StartId, EndId),
NewScheduledUsers = lists:append(ScheduledUsers, NewUsers),
NewTRef = maybe_start_timer(TRef),
Expand All @@ -255,9 +254,8 @@ handle_add(_StartId, _EndId, #state{status = Status} = State) ->

-spec handle_remove(user_count(), boolean(), state()) -> handle_call_res().
handle_remove(Count, ForceRemove, #state{status = running, scenario = Scenario}) ->
TimeStamp = erlang:monotonic_time(),
telemetry:execute([amoc, controller, users], #{count => Count},
#{monotonic_time => TimeStamp, scenario => Scenario, type => remove}),
amoc_telemetry:execute([controller, users], #{count => Count},
#{scenario => Scenario, type => remove}),
Pids = case ets:match_object(?USERS_TABLE, '$1', Count) of
{Objects, _} -> [Pid || {_Id, Pid} <- Objects];
'$end_of_table' -> []
Expand Down
11 changes: 4 additions & 7 deletions src/amoc_coordinator/amoc_coordinator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ start(Name, CoordinationPlan, Timeout) when ?IS_TIMEOUT(Timeout) ->
Plan = normalize_coordination_plan(CoordinationPlan),
case gen_event:start({local, Name}) of
{ok, _} ->
telemetry:execute([amoc, coordinator, start], #{count => 1},
#{monotonic_time => erlang:monotonic_time(), name => Name}),
amoc_telemetry:execute([coordinator, start], #{count => 1}, #{name => Name}),
%% according to gen_event documentation:
%%
%% When the event is received, the event manager calls
Expand All @@ -100,8 +99,7 @@ start(Name, CoordinationPlan, Timeout) when ?IS_TIMEOUT(Timeout) ->
-spec stop(name()) -> ok.
stop(Name) ->
gen_event:stop(Name),
telemetry:execute([amoc, coordinator, stop], #{count => 1},
#{monotonic_time => erlang:monotonic_time(), name => Name}).
amoc_telemetry:execute([coordinator, stop], #{count => 1}, #{name => Name}).

%% @see add/3
-spec add(name(), any()) -> ok.
Expand Down Expand Up @@ -158,15 +156,14 @@ init(CoordinationItem) ->
-spec handle_event(Event :: term(), state()) -> {ok, state()}.
handle_event(Event, {timeout, Name, Pid}) ->
%% there's only one "timeout" event handler for coordinator,
%% so calling telemetry:execute/3 here to ensure that it's
%% so calling amoc_telemetry:execute/3 here to ensure that it's
%% triggered just once per event.
TelemetryEvent = case Event of
{coordinate, _} -> add;
reset_coordinator -> reset;
coordinator_timeout -> timeout
end,
telemetry:execute([amoc, coordinator, TelemetryEvent], #{count => 1},
#{monotonic_time => erlang:monotonic_time(), name => Name}),
amoc_telemetry:execute([coordinator, TelemetryEvent], #{count => 1}, #{name => Name}),
erlang:send(Pid, Event),
{ok, {timeout, Name, Pid}};
handle_event(Event, {worker, WorkerPid}) ->
Expand Down
9 changes: 9 additions & 0 deletions src/amoc_telemetry.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-module(amoc_telemetry).

-export([execute/3]).

execute(Name, Measurements, Metadata) ->
TimeStamp = erlang:monotonic_time(),
NameWithAmocPrefix = [amoc | Name],
MetadataWithTS = Metadata#{monotonic_time => TimeStamp},
telemetry:execute(NameWithAmocPrefix, Measurements, MetadataWithTS).
8 changes: 2 additions & 6 deletions src/amoc_throttle/amoc_throttle_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,10 @@ maybe_raise_event(Name, Event) ->
end.

raise_event(Name, Event) when Event =:= request; Event =:= execute; Event =:= init ->
telemetry:execute([amoc, throttle, Event],
#{count => 1},
#{monotonic_time => erlang:monotonic_time(), name => Name}).
amoc_telemetry:execute([throttle, Event], #{count => 1}, #{name => Name}).

report_rate(Name, RatePerMinute) ->
telemetry:execute([amoc, throttle, rate],
#{rate => RatePerMinute},
#{monotonic_time => erlang:monotonic_time(), name => Name}).
amoc_telemetry:execute([throttle, rate], #{rate => RatePerMinute}, #{name => Name}).

-spec change_rate_and_stop_plan(name(), state()) -> state().
change_rate_and_stop_plan(Name, State) ->
Expand Down

0 comments on commit 875a4fb

Please sign in to comment.