diff --git a/lib/influxdb/client/http.rb b/lib/influxdb/client/http.rb index c4a7cf1..f2d623c 100644 --- a/lib/influxdb/client/http.rb +++ b/lib/influxdb/client/http.rb @@ -2,6 +2,7 @@ require 'cgi' require 'net/http' require 'net/https' +require 'zlib' module InfluxDB # rubocop:disable Metrics/MethodLength @@ -22,7 +23,8 @@ def get(url, options = {}) end def post(url, data) - headers = { "Content-Type" => "application/octet-stream" } + headers = { "Content-Type" => "application/octet-stream", + "Content-Encoding" => "gzip" } connect_with_retry do |http| response = do_request http, Net::HTTP::Post.new(url, headers), data @@ -70,7 +72,7 @@ def connect_with_retry def do_request(http, req, data = nil) req.basic_auth config.username, config.password if basic_auth? - req.body = data if data + req.body = Zlib.gzip(data, level: Zlib::BEST_SPEED) if data http.request(req) end diff --git a/spec/influxdb/cases/retry_requests_spec.rb b/spec/influxdb/cases/retry_requests_spec.rb index e608cb5..c9f9a52 100644 --- a/spec/influxdb/cases/retry_requests_spec.rb +++ b/spec/influxdb/cases/retry_requests_spec.rb @@ -36,7 +36,7 @@ stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 's', db: database }, headers: { "Content-Type" => "application/octet-stream" }, - body: body + body: Zlib.gzip(body, level: 1) ).to_raise(Timeout::Error) end @@ -76,7 +76,7 @@ .with( query: { u: "username", p: "password", precision: 's', db: database }, headers: { "Content-Type" => "application/octet-stream" }, - body: body + body: Zlib.gzip(body, level: 1) ) .to_raise(Timeout::Error).then .to_raise(Timeout::Error).then @@ -95,7 +95,7 @@ stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 's', db: database }, headers: { "Content-Type" => "application/octet-stream" }, - body: body + body: Zlib.gzip(body, level: 1) ).to_return(status: 401) expect { client.write_point(series, data) }.to raise_error(InfluxDB::AuthenticationError) diff --git a/spec/influxdb/cases/write_points_spec.rb b/spec/influxdb/cases/write_points_spec.rb index ad4e2c4..62f3b69 100644 --- a/spec/influxdb/cases/write_points_spec.rb +++ b/spec/influxdb/cases/write_points_spec.rb @@ -27,8 +27,8 @@ before do stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 's', db: database }, - headers: { "Content-Type" => "application/octet-stream" }, - body: body + headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" }, + body: Zlib.gzip(body, level: 1) ).to_return(status: 204) end @@ -63,8 +63,8 @@ before do stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 's', db: database }, - headers: { "Content-Type" => "application/octet-stream" }, - body: body + headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" }, + body: Zlib.gzip(body, level: 1) ).to_return(status: 204) end @@ -89,8 +89,8 @@ before do stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 's', db: database }, - headers: { "Content-Type" => "application/octet-stream" }, - body: body + headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" }, + body: Zlib.gzip(body, level: 1) ).to_return(status: 204) end @@ -118,8 +118,8 @@ before do stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 'ms', db: database }, - headers: { "Content-Type" => "application/octet-stream" }, - body: body + headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" }, + body: Zlib.gzip(body, level: 1) ).to_return(status: 204) end it "should POST multiple points" do @@ -144,8 +144,8 @@ before do stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 's', db: database, rp: 'rp_1_hour' }, - headers: { "Content-Type" => "application/octet-stream" }, - body: body + headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" }, + body: Zlib.gzip(body, level: 1) ).to_return(status: 204) end it "should POST multiple points" do @@ -170,8 +170,8 @@ before do stub_request(:post, "http://influxdb.test:9999/write").with( query: { u: "username", p: "password", precision: 's', db: 'overriden_db' }, - headers: { "Content-Type" => "application/octet-stream" }, - body: body + headers: { "Content-Type" => "application/octet-stream", "Content-Encoding" => "gzip" }, + body: Zlib.gzip(body, level: 1) ).to_return(status: 204) end it "should POST multiple points" do diff --git a/spec/influxdb/client_spec.rb b/spec/influxdb/client_spec.rb index bf40b8c..54b68e2 100644 --- a/spec/influxdb/client_spec.rb +++ b/spec/influxdb/client_spec.rb @@ -35,7 +35,7 @@ it "POST" do stub_request(:post, stub_url).with(headers: auth_header).to_return(status: 204) - expect(subject.post(url, {})).to be_a(Net::HTTPNoContent) + expect(subject.post(url, "{}")).to be_a(Net::HTTPNoContent) end end