Skip to content

Commit cbb8774

Browse files
committed
Allow application to pre-render labels for text format
Builds on top of prometheus-erl#136 This is useful when e.g. escaping is not needed, as it is in case with RabbitMQ, where no escaping is needed because strict rules already apply to names. This change provides not only direct speedup, but also significantly reduced garbage collection overhead, as for every output line the proplist with labels and the list with converted `#'LabelPair'{}`'s are replaced with just a single binary. Using RabbitMQ as an example, time to produce ~630k metrics went from ~6 to ~3 seconds. Downside is that it's only compatible with text format, and spec for `#'LabelPair'{}' is not respected.
1 parent 1880b1e commit cbb8774

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/model/prometheus_model_helpers.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
-type label_name() :: term().
5151
-type label_value() :: term().
5252
-type label() :: {label_name(), label_value()}.
53-
-type labels() :: [label()].
53+
-type pre_rendered_labels() :: binary().
54+
-type labels() :: [label()] | pre_rendered_labels().
5455
-type value() :: float() | integer() | undefined | infinity.
5556
-type prometheus_boolean() :: boolean() | number() | list() | undefined.
5657
-type gauge() :: value() | {value()} | {labels(), value()}.
@@ -328,6 +329,18 @@ histogram_metric(Labels, Buckets, Count, Sum) ->
328329
%% @doc Equivalent to
329330
%% {@link label_pair/1. `lists:map(fun label_pair/1, Labels)'}.
330331
%% @end
332+
%%
333+
%% NB `is_binary' clause here is for a special optimization for text
334+
%% format only: client code can pre-generate final labels string,
335+
%% e.g. when it knows when character escaping is not needed. This
336+
%% avoids direct performance cost of character escaping, and also
337+
%% reduces garabage collection pressure, as intermediate lists of
338+
%% tuples/records are not created at all. This optimization is used by
339+
%% RabbitMQ prometheus plugin (which calls `create_mf/5', and it ends
340+
%% here).
341+
%% WARNING Works only for text format, protobuf format export will
342+
%% fail with an error.
343+
label_pairs(B) when is_binary(B) -> B;
331344
label_pairs(Labels) -> lists:map(fun label_pair/1, Labels).
332345

333346
%% @doc

0 commit comments

Comments
 (0)