Class: SemanticLogger::Metric::NewRelic

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

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #formatter, #host, #logger, #metrics

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #default_formatter, #flush, #level

Methods inherited from Base

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

Constructor Details

#initialize(prefix: 'Custom', **args, &block) ⇒ NewRelic

Create Appender

Parameters

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

level: [:trace | :debug | :info | :warn | :error | :fatal]
  Override the log level for this appender.
  Default: :error

formatter: [Object|Proc]
  An instance of a class that implements #call, or a Proc to be used to format
  the output from this appender
  Default: Use the built-in formatter (See: #call)

filter: [Regexp|Proc]
  RegExp: Only include log messages where the class name matches the supplied.
  regular expression. All other messages will be ignored.
  Proc: Only include log messages where the supplied Proc returns true
        The Proc must return true or false.


40
41
42
43
# File 'lib/semantic_logger/metric/new_relic.rb', line 40

def initialize(prefix: 'Custom', **args, &block)
  @prefix = prefix
  super(**args, &block)
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



17
18
19
# File 'lib/semantic_logger/metric/new_relic.rb', line 17

def prefix
  @prefix
end

Instance Method Details

#call(log, _logger) ⇒ Object

Returns metric name to use.



46
47
48
49
50
51
# File 'lib/semantic_logger/metric/new_relic.rb', line 46

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

#log(log) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/semantic_logger/metric/new_relic.rb', line 53

def log(log)
  name = formatter.call(log, self)
  if (duration = log.duration)
    # Convert duration to seconds
    ::NewRelic::Agent.record_metric(name, duration / 1000.0)
  else
    ::NewRelic::Agent.increment_metric(name, log.metric_amount || 1)
  end
  true
end

#should_log?(log) ⇒ Boolean

Only forward log entries that contain metrics.

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/semantic_logger/metric/new_relic.rb', line 65

def should_log?(log)
  # Does not support metrics with dimensions.
  log.metric && !log.dimensions && meets_log_level?(log) && !filtered?(log)
end