Sha256: 16d66f4f96b36191a6e989d3bfa37bec3a32300b6b9061189e3857ecee08f275

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

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

module OpenTelemetry
  module Instrumentation
    module HttpClient
      module Patches
        # Module to prepend to HTTPClient for instrumentation
        module Client
          private

          def do_get_block(req, proxy, conn, &block)
            uri = req.header.request_uri
            url = "#{uri.scheme}://#{uri.host}"
            request_method = req.header.request_method

            attributes = {
              'http.method' => request_method,
              'http.scheme' => uri.scheme,
              'http.target' => uri.path,
              'http.url' => url,
              'net.peer.name' => uri.host,
              'net.peer.port' => uri.port
            }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)

            tracer.in_span("HTTP #{request_method}", attributes: attributes, kind: :client) do |span|
              OpenTelemetry.propagation.inject(req.header)
              super.tap do
                response = conn.pop
                annotate_span_with_response!(span, response)
                conn.push response
              end
            end
          end

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

            status_code = response.status_code.to_i

            span.set_attribute('http.status_code', status_code)
            span.status = OpenTelemetry::Trace::Status.error unless (100..399).include?(status_code.to_i)
          end

          def tracer
            HttpClient::Instrumentation.instance.tracer
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-http_client-0.22.2 lib/opentelemetry/instrumentation/http_client/patches/client.rb
opentelemetry-instrumentation-http_client-0.22.1 lib/opentelemetry/instrumentation/http_client/patches/client.rb
opentelemetry-instrumentation-http_client-0.22.0 lib/opentelemetry/instrumentation/http_client/patches/client.rb
opentelemetry-instrumentation-http_client-0.21.0 lib/opentelemetry/instrumentation/http_client/patches/client.rb