Sha256: cdd7f21a48a14c71820bf310fe9ea3da759191d47603531fbe865ee0e852fcab

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Elasticity
  class InstrumentedClient
    INDICES_METHODS = %w(exists create delete get_settings get_mapping flush refresh get_alias get_aliases put_alias delete_alias exists_alias update_aliases)
    INDEX_METHODS   = %w(index delete get mget search count msearch scroll delete_by_query bulk)

    def initialize(client)
      @client = client
    end

    def versions
      (@client.cluster.stats["nodes"] && @client.cluster.stats["nodes"]["versions"]) || []
    end

    # Generate wrapper methods for @client.indices
    INDICES_METHODS.each do |method_name|
      full_name = "index_#{method_name}"

      define_method(full_name) do |*args, &block|
        instrument(full_name, args) do
          @client.indices.public_send(method_name, *args, &block)
        end
      end
    end

    # Generate wrapper methods for @client
    INDEX_METHODS.each do |method_name|
      define_method(method_name) do |*args, &block|
        instrument(method_name, args) do
          @client.public_send(method_name, *args, &block)
        end
      end
    end

    private

    def instrument(name, args)
      ActiveSupport::Notifications.instrument("#{name}.elasticity", args: args, backtrace: caller(1)) do
        yield
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
es-elasticity-1.0.0 lib/elasticity/instrumented_client.rb
es-elasticity-1.0.0.jhumphreys lib/elasticity/instrumented_client.rb