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

Defined in:
lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb

Overview

Module to prepend to Net::HTTP for instrumentation

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

#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
# File 'lib/opentelemetry/instrumentation/net/http/patches/instrumentation.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 untraced?

  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
  }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)

  tracer.in_span(
    HTTP_METHODS_TO_SPAN_NAMES[req.method],
    attributes: attributes,
    kind: :client
  ) do |span|
    OpenTelemetry.propagation.inject(req)

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