Module: ElasticAPM::Spies::NetHTTPSpy::Ext Private

Defined in:
lib/elastic_apm/spies/net_http.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/elastic_apm/spies/net_http.rb', line 52

def request(req, body = nil, &block)
  unless (transaction = ElasticAPM.current_transaction)
    return super(req, body, &block)
  end

  if ElasticAPM::Spies::NetHTTPSpy.disabled?
    return super(req, body, &block)
  end

  host = req['host']&.split(':')&.first || address || 'localhost'
  method = req.method.to_s.upcase

  uri_or_path = URI(req.path)

  # Support the case where a whole url is passed as a path to a nil host
  uri =
    if uri_or_path.host
      uri_or_path
    else
      path, query = req.path.split('?')
      url = use_ssl? ? +'https://' : +'http://'
      url << host
      url << ":#{port}" if port
      url << path
      url << "?#{query}" if query
      URI(url)
    end

  context =
    ElasticAPM::Span::Context.new(
      http: { url: uri, method: method },
      destination: ElasticAPM::Span::Context::Destination.from_uri(uri, type: SUBTYPE)
    )

  ElasticAPM.with_span(
    "#{method} #{host}",
    TYPE,
    subtype: SUBTYPE,
    context: context
  ) do |span|
    trace_context = span&.trace_context || transaction.trace_context
    trace_context.apply_headers { |key, value| req[key] = value }

    result = super(req, body, &block)

    if (http = span&.context&.http)
      http.status_code = result.code
    end

    span&.outcome = Span::Outcome.from_http_status(result.code)
    result
  end
end