class Bigcommerce::Lightstep::Middleware::Faraday

Faraday middleware. It will add appropriate OT tags and trace IDs to outgoing connections done by Faraday

Constants

HTTP_ERROR_STATUS_THRESHOLD
HTTP_STATUS_INTERNAL_ERROR
HTTP_STATUS_REQUEST_TIMEOUT
HTTP_STATUS_SERVICE_UNAVAIL
OT_TAG_SAMPLED
OT_TAG_SPAN_ID
OT_TAG_TRACE_ID

Public Class Methods

new(app, service_name = nil) click to toggle source
Calls superclass method
# File lib/bigcommerce/lightstep/middleware/faraday.rb, line 32
def initialize(app, service_name = nil)
  super(app)
  @service_name = (service_name || 'external').to_s
end

Public Instance Methods

call(request_env) click to toggle source
# File lib/bigcommerce/lightstep/middleware/faraday.rb, line 37
def call(request_env)
  uri = uri_from_env(request_env)
  tracer = ::Bigcommerce::Lightstep::Tracer.instance

  tracer.start_span(service_key, context: request_env[:request_headers]) do |span|
    span.set_tag('http.url', uri.to_s.split('?').first)
    span.set_tag('http.method', request_env[:method].to_s.downcase)
    span.set_tag('http.external-service', true)
    span.set_tag('span.kind', 'client')

    inject_ot_tags!(request_env, span)

    begin
      response = @app.call(request_env).on_complete do |response_env|
        span.set_tag('http.status_code', response_env[:status].to_s)
        span.set_tag('error', true) if response_env[:status].to_i >= HTTP_ERROR_STATUS_THRESHOLD
        response_env
      end
    rescue ::Net::ReadTimeout
      span.set_tag('error', true)
      span.set_tag('http.status_code', HTTP_STATUS_REQUEST_TIMEOUT)
      raise
    rescue ::Faraday::ConnectionFailed
      span.set_tag('error', true)
      span.set_tag('http.status_code', HTTP_STATUS_SERVICE_UNAVAIL)
      raise
    rescue ::Faraday::ClientError
      span.set_tag('error', true)
      span.set_tag('http.status_code', HTTP_STATUS_INTERNAL_ERROR)
      raise
    end

    response
  end
end

Private Instance Methods

inject_ot_tags!(request_env, span) click to toggle source

@param [Hash] request_env @param [::LightStep::Span] span

# File lib/bigcommerce/lightstep/middleware/faraday.rb, line 79
def inject_ot_tags!(request_env, span)
  request_env[:request_headers].merge!(
    OT_TAG_TRACE_ID => span.context.trace_id.to_s,
    OT_TAG_SPAN_ID => span.context.id.to_s,
    OT_TAG_SAMPLED => 'true'
  )
end
path_key_for_uri(uri) click to toggle source

@param [URI::HTTP] uri @return [String]

# File lib/bigcommerce/lightstep/middleware/faraday.rb, line 101
def path_key_for_uri(uri)
  uri.path.tr('/', '_').reverse.chomp('_').reverse.chomp
end
service_key() click to toggle source

@return [String]

# File lib/bigcommerce/lightstep/middleware/faraday.rb, line 108
def service_key
  @service_name.to_s.downcase.tr('-', '_').tr('.', '_')
end
uri_from_env(env) click to toggle source

Handle either a URI object (passed by Faraday v0.8.x in testing), or something string-izable

@param [Hash] env @return [URI::HTTP]

# File lib/bigcommerce/lightstep/middleware/faraday.rb, line 93
def uri_from_env(env)
  env[:url].respond_to?(:host) ? env[:url] : URI.parse(env[:url].to_s)
end