Skip to content

Commit ebd7b2e

Browse files
committed
rabbit_prometheus_handler: Delete manual gzip handling
The Cowboy stream handler `cowboy_compress_h` is already enabled for this endpoint so there's no need to perform gzipping manually.
1 parent 6ec6875 commit ebd7b2e

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

deps/rabbitmq_prometheus/src/rabbit_prometheus_handler.erl

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ setup_metrics(Registry) ->
5959
{labels, ["registry", "content_type"]},
6060
{registry, Registry}],
6161
ScrapeEncodedSize = [{name, ?SCRAPE_ENCODED_SIZE},
62-
{help, "Scrape size, encoded"},
62+
{help, "Scrape size, encoded (deprecated)"},
6363
{labels, ["registry", "content_type", "encoding"]},
6464
{registry, Registry}],
6565

@@ -104,43 +104,31 @@ format_metrics(Request, Registry) ->
104104
ContentType = prometheus_text_format:content_type(),
105105
Encoding = accept_encoding_header:negotiate(AcceptEncoding, [<<"identity">>,
106106
<<"gzip">>]),
107-
Headers = #{<<"content-type">> => ContentType,
108-
<<"content-encoding">> => Encoding},
109-
case Encoding of
110-
<<"gzip">> ->
111-
Scrape = prometheus_summary:observe_duration(
112-
Registry,
113-
?SCRAPE_DURATION,
114-
[Registry, ContentType],
115-
fun () -> prometheus_text_format:format(Registry) end),
116-
prometheus_summary:observe(Registry,
117-
?SCRAPE_SIZE,
118-
[Registry, ContentType],
119-
iolist_size(Scrape)),
120-
Encoded = zlib:gzip(Scrape),
121-
prometheus_summary:observe(telemetry_registry(),
122-
?SCRAPE_ENCODED_SIZE,
123-
[Registry, ContentType, Encoding],
124-
iolist_size(Encoded)),
125-
cowboy_req:reply(200, Headers, Encoded, Request);
126-
<<"identity">> ->
127-
Req = cowboy_req:stream_reply(200, Headers, Request),
128-
Fmt = fun(Data, Size) ->
129-
cowboy_req:stream_body(Data, nofin, Req),
130-
Size + iolist_size(Data)
131-
end,
132-
Size = prometheus_summary:observe_duration(
133-
Registry,
134-
?SCRAPE_DURATION,
135-
[Registry, ContentType],
136-
fun () -> prometheus_text_format:format_into(Registry, 0, Fmt) end),
137-
cowboy_req:stream_body(<<>>, fin, Req),
138-
prometheus_summary:observe(Registry,
139-
?SCRAPE_SIZE,
140-
[Registry, ContentType],
141-
Size),
142-
Req
143-
end.
107+
Headers0 = #{<<"content-type">> => ContentType},
108+
Headers = case Encoding of
109+
<<"gzip">> ->
110+
%% `cowboy_compress_h` transparently compresses the data
111+
%% while streaming if `content-encoding` is unset.
112+
Headers0;
113+
_ ->
114+
Headers0#{<<"content-encoding">> => Encoding}
115+
end,
116+
Req = cowboy_req:stream_reply(200, Headers, Request),
117+
Fmt = fun(Data, Size) ->
118+
cowboy_req:stream_body(Data, nofin, Req),
119+
Size + iolist_size(Data)
120+
end,
121+
Size = prometheus_summary:observe_duration(
122+
Registry,
123+
?SCRAPE_DURATION,
124+
[Registry, ContentType],
125+
fun () -> prometheus_text_format:format_into(Registry, 0, Fmt) end),
126+
cowboy_req:stream_body(<<>>, fin, Req),
127+
prometheus_summary:observe(Registry,
128+
?SCRAPE_SIZE,
129+
[Registry, ContentType],
130+
Size),
131+
Req.
144132

145133
validate_registry(undefined, auto) ->
146134
{true, default};

0 commit comments

Comments
 (0)