Packages
prometheus
4.13.0
6.1.3
6.1.2
6.1.1
6.1.0
6.0.3
6.0.2
6.0.1
6.0.0
5.1.1
5.1.0
5.0.0
4.13.0
retired
4.12.0
4.11.0
4.10.0
4.9.1
4.9.0
4.8.2
4.8.1
4.8.0
4.6.0
4.5.0
4.4.1
4.4.0
4.3.0
4.2.2
4.2.0
4.1.0
4.0.1
4.0.0
3.5.1
3.5.0
3.4.6
3.4.5
3.4.4
3.4.3
3.4.2
3.4.1
3.4.0
3.3.2
3.3.1
3.3.0
3.2.3
3.2.2
3.2.1
3.1.1
3.1.0
3.0.1
3.0.0
3.0.0-rc1
3.0.0-alpha9
3.0.0-alpha8
3.0.0-alpha7
3.0.0-alpha6
3.0.0-alpha5
3.0.0-alpha4
3.0.0-alpha3
3.0.0-alpha2
3.0.0-alpha10
3.0.0-alpha1
2.2.0
2.1.0
2.0.0
1.7.0
1.6.0
1.5.0
1.0.2
1.0.1
1.0.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Prometheus.io client in Erlang
Retired package: Breaking changes regarding automated discovery of collectors, version was promoted to 5.0.0
Current section
Files
Jump to
Current section
Files
src/formats/prometheus_protobuf_format.erl
-module(prometheus_protobuf_format).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC("""
Serializes Prometheus registry using [protocol buffer format](http://bit.ly/2cxSuJP).
""").
-export([content_type/0, format/0, format/1]).
-behaviour(prometheus_format).
?DOC("Returns content type of the protocol buffer format.").
-spec content_type() -> binary().
content_type() ->
<<
"application/vnd.google.protobuf; "
"proto=io.prometheus.client.MetricFamily; "
"encoding=delimited"
>>.
?DOC(#{equiv => format(default)}).
?DOC("Formats `default` registry using protocol buffer format.").
-spec format() -> binary().
format() ->
format(default).
?DOC("Formats `Registry` using protocol buffer format.").
-spec format(Registry :: prometheus_registry:registry()) -> binary().
format(Registry) ->
{ok, Fd} = ram_file:open("", [write, read, binary]),
Callback = fun(_, Collector) ->
registry_collect_callback(Fd, Registry, Collector)
end,
prometheus_registry:collect(Registry, Callback),
{ok, Size} = ram_file:get_size(Fd),
{ok, Buf} = file:pread(Fd, 0, Size),
ok = file:close(Fd),
Buf.
registry_collect_callback(Fd, Registry, Collector) ->
Callback = fun(MF) -> file:write(Fd, delimited_encode_mf(MF)) end,
prometheus_collector:collect_mf(Registry, Collector, Callback).
delimited_encode_mf(MF) ->
IoRec = prometheus_model:encode_msg(MF),
Size = iolist_size(IoRec),
[e_varint(Size, <<>>), IoRec].
e_varint(N, Bin) when N =< 127 -> <<Bin/binary, N>>;
e_varint(N, Bin) ->
Bin2 = <<Bin/binary, (N band 127 bor 128)>>,
e_varint(N bsr 7, Bin2).