gzip metrics

This commit is contained in:
Sam 2018-01-10 18:13:13 +11:00
parent a5140aa55c
commit fc1fd12581
2 changed files with 22 additions and 2 deletions

View File

@ -2,6 +2,8 @@
require 'webrick'
require 'timeout'
require 'zlib'
require 'stringio'
module PrometheusExporter::Server
class WebServer
@ -22,7 +24,16 @@ module PrometheusExporter::Server
res['ContentType'] = 'text/plain; charset=utf-8'
if req.path == '/metrics'
res.status = 200
res.body = metrics
if req.header["accept-encoding"].to_s.include?("gzip")
sio = StringIO.new
writer = Zlib::GzipWriter.new(sio)
writer.write(metrics)
writer.close
res.body = sio.string
res.header["content-encoding"] = "gzip"
else
res.body = metrics
end
elsif req.path == '/send-metrics'
handle_metrics(req, res)
else

View File

@ -90,7 +90,16 @@ class PrometheusExporterTest < Minitest::Test
assert_match(/199/, collector.prometheus_metrics_text)
body = Net::HTTP.get(URI("http://localhost:#{port}/metrics"))
body = nil
Net::HTTP.new("localhost", port).start do |http|
request = Net::HTTP::Get.new "/metrics"
http.request(request) do |response|
assert_equal(["gzip"], response.to_hash["content-encoding"])
body = response.body
end
end
assert_match(/199/, body)
one_minute = Time.now + 60