class Peastash

Constants

STORE_NAME
VERSION

Attributes

configuration[RW]
instance_name[R]

Public Class Methods

configure!(conf = {}) click to toggle source
# File lib/peastash.rb, line 12
def configure!(conf = {})
  with_instance.configure!(conf)
end
new(instance_name) click to toggle source
# File lib/peastash.rb, line 47
def initialize(instance_name)
  @instance_name = instance_name

  @configuration = {
    :source => STORE_NAME,
    :tags => [],
    :output => Outputs::IO.new(Outputs::IO::default_io),
    :store_name => STORE_NAME,
    :dump_if_empty => true
  }

  configure!(@@instance_cache[:global].configuration || {}) if @@instance_cache[:global]
end
safe!() click to toggle source
# File lib/peastash.rb, line 32
def safe!
  @unsafe = false
end
safe?() click to toggle source
# File lib/peastash.rb, line 28
def safe?
  !@unsafe
end
safely() { || ... } click to toggle source
# File lib/peastash.rb, line 20
def safely
  yield
rescue StandardError => e
  STDERR.puts e.inspect
  STDERR.puts e.backtrace
  raise e unless safe?
end
unsafe!() click to toggle source
# File lib/peastash.rb, line 36
def unsafe!
  @unsafe = true
end
with_instance(instance_name = :global) click to toggle source
# File lib/peastash.rb, line 16
def with_instance(instance_name = :global)
  @@instance_cache[instance_name] ||= Peastash.new(instance_name)
end

Public Instance Methods

configure!(conf = {}) click to toggle source
# File lib/peastash.rb, line 66
def configure!(conf = {})
  self.configuration.merge!(conf)
  @source = configuration[:source]
  @base_tags = configuration[:tags].flatten
  @output = configuration[:output]
  @store_name = configuration[:store_name]
  @dump_if_empty = configuration[:dump_if_empty]
  @configured = true
end
enabled?() click to toggle source
# File lib/peastash.rb, line 100
def enabled?
  !!configuration[:enabled]
end
instance() click to toggle source
# File lib/peastash.rb, line 104
def instance
  @@instance_cache[instance_name]
end
log(additional_tags = []) { |instance| ... } click to toggle source
# File lib/peastash.rb, line 76
def log(additional_tags = [])
  Peastash.safely do
    configure! unless configured?
    tags.replace(additional_tags)
    store.clear
  end

  yield(instance)

  Peastash.safely do
    if enabled? && (!store.empty? || dump_if_empty?)
      event = build_event(@source, tags)
      @output.dump(event)
    end
  end
end
store() click to toggle source
# File lib/peastash.rb, line 61
def store
  Thread.current[instance_name] ||= Hash.new
  Thread.current[instance_name][@store_name] ||= Hash.new { |hash, key| hash[key] = {} }
end
tags() click to toggle source
# File lib/peastash.rb, line 93
def tags
  Peastash.safely do
    configure! unless configured?
    Thread.current[@store_name + ":tags"] ||= []
  end
end

Private Instance Methods

build_event(source, tags) click to toggle source
# File lib/peastash.rb, line 118
def build_event(source, tags)
  LogStash::Event.new({
    '@source' => source,
    '@fields' => store,
    '@tags' => @base_tags + tags,
    '@pid' => Process.pid
  })
end
configured?() click to toggle source
# File lib/peastash.rb, line 110
def configured?
  @configured
end
dump_if_empty?() click to toggle source
# File lib/peastash.rb, line 114
def dump_if_empty?
  @dump_if_empty
end