ruby-on-railsrubyelasticsearchopensearchamazon-opensearch

How to Enable Logging in Rails Application with OpenSearch and Searchkick Integration?


My Rails application currently utilizes Elasticsearch with the elasticsearch-model and elasticsearch-rails gems for integration. In the config/initializers/elasticsearch.rb file, I've configured it as follows:

config = {
  log: Rails.env.development? ? true : false,
}

Elasticsearch::Model.client = Elasticsearch::Client.new(config)

This setup enables logging in the Rails console when performing actions like Elasticsearch::Model.client.search or bulk data operations.

Now, I'm planning to transition to OpenSearch and use the Searchkick gem. Upon initializing the @client, I observed that the Searchkick library code lacks an option to enable logging. Here's the relevant part of the code:

if client_type == :opensearch
  OpenSearch::Client.new({
    url: ENV["OPENSEARCH_URL"],
    transport_options: { request: { timeout: timeout }, headers: { content_type: "application/json" } },
    retry_on_failure: 2
  }.deep_merge(client_options)) do |f|
    f.use Searchkick::Middleware
    f.request :aws_sigv4, signer_middleware_aws_params if aws_credentials
  end
end

As a result, I'm unable to see any logs when querying data.

Could anyone provide suggestions on how to enable logging for the development process? Any assistance would be greatly appreciated.


Solution

  • The answer by Daniel N. did not work for me. The document that is mentioned in that answer guides how to configure various types of logs in OpenSearch, such as application logs, slow logs, and task logs. I want to have logs on the Rails server when there is a connection between OpenSearch and Rails, so I did the following to debug in dev env:

    def self.client
      @client ||= if Rails.env.production? || Rails.env.staging?
                    Searchkick.client
                  else
                    OpenSearch::Client.new(
                      url: ENV['OPENSEARCH_URL'],
                      retry_on_failure: 5,
                      request_timeout: 120,
                      log: true
                    )
                  end
    end