Skip to content

Commit

Permalink
Ensure probe metrics are gauge type
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekwegr committed Nov 4, 2024
1 parent af53925 commit ccc6eb9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/instrument/mongoose_instrument.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
-type handler_key() :: atom(). % key in the `instrumentation' section of the config file
-type handler_fun() :: fun((event_name(), labels(), config(), measurements()) -> any()).
-type handlers() :: {[handler_fun()], config()}.
-type handler_map() :: #{labels() => handlers()}.
-type execution_time() :: integer().
-type measure_fun(Result) :: fun((execution_time(), Result) -> measurements()).
-type handler_module_opts() :: #{atom() => any()}.
Expand Down Expand Up @@ -274,16 +275,45 @@ set_up_and_register_event(EventName, Labels, Config, Events) ->
{ExistingLabels, _, _} = maps:next(maps:iterator(HandlerMap)),
case label_keys(ExistingLabels) of
LabelKeys ->
Handlers = do_set_up(EventName, Labels, Config),
Events#{EventName := HandlerMap#{Labels => Handlers}};
handle_metrics_config(EventName, Labels, Config, HandlerMap, Events);
ExistingKeys ->
{error, #{what => inconsistent_labels,
event_name => EventName, labels => Labels,
existing_label_keys => ExistingKeys}}
end;
#{} ->
handle_metrics_config(EventName, Labels, Config, new, Events)
end.

-spec handle_metrics_config(event_name(), labels(), config(), handler_map() | new, event_map()) ->
event_map() | {error, map()}.
handle_metrics_config(EventName, Labels, Config, HandlerMap, Events) ->
case filter_improper_probe_metrics(Config) of
{ok, _} ->
Handlers = do_set_up(EventName, Labels, Config),
Events#{EventName => #{Labels => Handlers}}
case HandlerMap of
new ->
Events#{EventName => #{Labels => Handlers}};
_ ->
Events#{EventName := HandlerMap#{Labels => Handlers}}
end;
{error, FilteredMetrics} ->
{error, #{what => non_gauge_metrics_in_probe,
event_name => EventName, labels => Labels,
improper_metrics => FilteredMetrics}}
end.

-spec filter_improper_probe_metrics(config()) -> {ok, #{}} | {error, metrics()}.
filter_improper_probe_metrics(Config) ->
case Config of
#{probe := _, metrics := Metrics} ->
FilteredMetrics = maps:filter(fun(_, V) -> V =/= gauge end, Metrics),
case map_size(FilteredMetrics) of
0 -> {ok, FilteredMetrics};
_ -> {error, FilteredMetrics}
end;
_ ->
{ok, #{}}
end.

-spec do_set_up(event_name(), labels(), config()) -> handlers().
Expand Down
14 changes: 14 additions & 0 deletions test/mongoose_instrument_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ api_test_cases() ->
set_up_and_span_with_error,
span_fails_when_not_set_up,
set_up_probe,
set_up_probe_with_incorrect_metric_type,
set_up_failing_probe,
set_up_and_tear_down_probe,
unexpected_events,
Expand Down Expand Up @@ -224,6 +225,19 @@ set_up_probe(Config) ->
fun(L) -> length(L) > 1 end),
?assertEqual([ExpectedEvent, ExpectedEvent], History2).

set_up_probe_with_incorrect_metric_type(Config) ->
Event = ?config(event, Config),
Cfg = #{metrics => #{test_metric => gauge},
probe => #{module => ?MODULE, interval => 1}},
Cfg1 = #{metrics => ImproperMetrics = #{test_metric => spiral},
probe => #{module => ?MODULE, interval => 1}},
Specs = [{Event, #{key => value1}, Cfg},
{Event, #{key => value2}, Cfg1}],
?assertError(#{what := non_gauge_metrics_in_probe, improper_metrics := ImproperMetrics},
mongoose_instrument:set_up(Event, ?LABELS, Cfg1)),
?assertError(#{what := non_gauge_metrics_in_probe, improper_metrics := ImproperMetrics},
mongoose_instrument:set_up(Specs)).

set_up_failing_probe(Config) ->
Event = ?config(event, Config),
Labels = ?LABELS,
Expand Down

0 comments on commit ccc6eb9

Please sign in to comment.