-
Notifications
You must be signed in to change notification settings - Fork 84
ES|QL support #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mashhurs
wants to merge
22
commits into
logstash-plugins:main
Choose a base branch
from
mashhurs:esql-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ES|QL support #233
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a307db2
ES|QL support: ESQL executor implementation, response type to accept …
mashhurs 9c35f22
Merge with upstream, warn if query doesn't include METADATA which DSL…
mashhurs 6f99055
Run unit tests with the LS version which actually supports the ES|QL.
mashhurs 086a592
Add query type to the agent. DRY of supported ES/LS versions.
mashhurs e30e0f9
Remove query type from user-agent since it is useless, put back accid…
mashhurs 7746c14
Initial docs added for ES|QL.
mashhurs 76303d8
Update query to include condition with string.
mashhurs 1fb29f7
Tested escaped chars cases, uses orignal query.
mashhurs 5d47f2f
Integration tests added.
mashhurs c291e24
Skip the ESQL test if LS with the ES client which doesn't support ESQ…
mashhurs 22e72e9
Add comments on response type and query params about ES|QL acceptance…
mashhurs af6e24a
Update spec/inputs/integration/elasticsearch_esql_spec.rb
mashhurs 4ce6fa4
Integration test skip condition correction.
mashhurs 4ed69ff
Introduce query_params option to accept drop_null_columns, set defaul…
mashhurs 0725f98
Fix the failed integration test.
mashhurs cfb36f3
Request dropping null columns and filter out null values. Consider se…
mashhurs a92a71e
Apply suggestions from code review
mashhurs d4f559d
Apply code review suggestions: to use decorator as a proc call, doc s…
mashhurs 65eb675
Rename warning msg field name to avoid conflicts. Generate a target a…
mashhurs 789f467
Ignore sub-fields with warninigs and keep only parent.
mashhurs fefe6a0
Introduce at high level which other params such as , etc.. follow it…
mashhurs e108c87
Add a tech preview fior the ESQL.
mashhurs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ class LogStash::Inputs::Elasticsearch < LogStash::Inputs::Base | |
require 'logstash/inputs/elasticsearch/paginated_search' | ||
require 'logstash/inputs/elasticsearch/aggregation' | ||
require 'logstash/inputs/elasticsearch/cursor_tracker' | ||
require 'logstash/inputs/elasticsearch/esql' | ||
|
||
include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1, :v8 => :v1) | ||
include LogStash::PluginMixins::ECSCompatibilitySupport::TargetCheck | ||
|
@@ -96,15 +97,21 @@ class LogStash::Inputs::Elasticsearch < LogStash::Inputs::Base | |
# The index or alias to search. | ||
config :index, :validate => :string, :default => "logstash-*" | ||
|
||
# The query to be executed. Read the Elasticsearch query DSL documentation | ||
# for more info | ||
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html | ||
# A type of Elasticsearch query, provided by @query. This will validate query shape and other params. | ||
config :query_type, :validate => %w[dsl esql], :default => 'dsl' | ||
|
||
# The query to be executed. DSL or ES|QL (when `query_type => 'esql'`) query shape is accepted. | ||
# Read the following documentations for more info | ||
# Query DSL: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html | ||
# ES|QL: https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html | ||
config :query, :validate => :string, :default => '{ "sort": [ "_doc" ] }' | ||
|
||
# This allows you to speccify the response type: either hits or aggregations | ||
# where hits: normal search request | ||
# aggregations: aggregation request | ||
config :response_type, :validate => ['hits', 'aggregations'], :default => 'hits' | ||
# This allows you to specify the DSL response type: one of [hits, aggregations] | ||
# where | ||
# hits: normal search request | ||
# aggregations: aggregation request | ||
# Note that this param is invalid when `query_type => 'esql'`, ES|QL response shape is always a tabular format | ||
config :response_type, :validate => %w[hits aggregations], :default => 'hits' | ||
|
||
# This allows you to set the maximum number of hits returned per scroll. | ||
config :size, :validate => :number, :default => 1000 | ||
|
@@ -286,6 +293,9 @@ class LogStash::Inputs::Elasticsearch < LogStash::Inputs::Base | |
DEFAULT_EAV_HEADER = { "Elastic-Api-Version" => "2023-10-31" }.freeze | ||
INTERNAL_ORIGIN_HEADER = { 'x-elastic-product-origin' => 'logstash-input-elasticsearch'}.freeze | ||
|
||
LS_ESQL_SUPPORT_VERSION = "8.17.4" # the version started using elasticsearch-ruby v8 | ||
ES_ESQL_SUPPORT_VERSION = "8.11.0" | ||
|
||
def initialize(params={}) | ||
super(params) | ||
|
||
|
@@ -302,10 +312,17 @@ def register | |
fill_hosts_from_cloud_id | ||
setup_ssl_params! | ||
|
||
@base_query = LogStash::Json.load(@query) | ||
if @slices | ||
@base_query.include?('slice') && fail(LogStash::ConfigurationError, "Elasticsearch Input Plugin's `query` option cannot specify specific `slice` when configured to manage parallel slices with `slices` option") | ||
@slices < 1 && fail(LogStash::ConfigurationError, "Elasticsearch Input Plugin's `slices` option must be greater than zero, got `#{@slices}`") | ||
if @query_type == 'esql' | ||
validate_ls_version_for_esql_support! | ||
validate_esql_query! | ||
not_allowed_options = original_params.keys & %w(index size slices search_api docinfo docinfo_target docinfo_fields response_type tracking_field) | ||
raise(LogStash::ConfigurationError, "Configured #{not_allowed_options} params are not allowed while using ES|QL query") if not_allowed_options&.size > 1 | ||
else | ||
@base_query = LogStash::Json.load(@query) | ||
if @slices | ||
@base_query.include?('slice') && fail(LogStash::ConfigurationError, "Elasticsearch Input Plugin's `query` option cannot specify specific `slice` when configured to manage parallel slices with `slices` option") | ||
@slices < 1 && fail(LogStash::ConfigurationError, "Elasticsearch Input Plugin's `slices` option must be greater than zero, got `#{@slices}`") | ||
end | ||
end | ||
|
||
@retries < 0 && fail(LogStash::ConfigurationError, "Elasticsearch Input Plugin's `retries` option must be equal or greater than zero, got `#{@retries}`") | ||
|
@@ -341,11 +358,13 @@ def register | |
|
||
test_connection! | ||
|
||
validate_es_for_esql_support! | ||
|
||
setup_serverless | ||
|
||
setup_search_api | ||
|
||
setup_query_executor | ||
@query_executor = create_query_executor | ||
|
||
setup_cursor_tracker | ||
|
||
|
@@ -363,16 +382,6 @@ def run(output_queue) | |
end | ||
end | ||
|
||
def get_query_object | ||
if @cursor_tracker | ||
query = @cursor_tracker.inject_cursor(@query) | ||
@logger.debug("new query is #{query}") | ||
else | ||
query = @query | ||
end | ||
LogStash::Json.load(query) | ||
end | ||
|
||
## | ||
# This can be called externally from the query_executor | ||
public | ||
|
@@ -383,6 +392,23 @@ def push_hit(hit, output_queue, root_field = '_source') | |
record_last_value(event) | ||
end | ||
|
||
def decorate_event(event) | ||
decorate(event) | ||
end | ||
|
||
private | ||
|
||
def get_query_object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. review note: moved to private area |
||
return @query if @query_type == 'esql' | ||
if @cursor_tracker | ||
query = @cursor_tracker.inject_cursor(@query) | ||
@logger.debug("new query is #{query}") | ||
else | ||
query = @query | ||
end | ||
LogStash::Json.load(query) | ||
end | ||
|
||
def record_last_value(event) | ||
@cursor_tracker.record_last_value(event) if @tracking_field | ||
end | ||
|
@@ -414,8 +440,6 @@ def set_docinfo_fields(hit, event) | |
event.set(@docinfo_target, docinfo_target) | ||
end | ||
|
||
private | ||
|
||
def hosts_default?(hosts) | ||
hosts.nil? || ( hosts.is_a?(Array) && hosts.empty? ) | ||
end | ||
|
@@ -664,18 +688,16 @@ def setup_search_api | |
|
||
end | ||
|
||
def setup_query_executor | ||
@query_executor = case @response_type | ||
when 'hits' | ||
if @resolved_search_api == "search_after" | ||
LogStash::Inputs::Elasticsearch::SearchAfter.new(@client, self) | ||
else | ||
logger.warn("scroll API is no longer recommended for pagination. Consider using search_after instead.") if es_major_version >= 8 | ||
LogStash::Inputs::Elasticsearch::Scroll.new(@client, self) | ||
end | ||
when 'aggregations' | ||
LogStash::Inputs::Elasticsearch::Aggregation.new(@client, self) | ||
end | ||
def create_query_executor | ||
return LogStash::Inputs::Elasticsearch::Esql.new(@client, self) if @query_type == 'esql' | ||
|
||
# DSL query executor | ||
return LogStash::Inputs::Elasticsearch::Aggregation.new(@client, self) if @response_type == 'aggregations' | ||
# response_type is hits, executor can be search_after or scroll type | ||
return LogStash::Inputs::Elasticsearch::SearchAfter.new(@client, self) if @resolved_search_api == "search_after" | ||
|
||
logger.warn("scroll API is no longer recommended for pagination. Consider using search_after instead.") if es_major_version >= 8 | ||
LogStash::Inputs::Elasticsearch::Scroll.new(@client, self) | ||
end | ||
|
||
def setup_cursor_tracker | ||
|
@@ -714,6 +736,26 @@ def get_transport_client_class | |
::Elastic::Transport::Transport::HTTP::Manticore | ||
end | ||
|
||
def validate_ls_version_for_esql_support! | ||
if Gem::Version.create(LOGSTASH_VERSION) < Gem::Version.create(LS_ESQL_SUPPORT_VERSION) | ||
fail("Current version of Logstash does not include Elasticsearch client which supports ES|QL. Please upgrade Logstash to at least #{LS_ESQL_SUPPORT_VERSION}") | ||
end | ||
end | ||
|
||
def validate_esql_query! | ||
fail(LogStash::ConfigurationError, "`query` cannot be empty") if @query.strip.empty? | ||
source_commands = %w[FROM ROW SHOW] | ||
contains_source_command = source_commands.any? { |source_command| @query.strip.start_with?(source_command) } | ||
fail(LogStash::ConfigurationError, "`query` needs to start with any of #{source_commands}") unless contains_source_command | ||
end | ||
|
||
def validate_es_for_esql_support! | ||
return unless @query_type == 'esql' | ||
# make sure connected ES supports ES|QL (8.11+) | ||
es_supports_esql = Gem::Version.create(es_version) >= Gem::Version.create(ES_ESQL_SUPPORT_VERSION) | ||
fail("Connected Elasticsearch #{es_version} version does not supports ES|QL. ES|QL feature requires at least Elasticsearch #{ES_ESQL_SUPPORT_VERSION} version.") unless es_supports_esql | ||
end | ||
|
||
module URIOrEmptyValidator | ||
## | ||
# @override to provide :uri_or_empty validator | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.