Skip to content

Commit 5a10d6e

Browse files
committed
fix: add api patch
Signed-off-by: Bence Csati <bence.csati@axoflow.com>
1 parent b892496 commit 5a10d6e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Elasticsearch API 9.x Compatibility Patch
2+
#
3+
# Fixes crashes in elasticsearch-api gem 9.1.2 when connecting to ES 7.x/8.x servers.
4+
#
5+
# Bug: The gem expects ES 9 headers and crashes with NoMethodError when they're nil
6+
# Fix: Add nil safety checks and skip ES9-specific processing for ES 7/8
7+
#
8+
# This patch is only needed if using elasticsearch gem 9.x
9+
# Not needed if using elasticsearch gem 7.x or 8.x
10+
11+
require 'elasticsearch/api'
12+
13+
module Elasticsearch
14+
module API
15+
module Utils
16+
class << self
17+
if method_defined?(:update_ndjson_headers!)
18+
alias_method :original_update_ndjson_headers!, :update_ndjson_headers!
19+
20+
def update_ndjson_headers!(headers, client_headers)
21+
return headers unless client_headers.is_a?(Hash)
22+
23+
current_content = client_headers.keys.find { |c| c.to_s.match?(/content[-_]?type/i) }
24+
return headers unless current_content
25+
26+
content_value = client_headers[current_content]
27+
return headers unless content_value
28+
29+
# ES 7/8 compatibility: Only process ES9-specific headers
30+
# If no "compatible-with" present, this is ES 7/8 format
31+
return headers unless content_value.to_s.include?('compatible-with')
32+
33+
# ES 9 detected, safe to call original
34+
original_update_ndjson_headers!(headers, client_headers)
35+
rescue StandardError => e
36+
warn "[elasticsearch-api-compat] Failed to update headers: #{e.class} - #{e.message}"
37+
headers
38+
end
39+
end
40+
end
41+
end
42+
end
43+
end

lib/fluent/plugin/out_elasticsearch.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
require_relative 'elasticsearch_index_lifecycle_management'
2828
require_relative 'elasticsearch_tls'
2929
require_relative 'elasticsearch_fallback_selector'
30+
require_relative 'elasticsearch_api_bugfix'
3031
begin
3132
require_relative 'oj_serializer'
3233
rescue LoadError
@@ -631,6 +632,11 @@ def client(host = nil, compress_connection = false)
631632

632633
ssl_options = { verify: @ssl_verify, ca_file: @ca_file}.merge(@ssl_version_options)
633634

635+
transport_options_hash = {
636+
headers: headers || { 'Content-Type' => 'application/json' },
637+
request: { timeout: @request_timeout },
638+
ssl: ssl_options,
639+
}
634640
transport = TRANSPORT_CLASS::Transport::HTTP::Faraday.new(connection_options.merge(
635641
options: {
636642
reload_connections: local_reload_connections,

0 commit comments

Comments
 (0)