gems sentry-rails 5.0.0

latest releases: 5.21.0, 5.20.1, 5.20.0...
2 years ago

Breaking Change - Goodbye faraday 👋

TL;DR: If you are already on version 4.9 and do not use config.transport.http_adapter and config.transport.faraday_builder, you don't need to change anything.

This version removes the dependency of faraday and replaces related implementation with the Net::HTTP standard library.

Why?

Since the old sentry-raven SDK, we've been using faraday as the HTTP client for years (see HTTPTransport). It's an amazing tool that saved us many work and allowed us to focus on SDK features.

But because many users also use faraday themselves and have their own version requirements, managing this dependency has become harder over the past few years. Just to list a few related issues:

And with the release of faraday 2.0, we could only imagine it getting even more difficult (which it kind of did, see #1663).

So we think it's time to say goodbye to it with this release.

What's changed?

By default, the SDK used faraday's net_http adapter, which is also built on top of Net::HTTP. So this change shouldn't impact most of the users.

The only noticeable changes are the removal of 2 faraday-specific transport configurations:

  • config.transport.http_adapter
  • config.transport.faraday_builder

If you are already on version 4.9 and do not use those configuration options, it'll be as simple as bundle update.

What if I still want to use faraday to send my events?

sentry-ruby already allows users to set a custom transport class with:

Sentry.init do |config|
  config.transport.transport_class = MyTransportClass
end

So to use a faraday-based transport, you can:

  1. Build a FaradayTransport like this:
require 'sentry/transport/http_transport'
require 'faraday'

class FaradayTransport < Sentry::HTTPTransport
  attr_reader :adapter

  def initialize(*args)
    @adapter = :net_http
    super
  end

  def send_data(data)
    encoding = ""

    if should_compress?(data)
      data = Zlib.gzip(data)
      encoding = GZIP_ENCODING
    end

    response = conn.post @endpoint do |req|
      req.headers['Content-Type'] = CONTENT_TYPE
      req.headers['Content-Encoding'] = encoding
      req.headers['X-Sentry-Auth'] = generate_auth_header
      req.body = data
    end

    if has_rate_limited_header?(response.headers)
      handle_rate_limited_response(response.headers)
    end
  rescue Faraday::Error => e
    error_info = e.message

    if e.response
      if e.response[:status] == 429
        handle_rate_limited_response(e.response[:headers])
      else
        error_info += "\nbody: #{e.response[:body]}"
        error_info += " Error in headers is: #{e.response[:headers]['x-sentry-error']}" if e.response[:headers]['x-sentry-error']
      end
    end

    raise Sentry::ExternalError, error_info
  end

  private

  def set_conn
    server = @dsn.server

    log_debug("Sentry HTTP Transport connecting to #{server}")

    Faraday.new(server, :ssl => ssl_configuration, :proxy => @transport_configuration.proxy) do |builder|
      builder.response :raise_error
      builder.options.merge! faraday_opts
      builder.headers[:user_agent] = "sentry-ruby/#{Sentry::VERSION}"
      builder.adapter(*adapter)
    end
  end

  def faraday_opts
    [:timeout, :open_timeout].each_with_object({}) do |opt, memo|
      memo[opt] = @transport_configuration.public_send(opt) if @transport_configuration.public_send(opt)
    end
  end

  def ssl_configuration
    {
      verify: @transport_configuration.ssl_verification,
      ca_file: @transport_configuration.ssl_ca_file
    }.merge(@transport_configuration.ssl || {})
  end
end
  1. Set config.transport.transport = FaradayTransport

Please keep in mind that this may not work in the future when the SDK changes its HTTPTransport implementation.

Don't miss a new sentry-rails release

NewReleases is sending notifications on new releases.