Skip to content

Commit

Permalink
Add timestamps to telemetry coordinator events
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Dec 5, 2023
1 parent a26fd9c commit b67f0e5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion guides/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ Indicates when a coordinating event was raised, like a process being added for c
```erlang
event_name: [amoc, coordinator, start | stop | add | reset | timeout]
measurements: #{count => 1}
metadata: #{name => atom()}
metadata: #{monotonic_time => integer(), name => atom()}
```
10 changes: 6 additions & 4 deletions src/amoc_coordinator/amoc_coordinator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ 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}, #{name => Name}),
telemetry:execute([amoc, coordinator, start], #{count => 1},
#{monotonic_time => erlang:monotonic_time(), name => Name}),
%% according to gen_event documentation:
%%
%% When the event is received, the event manager calls
Expand All @@ -99,7 +100,8 @@ 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}, #{name => Name}).
telemetry:execute([amoc, coordinator, stop], #{count => 1},
#{monotonic_time => erlang:monotonic_time(), name => Name}).

%% @see add/3
-spec add(name(), any()) -> ok.
Expand Down Expand Up @@ -163,8 +165,8 @@ handle_event(Event, {timeout, Name, Pid}) ->
reset_coordinator -> reset;
coordinator_timeout -> timeout
end,
telemetry:execute([amoc, coordinator, TelemetryEvent],
#{count => 1}, #{name => Name}),
telemetry:execute([amoc, coordinator, TelemetryEvent], #{count => 1},
#{monotonic_time => erlang:monotonic_time(), name => Name}),
erlang:send(Pid, Event),
{ok, {timeout, Name, Pid}};
handle_event(Event, {worker, WorkerPid}) ->
Expand Down
9 changes: 4 additions & 5 deletions test/amoc_coordinator_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,8 @@ assert_telemetry_events(Name, [{_Pid, Call, _Ret} | History], [Event | EventList
assert_telemetry_handler_call(Name, Call, Event) ->
EventName = [amoc, coordinator, Event],
Measurements = #{count => 1},
EventMetadata = #{name => Name},
HandlerConfig = ?TELEMETRY_HANDLER_CONFIG,
ExpectedHandlerCall = {?TELEMETRY_HANDLER, handler,
[EventName, Measurements,
EventMetadata, HandlerConfig]},
?assertEqual(ExpectedHandlerCall, Call).
?assertMatch(
{?TELEMETRY_HANDLER, handler,
[EventName, Measurements,
#{name := Name, monotonic_time := _}, HandlerConfig]}, Call).

0 comments on commit b67f0e5

Please sign in to comment.