Skip to content

Commit 9cb9304

Browse files
Merge pull request #3587 from rabbitmq/prometheus-label-pre-render-optimization
Pre-render prometheus labels (cherry picked from commit 1b7a8f8) Conflicts: rabbitmq-components.mk workspace_helpers.bzl
1 parent 27b47bf commit 9cb9304

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_core_metrics_collector.erl

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
%% as observed by RabbitMQ.
2626

2727
%% Used by `/metrics` and `/metrics/per-object`.
28-
-define(METRIC_NAME_PREFIX, "rabbitmq_").
28+
-define(METRIC_NAME_PREFIX, <<"rabbitmq_">>).
2929

3030
%% Used by `/metrics/detailed` endpoint
31-
-define(DETAILED_METRIC_NAME_PREFIX, "rabbitmq_detailed_").
31+
-define(DETAILED_METRIC_NAME_PREFIX, <<"rabbitmq_detailed_">>).
3232

3333
%% ==The source of these metrics can be found in the rabbit_core_metrics module==
3434
%% The relevant files are:
@@ -328,7 +328,8 @@ identity_info() ->
328328
}.
329329

330330
add_metric_family({Name, Type, Help, Metrics}, Callback) ->
331-
Callback(create_mf(?METRIC_NAME(Name), Help, Type, Metrics)).
331+
MN = <<?METRIC_NAME_PREFIX/binary, (prometheus_model_helpers:metric_name(Name))/binary>>,
332+
Callback(create_mf(MN, Help, Type, Metrics)).
332333

333334
mf(Callback, Prefix, Contents, Data) ->
334335
[begin
@@ -340,7 +341,7 @@ mf(Callback, Prefix, Contents, Data) ->
340341
end,
341342
Callback(
342343
create_mf(
343-
[Prefix, prometheus_model_helpers:metric_name(Name)],
344+
<<Prefix/binary, (prometheus_model_helpers:metric_name(Name))/binary>>,
344345
Help,
345346
catch_boolean(Type),
346347
?MODULE,
@@ -357,7 +358,7 @@ mf(Callback, Prefix, Contents, Data) ->
357358
end,
358359
Callback(
359360
create_mf(
360-
[Prefix, prometheus_model_helpers:metric_name(Name)],
361+
<<Prefix/binary, (prometheus_model_helpers:metric_name(Name))/binary>>,
361362
Help,
362363
catch_boolean(Type),
363364
?MODULE,
@@ -369,7 +370,7 @@ mf(Callback, Prefix, Contents, Data) ->
369370
mf_totals(Callback, Name, Type, Help, Size) ->
370371
Callback(
371372
create_mf(
372-
?METRIC_NAME(Name),
373+
<<?METRIC_NAME_PREFIX/binary, (prometheus_model_helpers:metric_name(Name))/binary>>,
373374
Help,
374375
catch_boolean(Type),
375376
Size
@@ -383,28 +384,36 @@ labels(Item) ->
383384
label(element(1, Item)).
384385

385386
label(#resource{virtual_host = VHost, kind = exchange, name = Name}) ->
386-
[{vhost, VHost}, {exchange, Name}];
387+
<<"vhost=\"", VHost/binary, "\",exchange=\"", Name/binary, "\"">>;
387388
label(#resource{virtual_host = VHost, kind = queue, name = Name}) ->
388-
[{vhost, VHost}, {queue, Name}];
389+
<<"vhost=\"", VHost/binary, "\",queue=\"", Name/binary, "\"">>;
389390
label({P, {#resource{virtual_host = QVHost, kind = queue, name = QName},
390391
#resource{virtual_host = EVHost, kind = exchange, name = EName}}}) when is_pid(P) ->
391392
%% channel_queue_exchange_metrics {channel_id, {queue_id, exchange_id}}
392-
[{channel, P}, {queue_vhost, QVHost}, {queue, QName},
393-
{exchange_vhost, EVHost}, {exchange, EName}];
393+
<<"channel=\"", (iolist_to_binary(pid_to_list(P)))/binary, "\",",
394+
"queue_vhost=\"", QVHost/binary, "\",",
395+
"queue=\"", QName/binary, "\",",
396+
"exchange_vhost=\"", EVHost/binary, "\",",
397+
"exchange=\"", EName/binary, "\""
398+
>>;
394399
label({RemoteAddress, Username, Protocol}) when is_binary(RemoteAddress), is_binary(Username),
395400
is_atom(Protocol) ->
396401
lists:filter(fun({_, V}) ->
397402
V =/= <<>>
398403
end, [{remote_address, RemoteAddress}, {username, Username},
399404
{protocol, atom_to_binary(Protocol, utf8)}]);
400405
label({I1, I2}) ->
401-
label(I1) ++ label(I2);
406+
case {label(I1), label(I2)} of
407+
{<<>>, L} -> L;
408+
{L, <<>>} -> L;
409+
{L1, L2} -> <<L1/binary, ",", L2/binary>>
410+
end;
402411
label(P) when is_pid(P) ->
403-
[{channel, P}];
412+
<<"channel=\"", (iolist_to_binary(pid_to_list(P)))/binary, "\"">>;
404413
label(A) when is_atom(A) ->
405414
case is_protocol(A) of
406-
true -> [{protocol, atom_to_binary(A, utf8)}];
407-
false -> []
415+
true -> <<"protocol=\"", (atom_to_binary(A, utf8))/binary, "\"">>;
416+
false -> <<>>
408417
end.
409418

410419
is_protocol(P) ->

rabbitmq-components.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ dep_cowboy = hex 2.8.0
115115
dep_cowlib = hex 2.9.1
116116
dep_jsx = hex 3.1.0
117117
dep_looking_glass = git https://github.com/rabbitmq/looking_glass master
118-
dep_prometheus = hex 4.8.1
118+
dep_prometheus = git https://github.com/deadtrickster/prometheus.erl 06425c21a39c1564164f1cc3fe5bdfa8b23b1f78
119119
dep_ra = hex 2.0.2
120120
dep_ranch = hex 2.1.0
121121
dep_recon = hex 2.5.1

workspace_helpers.bzl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,12 @@ erlang_lib(
178178
remote = "https://github.com/rabbitmq/osiris.git",
179179
)
180180

181-
hex_pm_bazel_erlang_lib(
181+
github_bazel_erlang_lib(
182182
name = "prometheus",
183-
version = "4.8.1",
184-
sha256 = "6edfbe928d271c7f657a6f2c46258738086584bd6cae4a000b8b9a6009ba23a5",
183+
repo = "prometheus.erl",
184+
org = "deadtrickster",
185+
ref = "06425c21a39c1564164f1cc3fe5bdfa8b23b1f78",
186+
version = "06425c21a39c1564164f1cc3fe5bdfa8b23b1f78",
185187
first_srcs = [
186188
"src/prometheus_collector.erl",
187189
"src/prometheus_format.erl",

0 commit comments

Comments
 (0)