Sha256: 0612ba342080c10623cdb9ce404e1092914c15577a4a5dd12ed007b50b1ea4d1

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
  module Instrumentation
    module Net
      module HTTP
        module Patches
          # Module to prepend to Net::HTTP for instrumentation
          module Instrumentation
            HTTP_METHODS_TO_SPAN_NAMES = Hash.new { |h, k| h[k] = "HTTP #{k}" }
            USE_SSL_TO_SCHEME = { false => 'http', true => 'https' }.freeze

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

              attributes = OpenTelemetry::Common::HTTP::ClientContext.attributes
              tracer.in_span(
                HTTP_METHODS_TO_SPAN_NAMES[req.method],
                attributes: attributes.merge(
                  'http.method' => req.method,
                  'http.scheme' => USE_SSL_TO_SCHEME[use_ssl?],
                  'http.target' => req.path,
                  'peer.hostname' => @address,
                  'peer.port' => @port
                ),
                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

            private

            def annotate_span_with_response!(span, response)
              return unless response&.code

              status_code = response.code.to_i

              span.set_attribute('http.status_code', status_code)
              span.status = OpenTelemetry::Trace::Status.http_to_status(
                status_code
              )
            end

            def tracer
              Net::HTTP::Instrumentation.instance.tracer
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-net_http-0.13.0 lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb
opentelemetry-instrumentation-net_http-0.12.0 lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb
opentelemetry-instrumentation-net_http-0.11.0 lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb