Module: OpenTelemetry::Instrumentation::Net::HTTP::Patches::Instrumentation

Defined in:
lib/hypertrace/instrumentation/net_http_patch.rb

Instance Method Summary collapse

Instance Method Details

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



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
# File 'lib/hypertrace/instrumentation/net_http_patch.rb', line 2

def request(req, body = nil, &block)
  # Do not trace recursive call for starting the connection
  return super(req, body, &block) unless started?

  # Prevent tracing our own export calls
  return super(req, body, &block) if req.path == "/v1/traces"

  scheme = use_ssl? ? "https://" : "http"
  url = "#{scheme}#{@address}:#{@port}#{req.path}"
  attributes = {
    OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => req.method,
    OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => USE_SSL_TO_SCHEME[use_ssl?],
    OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => req.path,
    OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => @address,
    OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => @port,
    OpenTelemetry::SemanticConventions::Trace::HTTP_URL =>  url
  }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)

  header_map = req.instance_variable_get(:@header)
  ht_attributes = Hypertrace::Instrumentation::DataCapture.headers_to_attribute_keys(header_map,
                                                                                    Hypertrace::Instrumentation::DataCapture::TYPE_REQUEST)
  content_type = header_map.find{|k, v| k.downcase == "content-type"}&.last
  if Hypertrace::Instrumentation::DataCapture.can_capture?(content_type, Hypertrace::Instrumentation::DataCapture::TYPE_REQUEST)
    body_cap = Hypertrace::Instrumentation::DataCapture.capturable_body(req.body)
    ht_attributes['http.request.body'] = body_cap if body_cap
  end
  tracer.in_span(
    HTTP_METHODS_TO_SPAN_NAMES[req.method],
    attributes: attributes.merge!(ht_attributes),
    kind: :client
  ) do |span|
    OpenTelemetry.propagation.inject(req)

    super(req, body, &block).tap do |response|
      response_headers = response.instance_variable_get(:@header)
      Hypertrace::Instrumentation::DataCapture.headers_to_attribute_keys(response_headers,
                                                                         Hypertrace::Instrumentation::DataCapture::TYPE_RESPONSE) do |k, v|
        span.set_attribute(k, v)
      end
      content_type = response_headers.find{|k, v| k.downcase == "content-type"}&.last
      if Hypertrace::Instrumentation::DataCapture.can_capture?(content_type, Hypertrace::Instrumentation::DataCapture::TYPE_RESPONSE)
        span.set_attribute('http.response.body', Hypertrace::Instrumentation::DataCapture.capturable_body(response.body))
      end
      annotate_span_with_response!(span, response)
    end
  end
end