Class: SemanticLogger::Metrics::NewRelic

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/metrics/new_relic.rb

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #formatter, #host

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #default_formatter, #flush, #level

Methods inherited from Base

#fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload

Constructor Details

#initialize(options = {}) ⇒ NewRelic

Parameters:

:prefix [String]
  Prefix to add to every metric before forwarding to NewRelic
  Default: 'Custom'

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/semantic_logger/metrics/new_relic.rb', line 10

def initialize(options = {})
  options = options.dup
  @prefix = options.delete(:prefix) || 'Custom'
  raise(ArgumentError, "Unknown options: #{options.inspect}") if options.size > 0
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



4
5
6
# File 'lib/semantic_logger/metrics/new_relic.rb', line 4

def prefix
  @prefix
end

Instance Method Details

#call(log) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/semantic_logger/metrics/new_relic.rb', line 16

def call(log)
  metric = log.metric
  # Add prefix for NewRelic
  metric = "#{prefix}/#{metric}" unless metric.start_with?(prefix)

  if duration = log.duration
    # Convert duration to seconds
    ::NewRelic::Agent.record_metric(metric, duration / 1000.0)
  else
    ::NewRelic::Agent.increment_metric(metric, log.metric_amount || 1)
  end
end