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 %% API
-export([add_handler/0, remove_handler/0, pre_config_update/3]). -export([add_handler/0, remove_handler/0, pre_config_update/3]).
-export([is_olp_enabled/0]).
-define(ZONES, [zones]). -define(ZONES, [zones]).
@ -33,3 +34,13 @@ remove_handler() ->
%% replace the old config with the new config %% replace the old config with the new config
pre_config_update(?ZONES, NewRaw, _OldRaw) -> pre_config_update(?ZONES, NewRaw, _OldRaw) ->
{ok, NewRaw}. {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, terminate/2,
code_change/3 code_change/3
]). ]).
-export([olp_metrics/0]).
%% BACKW: v4.3.0 %% BACKW: v4.3.0
-export([upgrade_retained_delayed_counter_type/0]). -export([upgrade_retained_delayed_counter_type/0]).
@ -269,13 +270,16 @@
%% Overload protetion counters %% Overload protetion counters
-define(OLP_METRICS, [ -define(OLP_METRICS, [
{counter, 'olp.delay.ok'}, {counter, 'overload_protection.delay.ok'},
{counter, 'olp.delay.timeout'}, {counter, 'overload_protection.delay.timeout'},
{counter, 'olp.hbn'}, {counter, 'overload_protection.hibernation'},
{counter, 'olp.gc'}, {counter, 'overload_protection.gc'},
{counter, 'olp.new_conn'} {counter, 'overload_protection.new_conn'}
]). ]).
olp_metrics() ->
lists:map(fun({_, Metric}) -> Metric end, ?OLP_METRICS).
-record(state, {next_idx = 1}). -record(state, {next_idx = 1}).
-record(metric, {name, type, idx}). -record(metric, {name, type, idx}).
@ -701,9 +705,9 @@ reserved_idx('authorization.cache_hit') -> 302;
reserved_idx('authentication.success') -> 310; reserved_idx('authentication.success') -> 310;
reserved_idx('authentication.success.anonymous') -> 311; reserved_idx('authentication.success.anonymous') -> 311;
reserved_idx('authentication.failure') -> 312; reserved_idx('authentication.failure') -> 312;
reserved_idx('olp.delay.ok') -> 400; reserved_idx('overload_protection.delay.ok') -> 400;
reserved_idx('olp.delay.timeout') -> 401; reserved_idx('overload_protection.delay.timeout') -> 401;
reserved_idx('olp.hbn') -> 402; reserved_idx('overload_protection.hibernation') -> 402;
reserved_idx('olp.gc') -> 403; reserved_idx('overload_protection.gc') -> 403;
reserved_idx('olp.new_conn') -> 404; reserved_idx('overload_protection.new_conn') -> 404;
reserved_idx(_) -> undefined. reserved_idx(_) -> undefined.

View File

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

View File

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

View File

@ -87,10 +87,25 @@ create_metric_views() ->
create_gauge(Meter, VmGauge, fun ?MODULE:get_vm_gauge/1), create_gauge(Meter, VmGauge, fun ?MODULE:get_vm_gauge/1),
ClusterGauge = [{'node.running', 0}, {'node.stopped', 0}], ClusterGauge = [{'node.running', 0}, {'node.stopped', 0}],
create_gauge(Meter, ClusterGauge, fun ?MODULE:get_cluster_gauge/1), 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), create_counter(Meter, Metrics, fun ?MODULE:get_metric_counter/1),
ok. 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') -> to_metric_name('messages.dropped.await_pubrel_timeout') ->
'messages.dropped.expired'; 'messages.dropped.expired';
to_metric_name('packets.connect.received') -> to_metric_name('packets.connect.received') ->

View File

@ -455,15 +455,15 @@ emqx_collect(emqx_session_terminated, Stats) ->
%% Metrics - overload protection %% Metrics - overload protection
emqx_collect(emqx_overload_protection_delay_ok, Stats) -> 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) -> 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) -> 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) -> 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) -> 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 %% Metrics - acl
emqx_collect(emqx_authorization_allow, Stats) -> emqx_collect(emqx_authorization_allow, Stats) ->
@ -559,7 +559,7 @@ emqx_metrics_packets() ->
]. ].
emqx_metrics_olp() -> emqx_metrics_olp() ->
case is_olp_enabled() of case emqx_config_zones:is_olp_enabled() of
true -> true ->
[ [
emqx_overload_protection_delay_ok, emqx_overload_protection_delay_ok,
@ -572,16 +572,6 @@ emqx_metrics_olp() ->
[] []
end. 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_metrics_acl() ->
[ [
emqx_authorization_allow, emqx_authorization_allow,