Skip to content
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

Use integration metadata to create ES actions (part 2) #1158

Conversation

andsel
Copy link
Contributor

@andsel andsel commented Nov 3, 2023

Release notes

Added support for propagating event processing metadata when this output is downstream of an Elastic Integration Filter and configured without explicit version, version_type, or routing directives

What does this PR do?

Change the creation of actions that are passed down to Elasticsearch to use also the metadata fields set by an integration.
The interested fields are version, version_type and routing, the field values are taken verbatim without placeholders resolution.
The version, version_type and routing that are configured in the plugin settings have precedence on the integration ones because manifest an explicit choice made by the user.

Why is it important/What is the impact to the user?

This PR complete the fix related to interoperability issue with Agent's integrations (started with #1155), where some metadata valued by integration has to be used down to Elasticsearch.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files (and/or docker env variables)
  • I have added tests that prove my fix is effective or that my feature works

Author's Checklist

  • use the plugin with an integration that configures the document id, and in case of same id no new documents are indexed.

How to test this PR locally

Please refer to the same flow used in #1155

Related issues

@andsel andsel self-assigned this Nov 3, 2023
@andsel andsel force-pushed the feature/use_integration_metadata_to_create_es_actions_part2 branch 3 times, most recently from 8714d88 to 64a6ef5 Compare November 8, 2023 11:26
@andsel andsel force-pushed the feature/use_integration_metadata_to_create_es_actions_part2 branch from 64a6ef5 to ed367e4 Compare November 10, 2023 09:33
@andsel andsel changed the title Feature/use integration metadata to create es actions part2 Use integration metadata to create ES actions (part 2) Nov 10, 2023
@andsel andsel marked this pull request as ready for review November 10, 2023 09:48
@andsel andsel requested a review from yaauie November 10, 2023 09:48
spec/unit/outputs/elasticsearch_spec.rb Outdated Show resolved Hide resolved
spec/unit/outputs/elasticsearch_spec.rb Outdated Show resolved Hide resolved
@@ -557,9 +554,30 @@ def common_event_params(event)
# }
params[:pipeline] = target_pipeline unless (target_pipeline.nil? || target_pipeline.empty?)

params[:version] = resolve_version(event, event_version)
params[:version_type] = resolve_version_type(event, event_version_type)

params
Copy link
Contributor

Choose a reason for hiding this comment

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

The issues raised elsewhere could likely be resolved by leaning on Hash#compact, which is available since Ruby 2.4 (and therefore on all Logstash 7+):

Suggested change
params
params.compact

spec/unit/outputs/elasticsearch_spec.rb Show resolved Hide resolved
@@ -557,9 +554,30 @@ def common_event_params(event)
# }
params[:pipeline] = target_pipeline unless (target_pipeline.nil? || target_pipeline.empty?)

params[:version] = resolve_version(event, event_version)
params[:version_type] = resolve_version_type(event, event_version_type)
Copy link
Contributor

Choose a reason for hiding this comment

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

alternate approaches to Hash#compact to avoid storing nil values:

  1. use a local variable:

    Suggested change
    params[:version_type] = resolve_version_type(event, event_version_type)
    resolved_version_type = resolve_version_type(event, event_version_type)
    params[:version_type] = resolved_version_type unless resolved_version_type.nil?
  2. rely on safe-descent &. with Object#tap:

    Suggested change
    params[:version_type] = resolve_version_type(event, event_version_type)
    resolve_version_type(event, event_version_type)&.tap { |resolved| params.store(:version_type, resolved) }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank's @yaauie to have provided also this alternative, I've opted for the one-liner compact

Copy link
Contributor Author

Choose a reason for hiding this comment

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

routing => nil is expected in some tests:
https://app.travis-ci.com/github/logstash-plugins/logstash-output-elasticsearch/jobs/613384063#L4748

This comes from the fixture

ESHelper.action_for_version(["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17", :_type=> doc_type }, event1.to_hash])
else
ESHelper.action_for_version(["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17" }, event1.to_hash])
end
end
let(:event2) { LogStash::Event.new("geoip" => { "location" => [ 0.0, 0.0] }, "@timestamp" => "2014-11-17T20:37:17.223Z", "@metadata" => {"retry_count" => 0}) }
let(:action2) do
if ESHelper.es_version_satisfies?("< 7")
ESHelper.action_for_version(["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17", :_type=> doc_type }, event2.to_hash])
else
ESHelper.action_for_version(["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17" }, event2.to_hash])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted the behaviour only for routing with commit e91210c

andsel and others added 2 commits November 16, 2023 09:12
Adding `version => nil` is a change in behaviour because, actually it's never added, so this commit respect the existing logic, do not add event's keys that are empty

Co-authored-by: Ry Biesemeyer <[email protected]>
… commit rollback the changes to satisfy that condition
@andsel andsel requested a review from yaauie November 16, 2023 13:38
Copy link
Contributor

@yaauie yaauie left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏼

@andsel andsel merged commit cfd82fa into logstash-plugins:main Nov 16, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use integration's metadata fields (_routing, _version, _version_type) when present
3 participants