Skip to content

Commit 18a819d

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 c767f8a commit 18a819d

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

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

148136
validate_registry(undefined, auto) ->
149137
{true, default};

0 commit comments

Comments
 (0)