Class: ActiveMetrics::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/active_metrics/collector.rb

Constant Summary collapse

PREFIX =
'com.active_metrics'.freeze

Class Method Summary collapse

Class Method Details

.attachObject

Start subscribing to the metrics-related events.



17
18
19
20
21
# File 'lib/active_metrics/collector.rb', line 17

def attach
  ActiveSupport::Notifications.subscribe(/#{PREFIX}/i) do |name, _, _, _, data|
    deliver(name, data)
  end
end

.deliver(name, data = {}) ⇒ Object

Deliver a metric to Librato

According to the Heroku DevCenter there is already a tight integration between Heroku logs and Librato so simply using ‘$stdout.puts` will be enough, as long as a specific format is used.

Parameters:

  • name (String)

    The name of the event being measured

  • data (Hash) (defaults to: {})

    a Hash with type of metric and the value to be recorded



31
32
33
34
35
36
37
# File 'lib/active_metrics/collector.rb', line 31

def deliver(name, data = {})
  key    = name.gsub(PREFIX, '')
  value  = data[:value]
  metric = data[:metric]

  $stdout.puts "#{metric}##{key}=#{value}" unless silent?
end

.record(event, payload = {}) ⇒ Object

Record an event

Parameters:

  • event (String)

    The name of the event

  • payload (Hash) (defaults to: {})

    A hash that contains the event-related data.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_metrics/collector.rb', line 43

def record(event, payload = {})
  # Add a prefix to all events so things broadcasted using this method
  # will not get picked up by possibly other `ActiveSupport::Notifications`
  # subscribers.
  name = "#{PREFIX}#{event}"

  if block_given?
    ActiveSupport::Notifications.instrument(name, payload) { yield }
  else
    ActiveSupport::Notifications.instrument(name, payload)
  end
end

.silent?Boolean

Should the metrics be silent?

Useful especially in QA or development environments, where you’ll might not want your logs to be filled with various metrics.

Returns:

  • (Boolean)


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

def silent?
  [1, '1', 'true'].include?(ENV['SILENT_METRICS'])
end