Skip to content

Commit 5d98688

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 08db661 commit 5d98688

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_core_metrics_collector.erl

Lines changed: 45 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,39 @@ 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+
QInfos = lists:foldl(
423+
fun(Q, Acc) ->
424+
#resource{virtual_host = VHost, name = Name} = amqqueue:get_name(Q),
425+
case is_map(VHostsFilter) andalso maps:get(VHost, VHostsFilter) == false of
426+
true -> Acc;
427+
false ->
428+
Type = amqqueue:get_type(Q),
429+
QInfo0 = [{vhost, VHost}, {queue, Name}, {queue_type, Type}],
430+
{Leader, Members} = Type:queue_topology(Q),
431+
case {rabbit_amqqueue:is_replicated(Q),
432+
Leader =:= node(),
433+
lists:member(node(), Members)} of
434+
{false, true, _} ->
435+
%% non-replicated queue running on this node
436+
[{QInfo0, 1}|Acc];
437+
{true, true, _} ->
438+
%% replicated queue with a leader on this node
439+
QInfo = [{membership, leader} | QInfo0],
440+
[{QInfo, 1}|Acc];
441+
{true, false, true} ->
442+
%% replicated queue with a non-leader member on this node
443+
QInfo = [{membership, follower} | QInfo0],
444+
[{QInfo, 1}|Acc];
445+
_ ->
446+
%% ignore queues with no local members
447+
Acc
448+
end
449+
end
450+
end, [], rabbit_amqqueue:list()),
451+
Callback(prometheus_model_helpers:create_mf(<<Prefix/binary, "queue_info">>, Help, gauge, QInfos)).
452+
409453
add_metric_family({Name, Type, Help, Metrics}, Callback) ->
410454
MN = <<?METRIC_NAME_PREFIX/binary, (prometheus_model_helpers:metric_name(Name))/binary>>,
411455
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)