Method: Fluent::Plugin::Output#metadata

Defined in:
lib/fluent/plugin/output.rb

#metadata(tag, time, record) ⇒ Object

TODO: optimize this code

Raises:

  • (ArgumentError)


909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
# File 'lib/fluent/plugin/output.rb', line 909

def (tag, time, record)
  # this arguments are ordered in output plugin's rule
  # Metadata 's argument order is different from this one (timekey, tag, variables)

  raise ArgumentError, "tag must be a String: #{tag.class}" unless tag.nil? || tag.is_a?(String)
  raise ArgumentError, "time must be a Fluent::EventTime (or Integer): #{time.class}" unless time.nil? || time.is_a?(Fluent::EventTime) || time.is_a?(Integer)
  raise ArgumentError, "record must be a Hash: #{record.class}" unless record.nil? || record.is_a?(Hash)

  if @chunk_keys.nil? && @chunk_key_time.nil? && @chunk_key_tag.nil?
    # for tests
    return Struct.new(:timekey, :tag, :variables).new
  end

  # timekey is int from epoch, and `timekey - timekey % 60` is assumed to mach with 0s of each minutes.
  # it's wrong if timezone is configured as one which supports leap second, but it's very rare and
  # we can ignore it (especially in production systems).
  if @chunk_keys.empty?
    if !@chunk_key_time && !@chunk_key_tag
      @buffer.()
    elsif @chunk_key_time && @chunk_key_tag
      timekey = calculate_timekey(time)
      @buffer.(timekey: timekey, tag: tag)
    elsif @chunk_key_time
      timekey = calculate_timekey(time)
      @buffer.(timekey: timekey)
    else
      @buffer.(tag: tag)
    end
  else
    timekey = if @chunk_key_time
                calculate_timekey(time)
              else
                nil
              end
    pairs = Hash[@chunk_key_accessors.map { |k, a| [k, a.call(record)] }]
    @buffer.(timekey: timekey, tag: (@chunk_key_tag ? tag : nil), variables: pairs)
  end
end