1: -module(mongoose_instrument_log_SUITE).
    2: -compile([export_all, nowarn_export_all]).
    3: 
    4: -include_lib("eunit/include/eunit.hrl").
    5: -include_lib("common_test/include/ct.hrl").
    6: -include("log_helper.hrl").
    7: 
    8: %% Setup and teardown
    9: 
   10: all() ->
   11:     [{group, default_level},
   12:      {group, configured_level}].
   13: 
   14: groups() ->
   15:     [{default_level, [parallel], cases()},
   16:      {configured_level, [parallel], cases()}].
   17: 
   18: cases() ->
   19:     [log_with_default_level,
   20:      log_level_in_event_config,
   21:      log_level_in_measurement,
   22:      log_level_in_measurement_overrides_event_config].
   23: 
   24: init_per_suite(Config) ->
   25:     mongoose_logs:set_module_loglevel(mongoose_instrument_log, debug),
   26:     log_helper:set_up(),
   27:     Config.
   28: 
   29: end_per_suite(_Config) ->
   30:     log_helper:tear_down(),
   31:     mongoose_logs:clear_module_loglevel(mongoose_instrument_log).
   32: 
   33: init_per_group(Group, Config) ->
   34:     Opts = #{log := #{level := LogLevel}} = opts(Group),
   35:     mongoose_config:set_opts(#{instrumentation => Opts}),
   36:     Config1 = async_helper:start(Config, mongoose_instrument, start_link, []),
   37:     mongoose_instrument:persist(),
   38:     [{loglevel, LogLevel} | Config1].
   39: 
   40: end_per_group(_Group, Config) ->
   41:     async_helper:stop_all(Config),
   42:     mongoose_config:erase_opts().
   43: 
   44: init_per_testcase(Case, Config) ->
   45:     log_helper:subscribe(),
   46:     [{event, concat(Case, event)} | Config].
   47: 
   48: end_per_testcase(_Case, _Config) ->
   49:     log_helper:unsubscribe().
   50: 
   51: opts(default_level) ->
   52:     #{log => config_parser_helper:default_config([instrumentation, log])};
   53: opts(configured_level) ->
   54:     #{log => #{level => info}}.
   55: 
   56: %% Test cases
   57: 
   58: log_with_default_level(Config) ->
   59:     Event = ?config(event, Config),
   60:     Level = ?config(loglevel, Config),
   61:     ok = mongoose_instrument:set_up(Event, #{host_type => <<"host1">>}, #{}),
   62:     ok = mongoose_instrument:execute(Event, #{host_type => <<"host1">>}, #{count => 1}),
   63:     ?assertLog(Level, #{what := Event, labels := #{host_type := <<"host1">>},
   64:                         measurements := #{count := 1}}).
   65: 
   66: log_level_in_event_config(Config) ->
   67:     Event = ?config(event, Config),
   68:     ok = mongoose_instrument:set_up(Event, #{host_type => <<"host1">>}, #{loglevel => error}),
   69:     ok = mongoose_instrument:execute(Event, #{host_type => <<"host1">>}, #{count => 1}),
   70:     ?assertLog(error, #{what := Event, labels := #{host_type := <<"host1">>},
   71:                         measurements := #{count := 1}}).
   72: 
   73: log_level_in_measurement(Config) ->
   74:     Event = ?config(event, Config),
   75:     ok = mongoose_instrument:set_up(Event, #{host_type => <<"host1">>}, #{}),
   76:     ok = mongoose_instrument:execute(Event, #{host_type => <<"host1">>}, #{count => 1,
   77:                                                                            loglevel => warning}),
   78:     ?assertLog(warning, #{what := Event, labels := #{host_type := <<"host1">>},
   79:                           measurements := #{count := 1}}).
   80: 
   81: log_level_in_measurement_overrides_event_config(Config) ->
   82:     Event = ?config(event, Config),
   83:     ok = mongoose_instrument:set_up(Event, #{host_type => <<"host1">>}, #{loglevel => error}),
   84:     ok = mongoose_instrument:execute(Event, #{host_type => <<"host1">>}, #{count => 1,
   85:                                                                            loglevel => warning}),
   86:     ?assertLog(warning, #{what := Event, labels := #{host_type := <<"host1">>},
   87:                           measurements := #{count := 1}}).
   88: 
   89: concat(A1, A2) ->
   90:     list_to_atom(atom_to_list(A1) ++ "_" ++ atom_to_list(A2)).