Class: DatastaxRails::Instrumentation::LogSubscriber

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

Overview

A log subscriber to attach to Datastax related events

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ LogSubscriber


8
9
10
11
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 8

def initialize(*args)
  super
  @odd = false
end

Class Method Details

.cql_runtimeObject


25
26
27
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 25

def self.cql_runtime
  Thread.current['datastax_cql_runtime'] ||= 0
end

.cql_runtime=(value) ⇒ Object


21
22
23
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 21

def self.cql_runtime=(value)
  Thread.current['datastax_cql_runtime'] = value
end

.reset_cql_runtimeObject


34
35
36
37
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 34

def self.reset_cql_runtime
  rt, self.cql_runtime = cql_runtime, 0
  rt
end

.reset_solr_runtimeObject


29
30
31
32
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 29

def self.reset_solr_runtime
  rt, self.solr_runtime = solr_runtime, 0
  rt
end

.solr_runtimeObject


17
18
19
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 17

def self.solr_runtime
  Thread.current['datastax_solr_runtime'] ||= 0
end

.solr_runtime=(value) ⇒ Object


13
14
15
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 13

def self.solr_runtime=(value)
  Thread.current['datastax_solr_runtime'] = value
end

Instance Method Details

#cql(event) ⇒ Object

Intercept `cql.datastax_rails` events, and display them in the Rails log


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 60

def cql(event)
  self.class.cql_runtime += event.duration
  return unless logger.debug? && DatastaxRails::Base.log_cql_queries

  payload = event.payload

  name    = "#{payload[:klass]} #{payload[:name]} (#{event.duration.round(1)}ms)"
  cql     = payload[:cql]
  binds   = nil
  results = payload[:result_count]

  unless (payload[:binds] || []).empty?
    binds = ' ' + payload[:binds].inspect
  end

  if odd?
    name = color(name, CYAN, true)
    cql = color(cql, nil, true)
  else
    name = color(name, MAGENTA, true)
  end

  debug "  #{name} #{cql}#{binds} - #{results} results"
end

#odd?Boolean


85
86
87
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 85

def odd?
  @odd = !@odd
end

#solr(event) ⇒ Object

Intercept `solr.datastax_rails` events, and display them in the Rails log


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/datastax_rails/instrumentation/log_subscriber.rb', line 40

def solr(event)
  self.class.solr_runtime += event.duration
  return unless logger.debug? && DatastaxRails::Base.log_solr_queries

  payload = event.payload

  name    = "#{payload[:klass]} #{payload[:name]} (#{event.duration.round(1)}ms)"
  search  = payload[:search].inspect.gsub(/:(\w+)=>/, '\1: ')

  if odd?
    name = color(name, CYAN, true)
    search = color(search, nil, true)
  else
    name = color(name, MAGENTA, true)
  end

  debug "  #{name} #{search}"
end