Skip to content

Commit 9818d67

Browse files
committed
Adjust Content-Type for ES9
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent d029312 commit 9818d67

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/fluent/plugin/out_elasticsearch.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def initialize(retry_stream)
7373
DEFAULT_RELOAD_AFTER = -1
7474
DEFAULT_TARGET_BULK_BYTES = -1
7575
DEFAULT_POLICY_ID = "logstash-policy"
76+
ES9_CONTENT_TYPE = "application/vnd.elasticsearch+x-ndjson; compatible-with=9"
7677

7778
config_param :host, :string, :default => 'localhost'
7879
config_param :port, :integer, :default => 9200
@@ -146,7 +147,7 @@ def initialize(retry_stream)
146147
config_param :sniffer_class_name, :string, :default => nil
147148
config_param :selector_class_name, :string, :default => nil
148149
config_param :reload_after, :integer, :default => DEFAULT_RELOAD_AFTER
149-
config_param :content_type, :enum, list: [:"application/json", :"application/x-ndjson"], :default => :"application/json",
150+
config_param :content_type, :enum, list: [:"application/json", :"application/x-ndjson", :"#{ES9_CONTENT_TYPE}"], :default => :"application/json",
150151
:deprecated => <<EOC
151152
elasticsearch gem v6.0.2 starts to use correct Content-Type. Please upgrade elasticserach gem and stop to use this option.
152153
see: https://github.com/elastic/elasticsearch-ruby/pull/514
@@ -350,10 +351,20 @@ class << self
350351
log.warn "Detected ES 7.x: `_doc` will be used as the document `_type`."
351352
@type_name = '_doc'.freeze
352353
end
353-
if @last_seen_major_version >= 8 && @type_name != DEFAULT_TYPE_NAME_ES_7x
354-
log.debug "Detected ES 8.x or above: This parameter has no effect."
354+
if @last_seen_major_version == 8 && @type_name != DEFAULT_TYPE_NAME_ES_7x
355+
log.debug "Detected ES 8.x: This parameter has no effect."
355356
@type_name = nil
356357
end
358+
if @last_seen_major_version >= 9
359+
if @type_name != DEFAULT_TYPE_NAME_ES_7x
360+
log.debug "Detected ES 9.x or above: This parameter has no effect."
361+
@type_name = nil
362+
end
363+
if @content_type != ES9_CONTENT_TYPE
364+
log.trace "Detected ES 9.x or above: Content-Type will be adjusted."
365+
@content_type = ES9_CONTENT_TYPE
366+
end
367+
end
357368
end
358369

359370
if @validate_client_version && !dry_run?

test/plugin/test_out_elasticsearch.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ def stub_elastic_info(url="http://localhost:9200/", version=elasticsearch_versio
9090
end
9191

9292
def stub_elastic(url="http://localhost:9200/_bulk")
93-
stub_request(:post, url).with do |req|
93+
headers = if elasticsearch_miscellaneous_content_type?
94+
{ "Content-Type" => "application/vnd.elasticsearch+x-ndjson; compatible-with=9" }
95+
else
96+
{}
97+
end
98+
99+
stub_request(:post, url).with({headers: headers}) do |req|
94100
@index_cmds = req.body.split("\n").map {|r| JSON.parse(r) }
95101
end.to_return({:status => 200, :body => "", :headers => { 'Content-Type' => 'json', 'x-elastic-product' => 'Elasticsearch' } })
96102
end
@@ -4016,6 +4022,7 @@ def test_writes_to_default_index_with_compression
40164022
data('Elasticsearch 6' => [6, Fluent::Plugin::ElasticsearchOutput::DEFAULT_TYPE_NAME],
40174023
'Elasticsearch 7' => [7, Fluent::Plugin::ElasticsearchOutput::DEFAULT_TYPE_NAME_ES_7x],
40184024
'Elasticsearch 8' => [8, nil],
4025+
'Elasticsearch 9' => [9, nil],
40194026
)
40204027
def test_writes_to_default_type(data)
40214028
version, index_type = data

0 commit comments

Comments
 (0)