module ElastomerClient::Notifications

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

require 'elastomer_client/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:

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

ElastomerClient::Notifications.service = your_own_service

Constants

NAME

The name to subscribe to for notifications

Attributes

service[RW]

Public Instance Methods

instrument(path, body, params) { || ... } click to toggle source

Internal: Execute the given block and provide instrumentation 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

# File lib/elastomer_client/notifications.rb, line 59
def instrument(path, body, params)
  payload = {
    index: params[:index],
    type: params[:type],
    action: params[:action],
    context: params[:context],
    request_body: body,
    body:   # for backwards compatibility
  }

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