Sha256: 24ee778d492985e5cf107a664013bb8212a71209dcc0989f191ee1347d101876

Contents?: true

Size: 1.2 KB

Versions: 21

Compression:

Stored size: 1.2 KB

Contents

module Elasticsearch
  module Rails
    module Instrumentation

      # A log subscriber to attach to Elasticsearch related events
      #
      # @see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb
      #
      class LogSubscriber < ActiveSupport::LogSubscriber
        def self.runtime=(value)
          Thread.current["elasticsearch_runtime"] = value
        end

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

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

        # Intercept `search.elasticsearch` events, and display them in the Rails log
        #
        def search(event)
          self.class.runtime += event.duration
          return unless logger.debug?

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

          debug %Q|  #{color(name, GREEN, true)} #{colorize_logging ? "\e[2m#{search}\e[0m" : search}|
        end
      end

    end
  end
end

Elasticsearch::Rails::Instrumentation::LogSubscriber.attach_to :elasticsearch

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
elasticsearch-rails-6.1.2 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-6.1.1 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-6.1.0 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-6.0.0 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-6.0.0.pre lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-5.1.0 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-5.0.2 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-2.0.1 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-5.0.1 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-2.0.0 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-5.0.0 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.9 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.8 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.7 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.6 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.5 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.4 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.3 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.2 lib/elasticsearch/rails/instrumentation/log_subscriber.rb
elasticsearch-rails-0.1.1 lib/elasticsearch/rails/instrumentation/log_subscriber.rb