Include monotonically increasing value in logstash field?

后端 未结 3 581
面向向阳花
面向向阳花 2021-01-15 05:01

I know there\'s no built in \"line count\" functionality while processing files through logstash (for various, understandable and documented reasons). But - there should be

3条回答
  •  感动是毒
    2021-01-15 05:41

    here's nothing built into logstash to do it.

    You can build a filter to do it pretty easily

    Just drop something like this into lib/logstash/filters/seq.rb

    # encoding: utf-8
    require "logstash/filters/base"
    require "logstash/namespace"
    require "set"
    #
    # This filter will adds a sequence number to a log entry
    #
    # The config looks like this:
    #
    #     filter {
    #       seq {
    #         field => "seq"
    #       }
    #     }
    #
    # The `field` is the field you want added to the event.
    class LogStash::Filters::Seq < LogStash::Filters::Base
    
      config_name "seq"
      milestone 1
    
      config :field, :validate => :string, :required => false, :default => "seq"
    
      public
      def register
        # Nothing
      end # def register
    
      public
      def initialize(config = {})
        super
    
        @threadsafe = false
    
        # This filter needs to keep state.
        @seq=1
      end # def initialize
    
      public
      def filter(event)
        return unless filter?(event)
        event[@field] = @seq
        @seq = @seq + 1
        filter_matched(event)
      end # def filter
    end # class LogStash::Filters::Seq
    

    This will start at 1 every time Logstash is restarted, but for most situations, this would be ok. If you need something that is persistent across restarts, you need to do a bit more work to persist it somewhere

提交回复
热议问题