diff --git a/src/amoc_controller.erl b/src/amoc_controller.erl index 67b43ca7..77d4f50d 100644 --- a/src/amoc_controller.erl +++ b/src/amoc_controller.erl @@ -202,6 +202,7 @@ handle_start_scenario(Scenario, Settings, #state{status = idle} = State) -> scenario_start = StartTime, scenario_ref = Ref, status = running}, + %% This simulates a span telemetry:execute([amoc, scenario, run, start], #{monotonic_time => StartTime, system_time => erlang:system_time()}, #{telemetry_span_context => Ref, scenario => Scenario}), @@ -240,9 +241,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), @@ -255,9 +255,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' -> [] @@ -332,6 +331,7 @@ terminate_scenario(#state{scenario = Scenario, scenario_ref = Ref}) -> case amoc_scenario:terminate(Scenario, ScenarioState) of {error, {Class, Reason, Stacktrace}} = Ret -> + %% This simulates a span StopTime = erlang:monotonic_time(), telemetry:execute([amoc, scenario, run, exception], #{duration => StopTime - StartTime, monotonic_time => StopTime}, @@ -339,6 +339,7 @@ terminate_scenario(#state{scenario = Scenario, kind => Class, reason => Reason, stacktrace => Stacktrace}), Ret; Ret -> + %% This simulates a span StopTime = erlang:monotonic_time(), telemetry:execute([amoc, scenario, run, stop], #{duration => StopTime - StartTime, monotonic_time => StopTime}, diff --git a/src/amoc_coordinator/amoc_coordinator.erl b/src/amoc_coordinator/amoc_coordinator.erl index 63658044..17e70a2b 100644 --- a/src/amoc_coordinator/amoc_coordinator.erl +++ b/src/amoc_coordinator/amoc_coordinator.erl @@ -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 @@ -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. @@ -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}) -> diff --git a/src/amoc_telemetry.erl b/src/amoc_telemetry.erl new file mode 100644 index 00000000..06ec1e79 --- /dev/null +++ b/src/amoc_telemetry.erl @@ -0,0 +1,15 @@ +%% @private +%% @copyright 2023 Erlang Solutions Ltd. +-module(amoc_telemetry). + +-export([execute/3]). + +-spec execute(EventName, Measurements, Metadata) -> ok when + EventName :: telemetry:event_name(), + Measurements :: telemetry:event_measurements(), + Metadata :: telemetry:event_metadata(). +execute(Name, Measurements, Metadata) -> + TimeStamp = erlang:monotonic_time(), + NameWithAmocPrefix = [amoc | Name], + MetadataWithTS = Metadata#{monotonic_time => TimeStamp}, + telemetry:execute(NameWithAmocPrefix, Measurements, MetadataWithTS). diff --git a/src/amoc_throttle/amoc_throttle_controller.erl b/src/amoc_throttle/amoc_throttle_controller.erl index a61c5246..3e62ad49 100644 --- a/src/amoc_throttle/amoc_throttle_controller.erl +++ b/src/amoc_throttle/amoc_throttle_controller.erl @@ -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) ->