Giter Site home page Giter Site logo

Comments (6)

avleen avatar avleen commented on June 15, 2024

This patch removes the check on if kv exists, it seems to fix this issues:

--- lib/logstash/filters/kv.rb     2014-04-29 21:37:18.335010728 +0000
+++ lib/logstash/filters/kv.rb  2014-04-29 21:37:20.230012427 +0000
@@ -182,23 +182,22 @@
     # Add default key-values for missing keys
     kv = @default_keys.merge(kv)

-    # If we have any keys, create/append the hash
-    if kv.length > 0
-      if @target.nil?
-        # Default is to write to the root of the event.
-        dest = event.to_hash
+    # If we have any keys, create/append the hash.
+    # If we don't, at least set @target as expected.
+    if @target.nil?
+      # Default is to write to the root of the event.
+      dest = event.to_hash
+    else
+      if !event[@target].is_a?(Hash)
+        @logger.debug("Overwriting existing target field", :target => @target)
+        dest = event[@target] = {}
       else
-        if !event[@target].is_a?(Hash)
-          @logger.debug("Overwriting existing target field", :target => @target)
-          dest = event[@target] = {}
-        else
-          dest = event[@target]
-        end
+        dest = event[@target]
       end
-
-      dest.merge!(kv)
-      filter_matched(event)
     end
+
+    dest.merge!(kv)
+    filter_matched(event)
   end # def filter

   private

from logstash.

colinsurprenant avatar colinsurprenant commented on June 15, 2024

Thanks for reporting this and you solution proposal. We will review this. Is it possible for you to submit a pull request with your changes? Also it would be great if you could add specs for this in spec/filters/kv.rb. Let us know if you need help with writing specs.

from logstash.

jordansissel avatar jordansissel commented on June 15, 2024

@avleen If I'm reading your report correctly, you're saying that if you have kv { source => "foo" target => "foo" } and an event { "foo": "bar" } (no kv to parse!) that the result should be { "foo": { } } ?

from logstash.

jordansissel avatar jordansissel commented on June 15, 2024

Example -

% bin/logstash -e 'filter { kv { source => "message" target => "message" } }'
hello=world
{
  "message"    => {
    "hello" => "world"
  },
  ...
}

fancy pants
{
  "message"    => "fancy pants",
  ...
}

In the 2nd case, no kv was matched and left the original source alone (as a string). This would cause Elasticsearch to be upset if it the field were mapped as an object, right?

I'm not sure what the best default behavior would be, here.

Options when @source == @target:

  • Promote @target to be empty hash {} as proposed by @avleen (I think?). This would cause data loss because we obliterate the old value.
  • Promote @target to be a hash of { "_original" => <original field value> } so it's still a hash but we don't lose data.

Thoughts?

from logstash.

avleen avatar avleen commented on June 15, 2024

That's correct. This comes down to object mappings and potential loss of
data.

@target should always become a hash. Either it ends up empty, or it has
some data. But it's always a hash. If it's empty, just drop it at the end
of the run.

I think the user should get a choice in whether @target is overwritten, or
if the original is preserved. Different people may expect different things.

On Fri Jan 23 2015 at 2:28:29 PM Jordan Sissel [email protected]
wrote:

Example -

% bin/logstash -e 'filter { kv { source => "message" target => "message" } }'
hello=world
{
"message" => {
"hello" => "world"
},
...
}

fancy pants
{
"message" => "fancy pants",
...
}

In the 2nd case, no kv was matched and left the original source alone (as
a string). This would cause Elasticsearch to be upset if it the field were
mapped as an object, right?

I'm not sure what the best default behavior would be, here.

Options when @source == @target:

  • Promote @target to be empty hash {} as proposed by @avleen
    https://github.com/avleen (I think?). This would cause data loss
    because we obliterate the old value.
  • Promote @target to be a hash of { "_original" => } so it's still a hash but we don't lose data.

Thoughts?

Reply to this email directly or view it on GitHub
#1315 (comment)
.

from logstash.

jordansissel avatar jordansissel commented on June 15, 2024

For Logstash 1.5.0, we've moved all plugins to individual repositories, so I have moved this issue to logstash-plugins/logstash-filter-kv#11. Let's continue the discussion there! :)

from logstash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.