Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.1.1
- Adds elastic-transport client support [#191](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/191)

## 4.1.0
- Added support for custom headers [#188](https://github.com/logstash-plugins/logstash-filter-elasticsearch/pull/188)

Expand Down
4 changes: 3 additions & 1 deletion lib/logstash/filters/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "monitor"

require_relative "elasticsearch/client"
require_relative "elasticsearch/patches/_elasticsearch_transport_http_manticore"

class LogStash::Filters::Elasticsearch < LogStash::Filters::Base
config_name "elasticsearch"
Expand Down Expand Up @@ -174,6 +173,9 @@ def register

test_connection!
setup_serverless
if get_client.es_transport_client_type == "elasticsearch_transport"
require_relative "elasticsearch/patches/_elasticsearch_transport_http_manticore"
Copy link
Contributor Author

@mashhurs mashhurs Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review note: _elasticsearch_transport_http_manticore sets user-agent and accept-encoding headers for the es-transport-client versions between 7.2 and 7.16. It seems to me we can remove this but I have kept so far as there are active 7.x users.
(I don't know which LS versions included >7.2 and <7.16 es-transport-client)

end
end # def register

def filter(event)
Expand Down
14 changes: 12 additions & 2 deletions lib/logstash/filters/elasticsearch/client.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# encoding: utf-8
require "elasticsearch"
require "base64"
require "elasticsearch/transport/transport/http/manticore"


module LogStash
module Filters
class ElasticsearchClient

attr_reader :client
attr_reader :es_transport_client_type

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

client_options = {
hosts: hosts,
transport_class: ::Elasticsearch::Transport::Transport::HTTP::Manticore,
transport_class: get_transport_client_class,
transport_options: transport_options,
ssl: ssl_options,
retry_on_failure: options[:retry_on_failure],
Expand Down Expand Up @@ -98,6 +98,16 @@ def setup_api_key(api_key)
token = ::Base64.strict_encode64(api_key.value)
{ 'Authorization' => "ApiKey #{token}" }
end

def get_transport_client_class
require "elasticsearch/transport/transport/http/manticore"
es_transport_client_type = "elasticsearch_transport"
::Elasticsearch::Transport::Transport::HTTP::Manticore
rescue ::LoadError
require "elastic/transport/transport/http/manticore"
es_transport_client_type = "elastic_transport"
::Elastic::Transport::Transport::HTTP::Manticore
end
end
end
end
4 changes: 2 additions & 2 deletions logstash-filter-elasticsearch.gemspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Gem::Specification.new do |s|

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

# Files
Expand Down
3 changes: 3 additions & 0 deletions spec/filters/elasticsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@

before(:each) do
allow(LogStash::Filters::ElasticsearchClient).to receive(:new).and_return(client)
allow(client).to receive(:es_transport_client_type).and_return('elasticsearch_transport')
allow(client).to receive(:search).and_return(response)
allow(plugin).to receive(:test_connection!)
allow(plugin).to receive(:setup_serverless)
Expand Down Expand Up @@ -347,6 +348,7 @@

before do
allow(plugin).to receive(:get_client).and_return(client_double)
allow(client_double).to receive(:es_transport_client_type).and_return('elasticsearch_transport')
allow(client_double).to receive(:client).and_return(transport_double)
end

Expand Down Expand Up @@ -821,6 +823,7 @@ def wait_receive_request

before(:each) do
allow(LogStash::Filters::ElasticsearchClient).to receive(:new).and_return(client)
allow(client).to receive(:es_transport_client_type).and_return('elasticsearch_transport')
allow(plugin).to receive(:test_connection!)
allow(plugin).to receive(:setup_serverless)
plugin.register
Expand Down
Loading