diff --git a/CHANGELOG.md b/CHANGELOG.md index 38ddca8160952f..a5d601500f1210 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 8.0.2 + - Fix bug where logging errors for bad response codes would raise an unhandled exception + ## 8.0.1 - Fix some documentation issues diff --git a/lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb b/lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb index b1d51f12b32450..6e12db12639d2c 100644 --- a/lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb +++ b/lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb @@ -66,7 +66,7 @@ def perform_request(url, method, path, params={}, body=nil) request_uri = format_url(url, path) - resp = @manticore.send(method.downcase, request_uri, params) + resp = @manticore.send(method.downcase, request_uri.to_s, params) # Manticore returns lazy responses by default # We want to block for our usage, this will wait for the repsonse @@ -106,7 +106,7 @@ def format_url(url, path_and_query=nil) request_uri.path = "#{request_uri.path}/#{parsed_path_and_query.path}".gsub(/\/{2,}/, "/") - request_uri.to_s + request_uri end def close diff --git a/logstash-output-elasticsearch.gemspec b/logstash-output-elasticsearch.gemspec index ade38506a81fa1..b606c2070c476e 100644 --- a/logstash-output-elasticsearch.gemspec +++ b/logstash-output-elasticsearch.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-output-elasticsearch' - s.version = '8.0.1' + s.version = '8.0.2' s.licenses = ['apache-2.0'] s.summary = "Logstash Output to Elasticsearch" s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program" diff --git a/spec/unit/outputs/elasticsearch/http_client/manticore_adapter_spec.rb b/spec/unit/outputs/elasticsearch/http_client/manticore_adapter_spec.rb index e27489c1fa81e9..f9279e39a95787 100644 --- a/spec/unit/outputs/elasticsearch/http_client/manticore_adapter_spec.rb +++ b/spec/unit/outputs/elasticsearch/http_client/manticore_adapter_spec.rb @@ -44,19 +44,44 @@ end end + describe "bad response codes" do + let(:uri) { ::LogStash::Util::SafeURI.new("http://localhost:9200") } + + it "should raise a bad response code error" do + resp = double("response") + allow(resp).to receive(:call) + allow(resp).to receive(:code).and_return(500) + allow(resp).to receive(:body).and_return("a body") + + expect(subject.manticore).to receive(:get). + with(uri.to_s + "/", anything). + and_return(resp) + + uri_with_path = uri.clone + uri_with_path.path = "/" + + expect(::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError).to receive(:new). + with(resp.code, uri_with_path, nil, resp.body).and_call_original + + expect do + subject.perform_request(uri, :get, "/") + end.to raise_error(::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError) + end + end + describe "format_url" do let(:url) { ::LogStash::Util::SafeURI.new("http://localhost:9200/path/") } let(:path) { "_bulk" } subject { described_class.new(double("logger"), {}) } it "should add the path argument to the uri's path" do - expect(java.net.URI.new(subject.format_url(url, path)).path).to eq("/path/_bulk") + expect(subject.format_url(url, path).path).to eq("/path/_bulk") end context "when uri contains query parameters" do let(:query_params) { "query=value&key=value2" } let(:url) { ::LogStash::Util::SafeURI.new("http://localhost:9200/path/?#{query_params}") } - let(:formatted) { java.net.URI.new(subject.format_url(url, path))} + let(:formatted) { subject.format_url(url, path)} it "should retain query_params after format" do expect(formatted.query).to eq(query_params) @@ -73,7 +98,7 @@ context "when the path contains query parameters" do let(:path) { "/special_bulk?pathParam=1"} - let(:formatted) { java.net.URI.new(subject.format_url(url, path)) } + let(:formatted) { subject.format_url(url, path) } it "should add the path correctly" do expect(formatted.path).to eq("#{url.path}special_bulk") @@ -86,10 +111,10 @@ context "when uri contains credentials" do let(:url) { ::LogStash::Util::SafeURI.new("http://myuser:mypass@localhost:9200") } - let(:formatted) { java.net.URI.new(subject.format_url(url, path)) } + let(:formatted) { subject.format_url(url, path) } it "should remove credentials after format" do - expect(formatted.user_info).to be_nil + expect(formatted.userinfo).to be_nil end end end