Skip to content

Commit

Permalink
Extract more telemetry helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Dec 11, 2023
1 parent 04ef91a commit 7933fc0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 39 deletions.
4 changes: 2 additions & 2 deletions guides/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ There are related to bad configuration events, they might deserve logging
```erlang
event_name: [amoc, config, get | verify | env]
measurements: #{}
metadata: #{log_class => syslog_level(), _ => _}
metadata: #{log_level => syslog_level(), _ => _}
```

## Cluster
Expand All @@ -105,5 +105,5 @@ There are related to clustering events
```erlang
event_name: [amoc, cluster, connect_nodes | nodedown | master_node_down]
measurements: #{count => non_neg_integer()},
metadata: #{node => node(), nodes => nodes(), state => map()}
metadata: #{nodes => nodes(), state => map()}
```
11 changes: 5 additions & 6 deletions src/amoc_config/amoc_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ get(Name) ->
get(Name, Default) when is_atom(Name) ->
case ets:lookup(amoc_config, Name) of
[] ->
telemetry:execute([amoc, config, get], #{},
#{log_class => error, msg => <<"no scenario setting">>,
scenario => Name}),
amoc_telemetry:execute_error(
[config, get], #{scenario => Name}, <<"no scenario setting">>),
throw({invalid_setting, Name});
[#module_parameter{name = Name, value = undefined}] ->
Default;
[#module_parameter{name = Name, value = Value}] ->
Value;
InvalidLookupRet ->
telemetry:execute([amoc, config, get], #{},
#{log_class => error, msg => <<"invalid lookup return value">>,
scenario => Name, return => InvalidLookupRet}),
amoc_telemetry:execute_error(
[config, get], #{scenario => Name, return => InvalidLookupRet},
<<"invalid lookup return value">>),
throw({invalid_lookup_ret_value, InvalidLookupRet})
end.
10 changes: 5 additions & 5 deletions src/amoc_config/amoc_config_env.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ get_os_env(Name, Default) ->
case parse_value(Value, Default) of
{ok, Term} -> Term;
{error, Error} ->
telemetry:execute(
[amoc, config, env], #{error => 1},
#{log_class => error, error => Error, variable_name => EnvName,
variable_value => Value, default_value => Default,
msg => <<"cannot parse environment variable, using default value">>}),
amoc_telemetry:execute_error(
[config, env],
#{error => Error, variable_name => EnvName,
variable_value => Value, default_value => Default},
<<"cannot parse environment variable, using default value">>),
Default
end.

Expand Down
18 changes: 9 additions & 9 deletions src/amoc_config/amoc_config_verification.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ verify(Fun, Value) ->
{true, NewValue} -> {true, NewValue};
{false, Reason} -> {false, {verification_failed, Reason}};
Ret ->
telemetry:execute([amoc, config, verify], #{error => 1},
#{log_class => error, verification_method => Fun,
verification_arg => Value, verification_return => Ret,
msg => <<"invalid verification method">>}),
amoc_telemetry:execute_error(
[config, verify],
#{verification_method => Fun, verification_arg => Value, verification_return => Ret},
<<"invalid verification method">>),
{false, {invalid_verification_return_value, Ret}}
catch
C:E:S ->
telemetry:execute([amoc, config, verify], #{error => 1},
#{log_class => error, verification_method => Fun,
verification_arg => Value,
kind => C, reason => E, stacktrace => S,
msg => <<"invalid verification method">>}),
amoc_telemetry:execute_error(
[config, verify],
#{verification_method => Fun, verification_arg => Value,
kind => C, reason => E, stacktrace => S},
<<"invalid verification method">>),
{false, {exception_during_verification, {C, E, S}}}
end.
10 changes: 3 additions & 7 deletions src/amoc_distribution/amoc_cluster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ handle_call(_Request, _From, State) ->

-spec handle_cast(any(), state()) -> {noreply, state()}.
handle_cast({connect_nodes, Nodes}, State) ->
telemetry:execute([amoc, cluster, connect_nodes], #{count => length(Nodes)},
#{nodes => Nodes, state => state_to_map(State)}),
amoc_telemetry:execute_nodes([cluster, connect_nodes], Nodes, state_to_map(State)),
NewState = handle_connect_nodes(Nodes, State),
schedule_timer(NewState),
{noreply, NewState};
Expand All @@ -148,14 +147,11 @@ handle_info(timeout, State) ->
schedule_timer(NewState),
{noreply, NewState};
handle_info({nodedown, Node}, #state{master = Node} = State) ->
telemetry:execute([amoc, cluster, master_node_down],
#{count => 1},
#{node => Node, state => state_to_map(State)}),
amoc_telemetry:execute_nodes([cluster, master_node_down], [Node], state_to_map(State)),
erlang:halt(),
{noreply, State};
handle_info({nodedown, Node}, State) ->
telemetry:execute([amoc, cluster, nodedown], #{count => 1},
#{node => Node, state => state_to_map(State)}),
amoc_telemetry:execute_nodes([cluster, nodedown], [Node], state_to_map(State)),
{noreply, merge(connection_lost, [Node], State)};
handle_info(_Info, State) ->
{noreply, State}.
Expand Down
22 changes: 19 additions & 3 deletions src/amoc_telemetry.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
-module(amoc_telemetry).

-export([execute/3]).
-export([execute/3, execute_error/3, execute_msg_to_self/3, execute_nodes/3]).

execute(Name, Measurements, Metadata) ->
TimeStamp = erlang:monotonic_time(),
NameWithAmocPrefix = [amoc | Name],
PrefixedName = [amoc | Name],
MetadataWithTS = Metadata#{monotonic_time => TimeStamp},
telemetry:execute(NameWithAmocPrefix, Measurements, MetadataWithTS).
telemetry:execute(PrefixedName, Measurements, MetadataWithTS).

execute_error(Name, Metadata, Message) ->
PrefixedName = [amoc | Name],
MetadataWithLog = Metadata#{log_level => error, msg => Message},
telemetry:execute(PrefixedName, #{error => 1}, MetadataWithLog).

execute_msg_to_self(Name, Message, Metadata) ->
TimeStamp = erlang:monotonic_time(),
PrefixedName = [amoc | Name],
Measurements = #{msg => Message, process => self()},
MetadataWithTS = Metadata#{monotonic_time => TimeStamp},
telemetry:execute(PrefixedName, Measurements, MetadataWithTS).

execute_nodes(Name, Nodes, State) ->
PrefixedName = [amoc | Name],
telemetry:execute(PrefixedName, #{count => length(Nodes)}, #{nodes => Nodes, state => State}).
10 changes: 3 additions & 7 deletions src/amoc_throttle/amoc_throttle_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,10 @@ inc_n(#state{n = N, max_n = MaxN} = State) ->

internal_event(Msg, #state{name = Name} = State) ->
PrintableState = printable_state(State),
telemetry:execute([amoc, throttle, process],
#{msg => Msg, process => self()},
#{printable_state => PrintableState,
monotonic_time => erlang:monotonic_time(), name => Name});
amoc_telemetry:execute_msg_to_self([throttle, process], Msg,
#{printable_state => PrintableState, name => Name});
internal_event(Msg, Name) when is_atom(Name) ->
telemetry:execute([amoc, throttle, process],
#{msg => Msg, process => self()},
#{monotonic_time => erlang:monotonic_time(), name => Name}).
amoc_telemetry:execute_msg_to_self([throttle, process], Msg, #{name => Name}).

printable_state(#state{} = State) ->
Fields = record_info(fields, state),
Expand Down

0 comments on commit 7933fc0

Please sign in to comment.