class SemanticLogger::Metric::NewRelic

Attributes

prefix[RW]

Public Class Methods

new(prefix: "Custom", **args, &block) click to toggle source

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.
Calls superclass method SemanticLogger::Subscriber::new
# File lib/semantic_logger/metric/new_relic.rb, line 40
def initialize(prefix: "Custom", **args, &block)
  @prefix = prefix
  super(**args, &block)
end

Public Instance Methods

call(log, _logger) click to toggle source

Returns metric name to use.

# 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) click to toggle source
# 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) click to toggle source

Only forward log entries that contain metrics.

# 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