Sha256: e298ccf6ad08b7d0bcb344927751d63a2fbcb4b0a927b7748bdb39459b42b6e4

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

# Copyright 2020 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?

              tracer.in_span(
                HTTP_METHODS_TO_SPAN_NAMES[req.method],
                attributes: {
                  '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

4 entries across 4 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-net_http-0.8.0 lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb
opentelemetry-instrumentation-net_http-0.7.0 lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb
opentelemetry-instrumentation-net_http-0.6.0 lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb
opentelemetry-instrumentation-net_http-0.5.0 lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb