Class: SemanticLogger::Metric::NewRelic

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

Instance Attribute Summary collapse

Attributes inherited from Subscriber

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

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_by_default?, #close, #console_output?, #console_stream, #default_formatter, #flush, #level

Constructor Details

#initialize(prefix: "Custom", **args) ⇒ 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.


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

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

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Instance Method Details

#call(log, _logger) ⇒ Object

Returns metric name to use.



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

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

#log(log) ⇒ Object



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

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)


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

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