Skip to content

Commit b9efe52

Browse files
committed
speedup text format rendering by more than 30%
1 parent e48ea9e commit b9efe52

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/formats/prometheus_text_format.erl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ format(Registry) ->
7272
registry_collect_callback(Fd, Registry, Collector)
7373
end,
7474
prometheus_registry:collect(Registry, Callback),
75-
file:write(Fd, io_lib:format("\n", [])),
75+
file:write(Fd, "\n"),
7676
{ok, Size} = ram_file:get_size(Fd),
7777
{ok, Str} = file:pread(Fd, 0, Size),
7878
ok = file:close(Fd),
@@ -91,9 +91,8 @@ registry_collect_callback(Fd, Registry, Collector) ->
9191

9292
%% @private
9393
emit_mf_prologue(Fd, #'MetricFamily'{name=Name, help=Help, type=Type}) ->
94-
Bytes = io_lib:format("# TYPE ~s ~s\n# HELP ~s ~s\n",
95-
[Name, string_type(Type),
96-
Name, escape_metric_help(Help)]),
94+
Bytes = ["# TYPE ", Name, " ", string_type(Type), "\n# HELP ",
95+
Name, " ", escape_metric_help(Help), "\n"],
9796
file:write(Fd, Bytes).
9897

9998
%% @private
@@ -142,23 +141,23 @@ string_type('UNTYPED') ->
142141
labels_string([]) -> "";
143142
labels_string(Labels) ->
144143
Fun = fun (#'LabelPair'{name=Name, value=Value}) ->
145-
io_lib:format("~s=\"~s\"", [Name, escape_label_value(Value)])
144+
[Name, "=\"", escape_label_value(Value), "\""]
146145
end,
147-
"{" ++ string:join(lists:map(Fun, Labels), ",") ++ "}".
146+
["{", lists:join(",", lists:map(Fun, Labels)), "}"].
148147

149148
emit_series(Fd, Name, Labels, undefined) ->
150149
LString = labels_string(Labels),
151-
file:write(Fd, io_lib:format("~s" ++ LString ++ " NaN\n", [Name]));
150+
file:write(Fd, [Name, LString, " NaN\n"]);
152151
emit_series(Fd, Name, Labels, Value) ->
153152
LString = labels_string(Labels),
154-
file:write(Fd, io_lib:format("~s" ++ LString ++ " ~p\n", [Name, Value])).
153+
file:write(Fd, [Name, LString, " ", io_lib:format("~p", [Value]) , "\n"]).
155154

156155
%% @private
157156
escape_metric_help(Help) ->
158157
sub(sub(Help, "\\", "\\\\\\\\"), "\n", "\\\\n").
159158

160159
bound_to_label_value(Bound) when is_number(Bound) ->
161-
Bound;
160+
io_lib:format("~p", [Bound]);
162161
bound_to_label_value(infinity) ->
163162
"+Inf".
164163

@@ -168,8 +167,9 @@ escape_label_value(LValue) when is_list(LValue)->
168167
?ESCAPE_LVALUE(LValue);
169168
escape_label_value(LValue) when is_binary(LValue) ->
170169
?ESCAPE_LVALUE(LValue);
171-
escape_label_value(LValue) ->
172-
?ESCAPE_LVALUE(io_lib:format("~p", [LValue])).
170+
escape_label_value(Value) ->
171+
erlang:error({wtf, Value}).
172+
173173

174174
-spec sub(iodata(), string(), string()) -> string().
175175
sub(Str, Old, New) ->

0 commit comments

Comments
 (0)