module EpsagonNetHTTPExtension

Net::HTTP patch for epsagon instrumentaton

Constants

HTTP_METHODS_TO_SPAN_NAMES
USE_SSL_TO_SCHEME

Public Instance Methods

config() click to toggle source
# File lib/instrumentation/net_http.rb, line 13
def config
  EpsagonNetHTTPInstrumentation.instance.config
end
request(req, body = nil, &block) click to toggle source
Calls superclass method
# File lib/instrumentation/net_http.rb, line 17
def request(req, body = nil, &block)
  # Do not trace recursive call for starting the connection
  return super(req, body, &block) unless started?
  return super(req, body, &block) if config[:epsagon][:ignore_domains].any? {|d| @address.include? d}

  attributes = Hash[OpenTelemetry::Common::HTTP::ClientContext.attributes]
  path_with_params, query = req.path.split('?')
  path, path_params = path_with_params.split(';')
  attributes.merge!({
                      'type' => 'http',
                      'operation' => req.method,
                      'http.scheme' => USE_SSL_TO_SCHEME[use_ssl?],
                      'http.request.path' => path
                    })

  unless config[:epsagon][:metadata_only]
    headers = Hash[req.each_header.to_a]
    attributes.merge!({
                        'http.request.path_params' => path_params,
                        'http.request.body' => body,
                        'http.request.headers' => Hash[headers],
                        'http.request.headers.User-Agent' => headers['user-agent']
                      })
    attributes.merge!(Util.epsagon_query_attributes(query))
  end
  tracer.in_span(
    @address,
    attributes: attributes,
    kind: :client
  ) do |span|
    OpenTelemetry.propagation.http.inject(req)

    super(req, body, &block).tap do |response|
      annotate_span_with_response!(span, response)
    end
  end
end

Private Instance Methods

annotate_span_with_response!(span, response) click to toggle source
# File lib/instrumentation/net_http.rb, line 57
def annotate_span_with_response!(span, response)
  return unless response&.code
  return unless span.respond_to?(:set_attribute)

  status_code = response.code.to_i

  span.set_attribute('http.status_code', status_code)
  unless config[:epsagon][:metadata_only]
    span.set_attribute('http.response.headers', Hash[response.each_header.to_a])
    span.set_attribute('http.response.body', response.body)
  end
  span.status = OpenTelemetry::Trace::Status.http_to_status(
    status_code
  )
end
tracer() click to toggle source
# File lib/instrumentation/net_http.rb, line 73
def tracer
  EpsagonNetHTTPInstrumentation.instance.tracer
end