Class: SemanticLogger::Metric::NewRelic

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

Constant Summary

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Subscriber

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

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #close, #console_output?, #console_stream, #default_formatter, #fails_any_batch?, #flush, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #wrote_nothing?

Constructor Details

#initialize(prefix: "Custom", **args) ⇒ NewRelic

Create the New Relic metrics subscriber

Parameters prefix: [String] Prefix to add, with a /, to every metric name that does not already start with it and a /, before forwarding to NewRelic, which charts custom metrics only under Custom/. So 'Customers/signup' is recorded as 'Custom/Customers/signup', and 'Custom/Orders/placed' as it is. Another prefix should start with Custom/ too, such as 'Custom/MyApp'. A trailing / is left out, and an empty prefix adds nothing. Only the default formatter, #call, adds it. Default: 'Custom'

level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this subscriber.
Default: none (:trace), so it records every metric entry that a logger emits; each
logger filters by its own level.

formatter: [Object|Proc]
An instance of a class that implements #call, or a Proc, that returns the name to
record the metric under.
Default: #call, which adds the prefix to the name of the entry's metric.

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.


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

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

Instance Attribute Details

#prefix ⇒ Object

Returns the value of attribute prefix.



20
21
22
# File 'lib/semantic_logger/metric/new_relic.rb', line 20

def prefix
  @prefix
end

Instance Method Details

#call(log, _logger) ⇒ Object

Returns metric name to use.



62
63
64
65
66
67
# File 'lib/semantic_logger/metric/new_relic.rb', line 62

def call(log, _logger)
  metric = log.metric
  return metric if metric.start_with?(@prefix_with_slash)

  "#{@prefix_with_slash}#{metric}"
end

#log(log) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/semantic_logger/metric/new_relic.rb', line 69

def log(log)
  name = format_log(log)
  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)


81
82
83
84
# File 'lib/semantic_logger/metric/new_relic.rb', line 81

def should_log?(log)
  # Records a metric that has dimensions under its name alone, since the agent's metric API has none.
  log.metric && meets_log_level?(log) && !filtered?(log)
end