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

Classes: Client, Transport

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

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

Examples:


@conn ||= Faraday.new(@base_url) do |f|
  Sbmt::Strangler::Http.configure_faraday(f, name: "http-client") do |http|
    http.idle_timeout = 42
  end
  f.timeout = 5
  f.response :json
end

Parameters:

  • conn (Faraday::Connection)
  • opts (Hash) (defaults to: {})

    faraday & middlewares options

Options Hash (opts):

  • :name (String)

    client name for tracing and instrumentation. Required.

  • :adapter_opts (Hash)

    net_http_persistent adapter options

  • :http_options (ActiveSupport::InheritableOptions)

    timeout options

  • :request_path_filter_regex (Regexp) — default: REQUEST_PATH_FILTER_REGEX

    regex for filtering out variables from http request metric ‘path` tag. Set to false to add empty value instead.

Returns:

  • (Faraday::Connection)

Raises:



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]

  http_options = opts[:http_options] || Sbmt::Strangler.configuration.http

  conn.options.timeout = http_options.timeout
  conn.options.read_timeout = http_options.read_timeout
  conn.options.open_timeout = http_options.open_timeout
  conn.options.write_timeout = http_options.write_timeout

  configure_faraday_metrics(conn, opts.slice(:name, :request_path_filter_regex))

  adapter_opts = {pool_size: http_options.keepalive_pool_size}.merge(opts[:adapter_opts] || {})
  conn.adapter :net_http_persistent, adapter_opts do |http|
    http.idle_timeout = http_options.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

    tags = {
      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(tags, event.duration.fdiv(1000))
  end
end