Class: ElasticsearchRecord::Instrumentation::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/elasticsearch_record/instrumentation/log_subscriber.rb

Overview

attach to ElasticsearchRecord related events

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =
%w[SCHEMA EXPLAIN]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reset_runtimeObject



18
19
20
21
# File 'lib/elasticsearch_record/instrumentation/log_subscriber.rb', line 18

def self.reset_runtime
  rt, self.runtime = runtime, 0
  rt
end

.runtimeObject



14
15
16
# File 'lib/elasticsearch_record/instrumentation/log_subscriber.rb', line 14

def self.runtime
  Thread.current["elasticsearch_record_runtime"] ||= 0
end

.runtime=(value) ⇒ Object



10
11
12
# File 'lib/elasticsearch_record/instrumentation/log_subscriber.rb', line 10

def self.runtime=(value)
  Thread.current["elasticsearch_record_runtime"] = value
end

Instance Method Details

#query(event) ⇒ Object

Intercept query.elasticsearch events, and display them in the Rails log



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/elasticsearch_record/instrumentation/log_subscriber.rb', line 24

def query(event)
  self.class.runtime += event.duration

  return unless logger.debug?

  payload = event.payload
  return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

  # build name from several payload data
  name = if payload[:async]
           "ASYNC #{payload[:name]} (#{payload[:lock_wait].round(1)}ms) (execution time #{event.duration.round(1)}ms)"
         else
           "#{payload[:name]} (#{event.duration.round(1)}ms)"
         end
  name = "CACHE #{name}" if payload[:cached]

  # nice feature: displays the REAL query-time (_qt)
  name = "#{name} (took: #{payload[:arguments][:_qt].round(1)}ms)" if payload[:arguments][:_qt]

  # build query
  query = payload[:arguments].except(:_qt).inspect.gsub(/:(\w+)=>/, '\1: ').presence || '-'

  # final coloring
  name  = color(name, name_color(payload[:name]), true)
  query = color(query, gate_color(payload[:gate]), true) if colorize_logging

  debug "  #{name} #{query}"
end