Class: FFWD::MetricEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/ffwd/metric_emitter.rb

Overview

Used to emit metrics to an ‘output’ channel

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, host, tags, attributes) ⇒ MetricEmitter

Returns a new instance of MetricEmitter.



32
33
34
35
36
37
# File 'lib/ffwd/metric_emitter.rb', line 32

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

Class Method Details

.build(output, base, opts) ⇒ Object



25
26
27
28
29
30
# File 'lib/ffwd/metric_emitter.rb', line 25

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

Instance Method Details

#emit(d) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ffwd/metric_emitter.rb', line 39

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

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