Module: EpsagonNetHTTPExtension

Defined in:
lib/instrumentation/net_http.rb

Overview

Net::HTTP patch for epsagon instrumentaton

Constant Summary collapse

HTTP_METHODS_TO_SPAN_NAMES =
Hash.new { |h, k| h[k] = "HTTP #{k}" }
USE_SSL_TO_SCHEME =
{ false => 'http', true => 'https' }.freeze

Instance Method Summary collapse

Instance Method Details

#configObject



13
14
15
# File 'lib/instrumentation/net_http.rb', line 13

def config
  EpsagonNetHTTPInstrumentation.instance.config
end

#request(req, body = nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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