Module: ElastomerClient::Notifications
- Included in:
- Client
- Defined in:
- lib/elastomer_client/notifications.rb
Overview
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:
-
:index - index name (if any)
-
:type - document 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 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
Constant Summary collapse
- NAME =
The name to subscribe to for notifications
"request.client.elastomer".freeze
Class Attribute Summary collapse
-
.service ⇒ Object
Returns the value of attribute service.
Instance Method Summary collapse
-
#instrument(path, body, params) ⇒ Object
Internal: Execute the given block and provide instrumentation info to subscribers.
Class Attribute Details
.service ⇒ Object
Returns the value of attribute service.
42 43 44 |
# File 'lib/elastomer_client/notifications.rb', line 42 def service @service end |
Instance Method Details
#instrument(path, body, params) ⇒ Object
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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 |