Class: Peastash

Inherits:
Object
  • Object
show all
Defined in:
lib/peastash.rb,
lib/peastash/version.rb,
lib/peastash/log_device.rb,
lib/peastash/middleware.rb,
lib/peastash/outputs/io.rb,
lib/peastash/rails_ext/watch.rb,
lib/peastash/rails_ext/railtie.rb

Defined Under Namespace

Modules: Outputs, Watch Classes: LogDevice, Middleware, Railtie

Constant Summary collapse

STORE_NAME =
'peastash'
VERSION =
"0.2.9"
@@instance_cache =
ThreadSafe::Cache.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_name) ⇒ Peastash

Returns a new instance of Peastash.



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



43
44
45
# File 'lib/peastash.rb', line 43

def configuration
  @configuration
end

#instance_nameObject (readonly)

Returns the value of attribute instance_name.



42
43
44
# File 'lib/peastash.rb', line 42

def instance_name
  @instance_name
end

Class Method Details

.configure!(conf = {}) ⇒ Object



12
13
14
# File 'lib/peastash.rb', line 12

def configure!(conf = {})
  with_instance.configure!(conf)
end

.safe!Object



32
33
34
# File 'lib/peastash.rb', line 32

def safe!
  @unsafe = false
end

.safe?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/peastash.rb', line 28

def safe?
  !@unsafe
end

.safelyObject



20
21
22
23
24
25
26
# 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!Object



36
37
38
# File 'lib/peastash.rb', line 36

def unsafe!
  @unsafe = true
end

.with_instance(instance_name = :global) ⇒ Object



16
17
18
# File 'lib/peastash.rb', line 16

def with_instance(instance_name = :global)
  @@instance_cache[instance_name] ||= Peastash.new(instance_name)
end

Instance Method Details

#configure!(conf = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
# 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?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/peastash.rb', line 100

def enabled?
  !!configuration[:enabled]
end

#instanceObject



104
105
106
# File 'lib/peastash.rb', line 104

def instance
  @@instance_cache[instance_name]
end

#log(additional_tags = []) {|instance| ... } ⇒ Object

Yields:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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

#storeObject



61
62
63
64
# 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

#tagsObject



93
94
95
96
97
98
# File 'lib/peastash.rb', line 93

def tags
  Peastash.safely do
    configure! unless configured?
    Thread.current[@store_name + ":tags"] ||= []
  end
end