Skip to content

Commit ef2f5ec

Browse files
authored
Merge pull request #191 from mashhurs/support-elastic-transport-client
Adds elastic-transport client support
2 parents 21de990 + d6e26ea commit ef2f5ec

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.1.1
2+
- Add elastic-transport client support used in elasticsearch-ruby 8.x [#191](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/191)
3+
14
## 4.1.0
25
- Added support for custom headers [#188](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/188)
36

lib/logstash/filters/elasticsearch.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
require "monitor"
77

88
require_relative "elasticsearch/client"
9-
require_relative "elasticsearch/patches/_elasticsearch_transport_http_manticore"
109

1110
class LogStash::Filters::Elasticsearch < LogStash::Filters::Base
1211
config_name "elasticsearch"
@@ -174,6 +173,9 @@ def register
174173

175174
test_connection!
176175
setup_serverless
176+
if get_client.es_transport_client_type == "elasticsearch_transport"
177+
require_relative "elasticsearch/patches/_elasticsearch_transport_http_manticore"
178+
end
177179
end # def register
178180

179181
def filter(event)

lib/logstash/filters/elasticsearch/client.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# encoding: utf-8
22
require "elasticsearch"
33
require "base64"
4-
require "elasticsearch/transport/transport/http/manticore"
54

65

76
module LogStash
87
module Filters
98
class ElasticsearchClient
109

1110
attr_reader :client
11+
attr_reader :es_transport_client_type
1212

1313
BUILD_FLAVOR_SERVERLESS = 'serverless'.freeze
1414
DEFAULT_EAV_HEADER = { "Elastic-Api-Version" => "2023-10-31" }.freeze
@@ -44,7 +44,7 @@ def initialize(logger, hosts, options = {})
4444

4545
client_options = {
4646
hosts: hosts,
47-
transport_class: ::Elasticsearch::Transport::Transport::HTTP::Manticore,
47+
transport_class: get_transport_client_class,
4848
transport_options: transport_options,
4949
ssl: ssl_options,
5050
retry_on_failure: options[:retry_on_failure],
@@ -98,6 +98,20 @@ def setup_api_key(api_key)
9898
token = ::Base64.strict_encode64(api_key.value)
9999
{ 'Authorization' => "ApiKey #{token}" }
100100
end
101+
102+
def get_transport_client_class
103+
# LS-core includes `elasticsearch` gem. The gem is composed of two separate gems: `elasticsearch-api` and `elasticsearch-transport`
104+
# And now `elasticsearch-transport` is old, instead we have `elastic-transport`.
105+
# LS-core updated `elasticsearch` > 8: https://github.com/elastic/logstash/pull/17161
106+
# Following source bits are for the compatibility to support both `elasticsearch-transport` and `elastic-transport` gems
107+
require "elasticsearch/transport/transport/http/manticore"
108+
es_transport_client_type = "elasticsearch_transport"
109+
::Elasticsearch::Transport::Transport::HTTP::Manticore
110+
rescue ::LoadError
111+
require "elastic/transport/transport/http/manticore"
112+
es_transport_client_type = "elastic_transport"
113+
::Elastic::Transport::Transport::HTTP::Manticore
114+
end
101115
end
102116
end
103117
end

logstash-filter-elasticsearch.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-filter-elasticsearch'
4-
s.version = '4.1.0'
4+
s.version = '4.1.1'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Copies fields from previous log events in Elasticsearch to current events "
77
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"
88
s.authors = ["Elastic"]
99
s.email = '[email protected]'
10-
s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10+
s.homepage = "https://elastic.co/logstash"
1111
s.require_paths = ["lib"]
1212

1313
# Files
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
2121

2222
# Gem dependencies
2323
s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
24-
s.add_runtime_dependency 'elasticsearch', ">= 7.14.9" # LS >= 6.7 and < 7.14 all used version 5.0.5
24+
s.add_runtime_dependency 'elasticsearch', ">= 7.14.9", '< 9'
2525
s.add_runtime_dependency 'manticore', ">= 0.7.1"
2626
s.add_runtime_dependency 'logstash-mixin-ca_trusted_fingerprint_support', '~> 1.0'
2727
s.add_development_dependency 'cabin', ['~> 0.6']

spec/filters/elasticsearch_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103

104104
before(:each) do
105105
allow(LogStash::Filters::ElasticsearchClient).to receive(:new).and_return(client)
106+
allow(client).to receive(:es_transport_client_type).and_return('elasticsearch_transport')
106107
allow(client).to receive(:search).and_return(response)
107108
allow(plugin).to receive(:test_connection!)
108109
allow(plugin).to receive(:setup_serverless)
@@ -347,6 +348,7 @@
347348

348349
before do
349350
allow(plugin).to receive(:get_client).and_return(client_double)
351+
allow(client_double).to receive(:es_transport_client_type).and_return('elasticsearch_transport')
350352
allow(client_double).to receive(:client).and_return(transport_double)
351353
end
352354

@@ -821,6 +823,7 @@ def wait_receive_request
821823

822824
before(:each) do
823825
allow(LogStash::Filters::ElasticsearchClient).to receive(:new).and_return(client)
826+
allow(client).to receive(:es_transport_client_type).and_return('elasticsearch_transport')
824827
allow(plugin).to receive(:test_connection!)
825828
allow(plugin).to receive(:setup_serverless)
826829
plugin.register

0 commit comments

Comments
 (0)