Module: Skywalking::Plugins::NetHttp::HttpIntercept

Defined in:
lib/skywalking/plugins/net_http.rb

Instance Method Summary collapse

Instance Method Details

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



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
54
55
56
57
58
59
# File 'lib/skywalking/plugins/net_http.rb', line 20

def request(req, body = nil, &block)
  method = req.method
  host = req['host']&.split(':')&.first || address || 'localhost'
  req_path = URI(req.path)
  req_info = ""
  uri = if req_path.host
          req_path
        else
          path, query = req.path.split('?')
          scheme = use_ssl? ? 'https' : 'http'
          req_info = "#{path}#{query ? "?#{query}" : ''}"
          full_url = "#{scheme}://#{host}#{":#{port}" if port}#{req_info}"
          URI(full_url)
        end

  Tracing::ContextManager.new_exit_span(
    operation: "#{method}:#{req_info}",
    peer: host,
    component: Tracing::Component::HttpClient
  ) do |span|
    span&.tag(Tracing::TagHttpMethod.new(method))
    span&.tag(Tracing::TagHttpURL.new(uri))
    span&.layer = Tracing::Layer::Http

    carrier = span&.inject
    headers = req.instance_variable_get(:@header)
    carrier&.each do |item|
      headers[item.key] ||= []
      headers[item.key].concat(Array(item.value))
    end

    result = super(req, body, &block)
    span&.tag(Tracing::TagHttpStatusCode.new(result.code))
    if result.code >= "400"
      span&.error_occurred = true
    end

    result
  end
end