Module: Sbmt::Strangler::Http
- Defined in:
- lib/sbmt/strangler/http.rb,
lib/sbmt/strangler/http/client.rb,
lib/sbmt/strangler/http/transport.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_HTTP_OPTIONS =
{ keepalive_pool_size: 256, keepalive_idle_timeout: 30, timeout: 5, read_timeout: 5, write_timeout: 5, open_timeout: 1, retries_count: 1 }.freeze
- REQUEST_PATH_FILTER_REGEX =
%r{(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})|(/\d+)|(/[A-Z]\d{9,11}(-\d{1})?)}
Class Method Summary collapse
-
.configure_faraday(conn, opts = {}) ⇒ Faraday::Connection
Configures Faraday connection.
- .configure_faraday_metrics(conn, opts = {}) ⇒ Object
Class Method Details
.configure_faraday(conn, opts = {}) ⇒ Faraday::Connection
Configures Faraday connection. Sets default options and adds default middlewares into chain. Accepts an optional block to configure net-http-persistent-adapter
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sbmt/strangler/http.rb', line 42 def self.configure_faraday(conn, opts = {}) raise ConfigurationError, "Faraday client :name must be set" unless opts[:name] = opts[:http_options] || Sbmt::Strangler.configuration.http conn..timeout = .timeout conn..read_timeout = .read_timeout conn..open_timeout = .open_timeout conn..write_timeout = .write_timeout configure_faraday_metrics(conn, opts.slice(:name, :request_path_filter_regex)) adapter_opts = {pool_size: .keepalive_pool_size}.merge(opts[:adapter_opts] || {}) conn.adapter :net_http_persistent, adapter_opts do |http| http.idle_timeout = .keepalive_idle_timeout yield http if block_given? end conn end |
.configure_faraday_metrics(conn, opts = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sbmt/strangler/http.rb', line 63 def self.configure_faraday_metrics(conn, opts = {}) @subscribers ||= {} name = opts.fetch(:name) instrument_full_name = ["request.faraday", name].compact.join(".") filter = opts.fetch(:request_path_filter_regex, REQUEST_PATH_FILTER_REGEX) conn.request :instrumentation, name: instrument_full_name return if @subscribers[instrument_full_name] @subscribers[instrument_full_name] = ActiveSupport::Notifications.subscribe(instrument_full_name) do |*args| event = ActiveSupport::Notifications::Event.new(*args) env = event.payload = { name: name, method: env.method, status: env.status || :error, host: env.url.host, path: filter ? env.url.path.gsub(filter, "/:id") : "" } Yabeda.sbmt_strangler.http_request_duration.measure(, event.duration.fdiv(1000)) end end |