lib/hallmonitor/outputters/new_relic.rb in hallmonitor-1.1.0 vs lib/hallmonitor/outputters/new_relic.rb in hallmonitor-2.0.0

- old
+ new

@@ -3,28 +3,61 @@ # Outputs events to NewRelic using their custom metrics API class NewRelic < Outputter # Initializes a new instance # @raise String if {NewRelic::Agent} isn't defined (Library isn't loaded) # @param prefix [String] String to prefix all metrics with - def initialize(prefix='') - raise "In order to use NewRelic, new_relic gem must be installed" unless defined?(::NewRelic::Agent) + def initialize(prefix = '') + unless defined?(::NewRelic::Agent) + fail 'In order to use NewRelic, new_relic gem must be installed' + end super(prefix) end def process(event) - if(event.respond_to?(:duration)) - ::NewRelic::Agent.record_metric(new_relic_name(event), event.duration) - elsif(event.respond_to?(:value)) - ::NewRelic::Agent.record_metric(new_relic_name(event), event.value) + if event.is_a?(Hallmonitor::TimedEvent) + process_timed_event(event) + elsif event.is_a?(Hallmonitor::GaugeEvent) + process_gauge_event(event) else - ::NewRelic::Agent.increment_metric(new_relic_name(event), event.count) + process_event(event) end end private + + def process_timed_event(event) + if event.duration.is_a?(Hash) + event.duration.each do |name, value| + ::NewRelic::Agent.record_metric(new_relic_name("#{event.name}.#{name}"), value) + end + else + ::NewRelic::Agent.record_metric(new_relic_name(event.name), event.duration) + end + end + + def process_gauge_event(event) + if event.value.is_a?(Hash) + event.value.each do |name, value| + ::NewRelic::Agent.record_metric(new_relic_name("#{event.name}.#{name}"), value) + end + else + ::NewRelic::Agent.record_metric(new_relic_name(event.name), event.value) + end + end + + def process_event(event) + if event.count.is_a?(Hash) + event.count.each do |name, value| + ::NewRelic::Agent.increment_metric(new_relic_name("#{event.name}.#{name}"), value) + end + else + ::NewRelic::Agent.increment_metric(new_relic_name(event.name), event.count) + end + end + # Formats the event name into the naming scheme that NewRelic expects - def new_relic_name(event) - "Custom/#{event.name.gsub('.','/')}" + def new_relic_name(name) + "Custom/#{name.tr('.', '/')}" end end end end