Sha256: af4ea21cccb1390b1b4a16aaeaf1089d4661bee37ce6bc1cfe62c0b22fdbf1db

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module LoggedRequest
  def logged_request(opts = {}, &block)
    # We intend on using the following variables in this method's
    # ensure block. This means that we must take care to ensure
    # that we check for nil against these variables whenever they
    # are accessed.
    started = Time.now
    log_request(opts, started)
    response, exception = nil, nil
    response = execute(opts, &block)
  rescue StandardError => ex
    exception = ex.class.to_s
    response = ex.respond_to?(:response) ? ex.response : nil
    raise ex # Re-raise the exception, we just wanted to capture it
  ensure
    logged_response = opts.merge(exception: exception, response: response)
    ActiveSupport::Notifications.instrument(
      RestClient::Jogger.response_pattern,
      log_payload(logged_response, started)
    )
  end

  private

  def log_payload(params, started)
    params.merge(
      headers: filtered_headers(params),
      start_time: started
    )
  end

  def filtered_headers(opts)
    opts.fetch(:headers, {}).reject { |k, _| k.to_s.casecmp('authorization').zero? }
  end

  def log_request(opts, started)
    ActiveSupport::Notifications.instrument(
      RestClient::Jogger.request_pattern,
      log_payload(opts, started)
    )
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rest-client-jogger-1.1.0 lib/rest_client/core_ext/logged_request.rb
rest-client-jogger-1.0.1 lib/rest_client/core_ext/logged_request.rb
rest-client-jogger-1.0.0 lib/rest_client/core_ext/logged_request.rb