Module: Elastomer::Notifications

Included in:
Client
Defined in:
lib/elastomer/notifications.rb

Overview

So you want to get notifications from your ElasticSearch client? Well, you’ve come to the right place!

require 'elastomer/notifications'

Requiring this module will add ActiveSupport notifications to all ElasticSearch requests. To subscribe to those requests …

ActiveSupport::Notifications.subscribe('request.client.elastomer') do |name, start_time, end_time, _, payload|
  duration = end_time - start_time
  $stderr.puts '[%s] %s %s (%.3f)' % [payload[:status], payload[:index], payload[:action], duration]
end

The payload contains the following bits of information:

  • :index - index name (if any)

  • :type - documeht type (if any)

  • :action - the action being performed

  • :url - request URL

  • :method - request method (:head, :get, :put, :post, :delete)

  • :status - response status code

If you want to use your own notifications service then you will need to let Elastomer know by setting the ‘service` here in the Notifications module. The service should adhere to the ActiveSupport::Notifications specification.

Elastomer::Notifications.service = your_own_service

Constant Summary collapse

NAME =

The name to subscribe to for notifications

'request.client.elastomer'.freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.serviceObject

Returns the value of attribute service.



39
40
41
# File 'lib/elastomer/notifications.rb', line 39

def service
  @service
end

Instance Method Details

#instrument(path, body, params) ⇒ Object

Internal: Execute the given block and provide instrumentaiton info to subscribers. The name we use for subscriptions is ‘request.client.elastomer` and a supplemental payload is provided with more information about the specific ElasticSearch request.

path - The full request path as a String body - The request body as a String or ‘nil` params - The request params Hash block - The block that will be instrumented

Returns the response from the block



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/elastomer/notifications.rb', line 56

def instrument( path, body, params )
  payload = {
    :index   => params[:index],
    :type    => params[:type],
    :action  => params[:action],
    :context => params[:context],
    :body    => body
  }

  ::Elastomer::Notifications.service.instrument(NAME, payload) do
    response = yield
    payload[:url]    = response.env[:url]
    payload[:method] = response.env[:method]
    payload[:status] = response.status
    response
  end
end