Class: FFWD::EventEmitter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/ffwd/event_emitter.rb

Overview

Used to emit events to an ‘output’ channel

Can take two parts of a configuration ‘base’ and ‘opts’ to decide which metadata emitted events should be decorated with.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log

Constructor Details

#initialize(output, host, ttl, tags, attributes) ⇒ EventEmitter

Returns a new instance of EventEmitter.



37
38
39
40
41
42
43
# File 'lib/ffwd/event_emitter.rb', line 37

def initialize output, host, ttl, tags, attributes
  @output = output
  @host = host
  @ttl = ttl
  @tags = tags
  @attributes = attributes
end

Class Method Details

.build(output, base, opts) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/ffwd/event_emitter.rb', line 28

def self.build output, base, opts
  output = output
  host = opts[:host] || base[:host] || FFWD.current_host
  ttl = opts[:ttl] || base[:ttl]
  tags = FFWD.merge_sets base[:tags], opts[:tags]
  attributes = FFWD.merge_hashes base[:attributes], opts[:attributes]
  new output, host, ttl, tags, attributes
end

Instance Method Details

#emit(d) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ffwd/event_emitter.rb', line 45

def emit d
  d[:time] ||= Time.now
  d[:host] ||= @host if @host
  d[:ttl] ||= @ttl if @ttl
  d[:value] = nil if (v = d[:value] and v.is_a?(Float) and v.nan?)
  d[:fixed_tags] = @tags
  d[:fixed_attr] = @attributes

  @output.event Event.make(d)
rescue => e
  log.error "Failed to emit event", e
end