Skip to content

Commit c4b1954

Browse files
committed
Emit queue_info metric
To allow filtering on queue type or membership status, we need an info metric for queues; see https://grafana.com/blog/2021/08/04/how-to-use-promql-joins-for-more-effective-queries-of-prometheus-metrics-at-scale/#info-metrics With this change, per-object metrics and the detailed metrics (if queue-related families are requested) will contain rabbitmq_queue_info / rabbitmq_detailed_queue_info with a value of 1 and labels including the queue name, vhost, queue type and membership status.
1 parent 22e4853 commit c4b1954

File tree

2 files changed

+97
-9
lines changed

2 files changed

+97
-9
lines changed

deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_core_metrics_collector.erl

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,25 @@ register() ->
301301
deregister_cleanup(_) -> ok.
302302

303303
collect_mf('detailed', Callback) ->
304-
collect(true, ?DETAILED_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_RAW), Callback),
304+
IncludedMFs = enabled_mfs_from_pdict(?METRICS_RAW),
305+
collect(true, ?DETAILED_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), IncludedMFs, Callback),
305306
collect(true, ?CLUSTER_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_CLUSTER), Callback),
307+
%% the detailed endpoint should emit queue_info only if queue metrics were requested
308+
MFs = proplists:get_keys(IncludedMFs),
309+
case lists:member(queue_coarse_metrics, MFs) orelse
310+
lists:member(queue_consumer_count, MFs) orelse
311+
lists:member(queue_metrics, MFs) of
312+
true ->
313+
emit_queue_info(?DETAILED_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), Callback);
314+
false -> ok
315+
end,
306316
%% identity is here to enable filtering on a cluster name (as already happens in existing dashboards)
307317
emit_identity_info(<<"detailed">>, Callback),
308318
ok;
309319
collect_mf('per-object', Callback) ->
310320
collect(true, ?METRIC_NAME_PREFIX, false, ?METRICS_RAW, Callback),
311321
totals(Callback),
322+
emit_queue_info(?METRIC_NAME_PREFIX, false, Callback),
312323
emit_identity_info(<<"per-object">>, Callback),
313324
ok;
314325
collect_mf('memory-breakdown', Callback) ->
@@ -406,6 +417,70 @@ identity_info(Endpoint) ->
406417
}]
407418
}.
408419

420+
emit_queue_info(Prefix, VHostsFilter, Callback) ->
421+
Help = <<"A metric with a constant '1' value and labels that provide some queue details">>,
422+
423+
QInfos = lists:foldl(
424+
fun(Q, Acc) ->
425+
#resource{virtual_host = VHost, name = Name} = QName = amqqueue:get_name(Q),
426+
case is_map(VHostsFilter) andalso maps:get(VHost, VHostsFilter) == false of
427+
true -> Acc;
428+
false ->
429+
QInfo0 = [{vhost, VHost}, {queue, Name}],
430+
case amqqueue:get_type(Q) of
431+
rabbit_classic_queue = T ->
432+
case amqqueue:qnode(Q) =:= node() of
433+
true ->
434+
QInfo = [{queue_type, T} | QInfo0],
435+
[{QInfo, 1}|Acc];
436+
false ->
437+
%% skip non-local queues
438+
Acc
439+
end;
440+
rabbit_quorum_queue = T ->
441+
case rabbit_quorum_queue:infos(QName, [local_state]) of
442+
[{local_state, not_member}] ->
443+
%% skip quorum queues with no local member
444+
Acc;
445+
[{local_state, State}] ->
446+
QInfo = [{queue_type, T}, {membership, State} | QInfo0],
447+
[{QInfo, 1}|Acc];
448+
_ ->
449+
%% the queue might get deleted metrics are collected
450+
Acc
451+
end;
452+
rabbit_stream_queue = T ->
453+
case rabbit_stream_queue:info(Q, [leader,members]) of
454+
[{leader, L}, {members, _}] when L =:= node() ->
455+
QInfo = [{queue_type, T}, {membership, leader} | QInfo0],
456+
[{QInfo, 1}|Acc];
457+
[{leader, _}, {members, Members}] ->
458+
case lists:member(node(), Members) of
459+
true ->
460+
QInfo = [{queue_type, stream}, {membership, follower} | QInfo0],
461+
[{QInfo, 1}|Acc];
462+
false ->
463+
%% skip stream queues with no local member
464+
Acc
465+
end;
466+
_ ->
467+
%% the queue might get deleted metrics are collected
468+
Acc
469+
end;
470+
T ->
471+
case amqqueue:qnode(Q) =:= node() of
472+
true ->
473+
QInfo = [{queue_type, T} | QInfo0],
474+
[{QInfo, 1}|Acc];
475+
false ->
476+
%% skip non-local queues
477+
Acc
478+
end
479+
end
480+
end
481+
end, [], rabbit_amqqueue:list()),
482+
Callback(prometheus_model_helpers:create_mf(<<Prefix/binary, "queue_info">>, Help, gauge, QInfos)).
483+
409484
add_metric_family({Name, Type, Help, Metrics}, Callback) ->
410485
MN = <<?METRIC_NAME_PREFIX/binary, (prometheus_model_helpers:metric_name(Name))/binary>>,
411486
Callback(create_mf(MN, Help, Type, Metrics)).

deps/rabbitmq_prometheus/test/rabbit_prometheus_http_SUITE.erl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,19 +533,32 @@ queue_consumer_count_single_vhost_per_object_test(Config) ->
533533

534534
%% There should be exactly 2 metrics returned (2 queues in that vhost, `queue_consumer_count` has only single metric)
535535
?assertEqual(#{rabbitmq_detailed_queue_consumers =>
536-
#{#{queue => "vhost-1-queue-with-consumer",vhost => "vhost-1"} => [1],
537-
#{queue => "vhost-1-queue-with-messages",vhost => "vhost-1"} => [0]}},
536+
#{#{queue => "vhost-1-queue-with-consumer",vhost => "vhost-1"} => [1],
537+
#{queue => "vhost-1-queue-with-messages",vhost => "vhost-1"} => [0]},
538+
rabbitmq_detailed_queue_info =>
539+
#{#{queue => "vhost-1-queue-with-consumer", vhost => "vhost-1", queue_type => "rabbit_classic_queue"} => [1],
540+
#{queue => "vhost-1-queue-with-messages", vhost => "vhost-1", queue_type => "rabbit_classic_queue"} => [1]}
541+
},
538542
parse_response(Body)),
539543
ok.
540544

541545
queue_consumer_count_all_vhosts_per_object_test(Config) ->
542546
Expected = #{rabbitmq_detailed_queue_consumers =>
543-
#{#{queue => "vhost-1-queue-with-consumer",vhost => "vhost-1"} => [1],
544-
#{queue => "vhost-1-queue-with-messages",vhost => "vhost-1"} => [0],
545-
#{queue => "vhost-2-queue-with-consumer",vhost => "vhost-2"} => [1],
546-
#{queue => "vhost-2-queue-with-messages",vhost => "vhost-2"} => [0],
547-
#{queue => "default-queue-with-consumer",vhost => "/"} => [1],
548-
#{queue => "default-queue-with-messages",vhost => "/"} => [0]}},
547+
#{#{queue => "vhost-1-queue-with-consumer",vhost => "vhost-1"} => [1],
548+
#{queue => "vhost-1-queue-with-messages",vhost => "vhost-1"} => [0],
549+
#{queue => "vhost-2-queue-with-consumer",vhost => "vhost-2"} => [1],
550+
#{queue => "vhost-2-queue-with-messages",vhost => "vhost-2"} => [0],
551+
#{queue => "default-queue-with-consumer",vhost => "/"} => [1],
552+
#{queue => "default-queue-with-messages",vhost => "/"} => [0]},
553+
554+
rabbitmq_detailed_queue_info =>
555+
#{#{queue => "default-queue-with-consumer", vhost => "/", queue_type => "rabbit_classic_queue"} => [1],
556+
#{queue => "default-queue-with-messages", vhost => "/", queue_type => "rabbit_classic_queue"} => [1],
557+
#{queue => "vhost-1-queue-with-consumer", vhost => "vhost-1", queue_type => "rabbit_classic_queue"} => [1],
558+
#{queue => "vhost-1-queue-with-messages", vhost => "vhost-1", queue_type => "rabbit_classic_queue"} => [1],
559+
#{queue => "vhost-2-queue-with-consumer", vhost => "vhost-2", queue_type => "rabbit_classic_queue"} => [1],
560+
#{queue => "vhost-2-queue-with-messages", vhost => "vhost-2", queue_type => "rabbit_classic_queue"} => [1]}
561+
},
549562

550563
%% No vhost given, all should be returned
551564
{_, Body1} = http_get_with_pal(Config, "/metrics/detailed?family=queue_consumer_count&per-object=1", [], 200),

0 commit comments

Comments
 (0)