Skip to content

Commit

Permalink
Transform config log statements into telemetry events
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Dec 5, 2023
1 parent 8a19b29 commit 97b0f46
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
10 changes: 10 additions & 0 deletions guides/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ event_name: [amoc, coordinator, start | stop | add | reset | timeout]
measurements: #{count => 1}
metadata: #{name => atom()}
```

## Config

### Internal events
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(), _ => _}
```
9 changes: 6 additions & 3 deletions src/amoc_config/amoc_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
%% @doc TODO
-module(amoc_config).

-include_lib("kernel/include/logger.hrl").
-include("amoc_config.hrl").

-export([get/1, get/2]).
Expand All @@ -20,13 +19,17 @@ get(Name) ->
get(Name, Default) when is_atom(Name) ->
case ets:lookup(amoc_config, Name) of
[] ->
?LOG_ERROR("no scenario setting ~p", [Name]),
telemetry:execute([amoc, config, get], #{},
#{log_class => error, msg => <<"no scenario setting">>,
scenario => Name}),
throw({invalid_setting, Name});
[#module_parameter{name = Name, value = undefined}] ->
Default;
[#module_parameter{name = Name, value = Value}] ->
Value;
InvalidLookupRet ->
?LOG_ERROR("invalid lookup return value ~p ~p", [Name, InvalidLookupRet]),
telemetry:execute([amoc, config, get], #{},
#{log_class => error, msg => <<"invalid lookup return value">>,
scenario => Name, return => InvalidLookupRet}),
throw({invalid_lookup_ret_value, InvalidLookupRet})
end.
13 changes: 5 additions & 8 deletions src/amoc_config/amoc_config_env.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

-export([get/1, get/2]).

-include_lib("kernel/include/logger.hrl").

-define(DEFAULT_PARSER_MODULE, amoc_config_parser).

-callback(parse_value(string()) -> {ok, amoc_config:value()} | {error, any()}).
Expand All @@ -41,12 +39,11 @@ get_os_env(Name, Default) ->
case parse_value(Value, Default) of
{ok, Term} -> Term;
{error, Error} ->
?LOG_ERROR("cannot parse environment variable, using default value.~n"
" parsing error: '~p'~n"
" variable name: '$~s'~n"
" variable value: '~s'~n"
" default value: '~p'~n",
[Error, EnvName, Value, Default]),
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">>}),
Default
end.

Expand Down
14 changes: 9 additions & 5 deletions src/amoc_config/amoc_config_verification.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
%% API
-export([process_scenario_config/2]).

-include_lib("kernel/include/logger.hrl").
-include("amoc_config.hrl").

%% @doc Applies the processing as provided by the `required_variable' list to the provided scenario config
Expand Down Expand Up @@ -40,12 +39,17 @@ verify(Fun, Value) ->
{true, NewValue} -> {true, NewValue};
{false, Reason} -> {false, {verification_failed, Reason}};
Ret ->
?LOG_ERROR("invalid verification method ~p(~p), return value : ~p",
[Fun, Value, Ret]),
telemetry:execute([amoc, config, verify], #{error => 1},
#{log_class => error, verification_method => Fun,
verification_arg => Value, verification_return => Ret,
msg => <<"invalid verification method">>}),
{false, {invalid_verification_return_value, Ret}}
catch
C:E:S ->
?LOG_ERROR("invalid verification method ~p(~p), exception: ~p ~p ~p",
[Fun, Value, 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">>}),
{false, {exception_during_verification, {C, E, S}}}
end.

0 comments on commit 97b0f46

Please sign in to comment.