1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-12-20 15:21:12 +00:00

Lazy instanciate tags and fields on a Buffer instance

This commit is contained in:
Holger Just 2017-08-03 22:31:48 +02:00
parent 697641635b
commit 4438bf42bc
3 changed files with 26 additions and 20 deletions

View File

@ -55,13 +55,19 @@ module Rackstash
FIELD_VERSION, # the version of the schema. Currently "1" FIELD_VERSION, # the version of the schema. Currently "1"
].freeze ].freeze
# @!attribute [r] fields
# @return [Fields::Hash] the defined fields of the current buffer in a # @return [Fields::Hash] the defined fields of the current buffer in a
# hash-like structure # hash-like structure
attr_reader :fields def fields
@fields ||= Rackstash::Fields::Hash.new(forbidden_keys: FORBIDDEN_FIELDS)
end
# @!attribute [r] tags
# @return [Fields::Tags] a tags list containing the defined tags for the # @return [Fields::Tags] a tags list containing the defined tags for the
# current buffer. It contains frozen strings only. # current buffer. It contains frozen strings only.
attr_reader :tags def tags
@tags ||= Rackstash::Fields::Tags.new
end
# @return [Sink] the log {Sink} where the buffer is eventually flushed to # @return [Sink] the log {Sink} where the buffer is eventually flushed to
attr_reader :sink attr_reader :sink
@ -104,9 +110,9 @@ module Rackstash
# overwrite them. # overwrite them.
# @return [Exception] the passed `exception` # @return [Exception] the passed `exception`
def add_exception(exception, force: true) def add_exception(exception, force: true)
return exception unless force || @fields[FIELD_ERROR].nil? return exception unless force || fields[FIELD_ERROR].nil?
@fields.merge!( fields.merge!(
FIELD_ERROR => exception.class.name, FIELD_ERROR => exception.class.name,
FIELD_ERROR_MESSAGE => exception.message, FIELD_ERROR_MESSAGE => exception.message,
FIELD_ERROR_TRACE => (exception.backtrace || []).join("\n") FIELD_ERROR_TRACE => (exception.backtrace || []).join("\n")
@ -165,9 +171,9 @@ module Rackstash
# #
# @return [self] # @return [self]
def clear def clear
@messages = Concurrent::Array.new @messages = []
@fields = Rackstash::Fields::Hash.new(forbidden_keys: FORBIDDEN_FIELDS) @fields = nil
@tags = Rackstash::Fields::Tags.new @tags = nil
@timestamp = nil @timestamp = nil
self self
@ -209,8 +215,8 @@ module Rackstash
def pending? def pending?
return true if @messages.any? return true if @messages.any?
if allow_empty? if allow_empty?
return true unless @fields.empty? return true unless @fields.nil? || @fields.empty?
return true unless @tags.empty? return true unless @tags.nil? || @tags.empty?
end end
false false
end end
@ -223,7 +229,7 @@ module Rackstash
# as it is set on the buffer. If you pass the optional `scope` value, the # as it is set on the buffer. If you pass the optional `scope` value, the
# Procs will be evaluated in the context of this scope. # Procs will be evaluated in the context of this scope.
# #
# @param tags [Array<#to_s, #call>] Strings to add as tags to the buffer. # @param new_tags [Array<#to_s, #call>] Strings to add as tags to the buffer.
# You can either give (arrays of) strings here or procs which return # You can either give (arrays of) strings here or procs which return
# a string or an array of strings when called. # a string or an array of strings when called.
# @param scope [nil, Object] If anything other then `nil` is given here, we # @param scope [nil, Object] If anything other then `nil` is given here, we
@ -232,8 +238,8 @@ module Rackstash
# in the context where they were created. # in the context where they were created.
# @return [Fields::Tags] the resolved tags which are set on the buffer. # @return [Fields::Tags] the resolved tags which are set on the buffer.
# All strings are frozen. # All strings are frozen.
def tag(*tags, scope: nil) def tag(*new_tags, scope: nil)
@tags.merge!(tags, scope: scope) tags.merge!(new_tags, scope: scope)
end end
# Returns the time of the current buffer as an ISO 8601 formatted string. # Returns the time of the current buffer as an ISO 8601 formatted string.

View File

@ -163,8 +163,8 @@ module Rackstash
end end
# (see Buffer#tag) # (see Buffer#tag)
def tag(*tags, scope: nil) def tag(*new_tags, scope: nil)
buffer.tag(*tags, scope: scope) buffer.tag(*new_tags, scope: scope)
end end
# (see Buffer#tags) # (see Buffer#tags)

View File

@ -265,7 +265,7 @@ describe Rackstash::Buffer do
expect(buffer.pending?).to be true expect(buffer.pending?).to be true
end end
context 'allow_empty == true' do context 'with allow_empty: true' do
before do before do
buffer_options[:allow_empty] = true buffer_options[:allow_empty] = true
expect(buffer.allow_empty?).to be true expect(buffer.allow_empty?).to be true
@ -286,7 +286,7 @@ describe Rackstash::Buffer do
end end
end end
context 'allow_empty == false' do context 'with allow_empty: false' do
before do before do
buffer_options[:allow_empty] = false buffer_options[:allow_empty] = false
expect(buffer.allow_empty?).to be false expect(buffer.allow_empty?).to be false