rabbit_mgmt_data: Convert several stats to RabbitMQ 3.7.x format if needed

I.e., if the `empty_basic_get_metric` feature flag is disabled, we
remove the "get empty" queue metric from the slide, before returning it
to the caller.

[#159298729]
This commit is contained in:
Jean-Sébastien Pédron 2018-10-19 16:23:15 +02:00
parent 1388df59de
commit adbac7ddfc
4 changed files with 105 additions and 4 deletions

View File

@ -55,6 +55,7 @@
to_list/2,
to_list/3,
foldl/5,
map/2,
to_normalized_list/5]).
-export([timestamp/0,
@ -339,6 +340,15 @@ foldl(Now, Start0, Fun, Acc, #slide{max_n = _MaxN, buf2 = _Buf2,
interval = _Interval} = Slide) ->
lists:foldl(Fun, Acc, element(2, to_list_from(Now, Start0, Slide)) ++ [last]).
map(Fun, #slide{buf1 = Buf1, buf2 = Buf2, total = Total} = Slide) ->
BufFun = fun({Timestamp, Value}) ->
{Timestamp, Fun(Value)}
end,
MappedBuf1 = lists:map(BufFun, Buf1),
MappedBuf2 = lists:map(BufFun, Buf2),
MappedTotal = Fun(Total),
Slide#slide{buf1 = MappedBuf1, buf2 = MappedBuf2, total = MappedTotal}.
maybe_add_last_sample(_Now, #slide{total = T, n = N,
buf1 = [{_, T} | _] = Buf1}) ->
{N, Buf1};

View File

@ -11,7 +11,7 @@
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2016 Pivotal Software, Inc. All rights reserved.
%% Copyright (c) 2016-2018 Pivotal Software, Inc. All rights reserved.
%%
-module(rabbit_mgmt_data).
@ -345,7 +345,8 @@ lookup_smaller_sample(Table, Id) ->
[] ->
not_found;
[{_, Slide}] ->
exometer_slide:optimize(Slide)
Slide1 = exometer_slide:optimize(Slide),
maybe_convert_for_compatibility(Table, Slide1)
end.
-spec lookup_samples(atom(), any(), #range{}) -> maybe_slide().
@ -354,7 +355,8 @@ lookup_samples(Table, Id, Range) ->
[] ->
not_found;
[{_, Slide}] ->
exometer_slide:optimize(Slide)
Slide1 = exometer_slide:optimize(Slide),
maybe_convert_for_compatibility(Table, Slide1)
end.
lookup_all(Table, Ids, SecondKey) ->
@ -370,9 +372,26 @@ lookup_all(Table, Ids, SecondKey) ->
[] ->
not_found;
_ ->
exometer_slide:sum(Slides, empty(Table, 0))
Slide = exometer_slide:sum(Slides, empty(Table, 0)),
maybe_convert_for_compatibility(Table, Slide)
end.
maybe_convert_for_compatibility(Table, Slide)
when Table =:= channel_queue_stats_deliver_stats orelse
Table =:= channel_stats_deliver_stats orelse
Table =:= queue_stats_deliver_stats orelse
Table =:= vhost_stats_deliver_stats ->
ConversionNeeded = rabbit_feature_flags:is_disabled(
empty_basic_get_metric),
case ConversionNeeded of
false ->
Slide;
true ->
rabbit_mgmt_data_compat:drop_get_empty_queue_metric(Slide)
end;
maybe_convert_for_compatibility(_, Slide) ->
Slide.
get_table_keys(Table, Id0) ->
ets:select(Table, match_spec_keys(Id0)).

View File

@ -0,0 +1,49 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2018 Pivotal Software, Inc. All rights reserved.
%%
-module(rabbit_mgmt_data_compat).
-export([fill_get_empty_queue_metric/1,
drop_get_empty_queue_metric/1]).
fill_get_empty_queue_metric(Slide) ->
exometer_slide:map(
fun
(Value) when is_tuple(Value) andalso size(Value) =:= 8 ->
Value;
(Value) when is_tuple(Value) andalso size(Value) =:= 7 ->
%% We want to remote the last element, which is
%% the count of basic.get on empty queues.
list_to_tuple(
tuple_to_list(Value) ++ [0]);
(Value) ->
Value
end, Slide).
drop_get_empty_queue_metric(Slide) ->
exometer_slide:map(
fun
(Value) when is_tuple(Value) andalso size(Value) =:= 8 ->
%% We want to remote the last element, which is
%% the count of basic.get on empty queues.
list_to_tuple(
lists:sublist(
tuple_to_list(Value), size(Value) - 1));
(Value) when is_tuple(Value) andalso size(Value) =:= 7 ->
Value;
(Value) ->
Value
end, Slide).

View File

@ -0,0 +1,23 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License
%% at http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2018 Pivotal Software, Inc. All rights reserved.
%%
-module(rabbit_mgmt_ff).
-rabbit_feature_flag(
{empty_basic_get_metric,
#{desc => "Count AMQP `basic.get` on empty queues in stats",
stability => stable
}}).