Class: NexusSemanticLogger::LoggerMetricsSubscriber

Inherits:
SemanticLogger::Subscriber
  • Object
show all
Defined in:
lib/nexus_semantic_logger/logger_metrics_subscriber.rb

Overview

Instance Method Summary collapse

Instance Method Details

#call(log) ⇒ Object



9
10
11
# File 'lib/nexus_semantic_logger/logger_metrics_subscriber.rb', line 9

def call(log)
  log(log) if should_log?(log)
end

#log(log) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nexus_semantic_logger/logger_metrics_subscriber.rb', line 13

def log(log)
  metric = log.metric
  tags = log.payload.nil? ? nil : []
  log.payload&.each_pair { |key, value| tags << "#{key}:#{value}" }
  if (duration = log.duration)
    NexusSemanticLogger.metrics.timing(metric, duration, tags: tags)
  else
    amount = (log.metric_amount || 1).round
    if amount.negative?
      NexusSemanticLogger.metrics.decrement(metric, tags: tags)
    else
      NexusSemanticLogger.metrics.increment(metric, tags: tags)
    end
  end
end

#should_log?(log) ⇒ Boolean

Only forward log entries that contain metrics.

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/nexus_semantic_logger/logger_metrics_subscriber.rb', line 30

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