feat: rename olp metrics to overload_protection metrics

This commit is contained in:
zhongwencool 2023-08-23 15:25:54 +08:00
parent b9a97923c4
commit 6bb15541c7
6 changed files with 58 additions and 38 deletions

View File

@ -19,6 +19,7 @@
%% API
-export([add_handler/0, remove_handler/0, pre_config_update/3]).
-export([is_olp_enabled/0]).
-define(ZONES, [zones]).
@ -33,3 +34,13 @@ remove_handler() ->
%% replace the old config with the new config
pre_config_update(?ZONES, NewRaw, _OldRaw) ->
{ok, NewRaw}.
is_olp_enabled() ->
maps:fold(
fun
(_, #{overload_protection := #{enable := true}}, _Acc) -> true;
(_, _, Acc) -> Acc
end,
false,
emqx_conf:get([zones], #{})
).

View File

@ -67,6 +67,7 @@
terminate/2,
code_change/3
]).
-export([olp_metrics/0]).
%% BACKW: v4.3.0
-export([upgrade_retained_delayed_counter_type/0]).
@ -269,13 +270,16 @@
%% Overload protetion counters
-define(OLP_METRICS, [
{counter, 'olp.delay.ok'},
{counter, 'olp.delay.timeout'},
{counter, 'olp.hbn'},
{counter, 'olp.gc'},
{counter, 'olp.new_conn'}
{counter, 'overload_protection.delay.ok'},
{counter, 'overload_protection.delay.timeout'},
{counter, 'overload_protection.hibernation'},
{counter, 'overload_protection.gc'},
{counter, 'overload_protection.new_conn'}
]).
olp_metrics() ->
lists:map(fun({_, Metric}) -> Metric end, ?OLP_METRICS).
-record(state, {next_idx = 1}).
-record(metric, {name, type, idx}).
@ -701,9 +705,9 @@ reserved_idx('authorization.cache_hit') -> 302;
reserved_idx('authentication.success') -> 310;
reserved_idx('authentication.success.anonymous') -> 311;
reserved_idx('authentication.failure') -> 312;
reserved_idx('olp.delay.ok') -> 400;
reserved_idx('olp.delay.timeout') -> 401;
reserved_idx('olp.hbn') -> 402;
reserved_idx('olp.gc') -> 403;
reserved_idx('olp.new_conn') -> 404;
reserved_idx('overload_protection.delay.ok') -> 400;
reserved_idx('overload_protection.delay.timeout') -> 401;
reserved_idx('overload_protection.hibernation') -> 402;
reserved_idx('overload_protection.gc') -> 403;
reserved_idx('overload_protection.new_conn') -> 404;
reserved_idx(_) -> undefined.

View File

@ -38,11 +38,11 @@
| backoff_new_conn.
-type cnt_name() ::
'olp.delay.ok'
| 'olp.delay.timeout'
| 'olp.hbn'
| 'olp.gc'
| 'olp.new_conn'.
'overload_protection.delay.ok'
| 'overload_protection.delay.timeout'
| 'overload_protection.hibernation'
| 'overload_protection.gc'
| 'overload_protection.new_conn'.
-define(overload_protection, overload_protection).
@ -63,10 +63,10 @@ backoff(Zone) ->
false ->
false;
ok ->
emqx_metrics:inc('olp.delay.ok'),
emqx_metrics:inc('overload_protection.delay.ok'),
ok;
timeout ->
emqx_metrics:inc('olp.delay.timeout'),
emqx_metrics:inc('overload_protection.delay.timeout'),
timeout
end;
_ ->
@ -76,18 +76,18 @@ backoff(Zone) ->
%% @doc If forceful GC should be skipped when the system is overloaded.
-spec backoff_gc(Zone :: atom()) -> boolean().
backoff_gc(Zone) ->
do_check(Zone, ?FUNCTION_NAME, 'olp.gc').
do_check(Zone, ?FUNCTION_NAME, 'overload_protection.gc').
%% @doc If hibernation should be skipped when the system is overloaded.
-spec backoff_hibernation(Zone :: atom()) -> boolean().
backoff_hibernation(Zone) ->
do_check(Zone, ?FUNCTION_NAME, 'olp.hbn').
do_check(Zone, ?FUNCTION_NAME, 'overload_protection.hibernation').
%% @doc Returns {error, overloaded} if new connection should be
%% closed when system is overloaded.
-spec backoff_new_conn(Zone :: atom()) -> ok | {error, overloaded}.
backoff_new_conn(Zone) ->
case do_check(Zone, ?FUNCTION_NAME, 'olp.new_conn') of
case do_check(Zone, ?FUNCTION_NAME, 'overload_protection.new_conn') of
true ->
{error, overloaded};
false ->

View File

@ -118,7 +118,7 @@ new_conn(
{stop, stream_accept_error, S}
end;
true ->
emqx_metrics:inc('olp.new_conn'),
emqx_metrics:inc('overload_protection.new_conn'),
_ = quicer:async_shutdown_connection(
Conn,
?QUIC_CONNECTION_SHUTDOWN_FLAG_NONE,

View File

@ -87,10 +87,25 @@ create_metric_views() ->
create_gauge(Meter, VmGauge, fun ?MODULE:get_vm_gauge/1),
ClusterGauge = [{'node.running', 0}, {'node.stopped', 0}],
create_gauge(Meter, ClusterGauge, fun ?MODULE:get_cluster_gauge/1),
Metrics = lists:map(fun({K, V}) -> {to_metric_name(K), V, unit(K)} end, emqx_metrics:all()),
Metrics0 = filter_olp_metrics(emqx_metrics:all()),
Metrics = lists:map(fun({K, V}) -> {to_metric_name(K), V, unit(K)} end, Metrics0),
create_counter(Meter, Metrics, fun ?MODULE:get_metric_counter/1),
ok.
filter_olp_metrics(Metrics) ->
case emqx_config_zones:is_olp_enabled() of
true ->
Metrics;
false ->
OlpMetrics = emqx_metrics:olp_metrics(),
lists:filter(
fun({K, _}) ->
not lists:member(K, OlpMetrics)
end,
Metrics
)
end.
to_metric_name('messages.dropped.await_pubrel_timeout') ->
'messages.dropped.expired';
to_metric_name('packets.connect.received') ->

View File

@ -455,15 +455,15 @@ emqx_collect(emqx_session_terminated, Stats) ->
%% Metrics - overload protection
emqx_collect(emqx_overload_protection_delay_ok, Stats) ->
counter_metric(?C('olp.delay.ok', Stats));
counter_metric(?C('overload_protection.delay.ok', Stats));
emqx_collect(emqx_overload_protection_delay_timeout, Stats) ->
counter_metric(?C('olp.delay.timeout', Stats));
counter_metric(?C('overload_protection.delay.timeout', Stats));
emqx_collect(emqx_overload_protection_hibernation, Stats) ->
counter_metric(?C('olp.hbn', Stats));
counter_metric(?C('overload_protection.hibernation', Stats));
emqx_collect(emqx_overload_protection_gc, Stats) ->
counter_metric(?C('olp.gc', Stats));
counter_metric(?C('overload_protection.gc', Stats));
emqx_collect(emqx_overload_protection_new_conn, Stats) ->
counter_metric(?C('olp.new_conn', Stats));
counter_metric(?C('overload_protection.new_conn', Stats));
%%--------------------------------------------------------------------
%% Metrics - acl
emqx_collect(emqx_authorization_allow, Stats) ->
@ -559,7 +559,7 @@ emqx_metrics_packets() ->
].
emqx_metrics_olp() ->
case is_olp_enabled() of
case emqx_config_zones:is_olp_enabled() of
true ->
[
emqx_overload_protection_delay_ok,
@ -572,16 +572,6 @@ emqx_metrics_olp() ->
[]
end.
is_olp_enabled() ->
maps:fold(
fun
(_, #{overload_protection := #{enable := true}}, _Acc) -> true;
(_, _, Acc) -> Acc
end,
false,
emqx_conf:get([zones], #{})
).
emqx_metrics_acl() ->
[
emqx_authorization_allow,