Skip to content

Commit c606437

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 b568214 commit c606437

File tree

2 files changed

+93
-10
lines changed

2 files changed

+93
-10
lines changed

deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_core_metrics_collector.erl

Lines changed: 48 additions & 2 deletions
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,42 @@ identity_info(Endpoint) ->
406417
}]
407418
}.
408419

420+
has_leader_running_locally(Q) when is_pid(Q) ->
421+
node(Q) =:= node() andalso is_process_alive(Q);
422+
has_leader_running_locally({Name, Node}) when Node =:= node() ->
423+
is_process_alive(whereis(Name));
424+
has_leader_running_locally(_) ->
425+
false.
426+
427+
emit_queue_info(Prefix, VHostsFilter, Callback) ->
428+
Help = <<"A metric with a constant '1' value and labels that provide some queue details">>,
429+
QInfos = lists:foldl(
430+
fun(Q, Acc) ->
431+
#resource{virtual_host = VHost, name = Name} = amqqueue:get_name(Q),
432+
case is_map(VHostsFilter) andalso maps:get(VHost, VHostsFilter) == false of
433+
true -> Acc;
434+
false ->
435+
Type = amqqueue:get_type(Q),
436+
TypeState = amqqueue:get_type_state(Q),
437+
QInfo0 = [{vhost, VHost}, {queue, Name}, {queue_type, Type}],
438+
Members = maps:get(nodes, TypeState, []),
439+
case {has_leader_running_locally(amqqueue:get_pid(Q)),
440+
lists:member(node(), Members)} of
441+
{true, _} ->
442+
QInfo = [{membership, leader} | QInfo0],
443+
[{QInfo, 1}|Acc];
444+
{false, true} ->
445+
%% replicated queue with a non-leader member on this node
446+
QInfo = [{membership, follower} | QInfo0],
447+
[{QInfo, 1}|Acc];
448+
_ ->
449+
%% ignore queues with no local members
450+
Acc
451+
end
452+
end
453+
end, [], rabbit_amqqueue:list()),
454+
Callback(prometheus_model_helpers:create_mf(<<Prefix/binary, "queue_info">>, Help, gauge, QInfos)).
455+
409456
add_metric_family({Name, Type, Help, Metrics}, Callback) ->
410457
MN = <<?METRIC_NAME_PREFIX/binary, (prometheus_model_helpers:metric_name(Name))/binary>>,
411458
Callback(create_mf(MN, Help, Type, Metrics)).
@@ -890,4 +937,3 @@ vhosts_filter_from_pdict() ->
890937
Enabled = maps:from_list([ {VHost, true} || VHost <- L ]),
891938
maps:merge(All, Enabled)
892939
end.
893-

deps/rabbitmq_prometheus/test/rabbit_prometheus_http_SUITE.erl

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,19 +533,56 @@ 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",
540+
vhost => "vhost-1",
541+
queue_type => "rabbit_classic_queue",
542+
membership => "leader"} => [1],
543+
#{queue => "vhost-1-queue-with-messages",
544+
vhost => "vhost-1",
545+
queue_type => "rabbit_classic_queue",
546+
membership => "leader"} => [1]}
547+
},
538548
parse_response(Body)),
539549
ok.
540550

541551
queue_consumer_count_all_vhosts_per_object_test(Config) ->
542552
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]}},
553+
#{#{queue => "vhost-1-queue-with-consumer",vhost => "vhost-1"} => [1],
554+
#{queue => "vhost-1-queue-with-messages",vhost => "vhost-1"} => [0],
555+
#{queue => "vhost-2-queue-with-consumer",vhost => "vhost-2"} => [1],
556+
#{queue => "vhost-2-queue-with-messages",vhost => "vhost-2"} => [0],
557+
#{queue => "default-queue-with-consumer",vhost => "/"} => [1],
558+
#{queue => "default-queue-with-messages",vhost => "/"} => [0]},
559+
560+
rabbitmq_detailed_queue_info =>
561+
#{#{queue => "default-queue-with-consumer",
562+
vhost => "/",
563+
queue_type => "rabbit_classic_queue",
564+
membership => "leader"} => [1],
565+
#{queue => "default-queue-with-messages",
566+
vhost => "/",
567+
queue_type => "rabbit_classic_queue",
568+
membership => "leader"} => [1],
569+
#{queue => "vhost-1-queue-with-consumer",
570+
vhost => "vhost-1",
571+
queue_type => "rabbit_classic_queue",
572+
membership => "leader"} => [1],
573+
#{queue => "vhost-1-queue-with-messages",
574+
vhost => "vhost-1",
575+
queue_type => "rabbit_classic_queue",
576+
membership => "leader"} => [1],
577+
#{queue => "vhost-2-queue-with-consumer",
578+
vhost => "vhost-2",
579+
queue_type => "rabbit_classic_queue",
580+
membership => "leader"} => [1],
581+
#{queue => "vhost-2-queue-with-messages",
582+
vhost => "vhost-2",
583+
queue_type => "rabbit_classic_queue",
584+
membership => "leader"} => [1]}
585+
},
549586

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

0 commit comments

Comments
 (0)