Sha256: 7c4743d2fe7c78c9fb50d262de1fb4e06ad45220cc5dc5aea4cf5d70f6ec3f96

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

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

module OpenTelemetry
  module Instrumentation
    module HTTP
      module Patches
        # Module to prepend to HTTP::Client for instrumentation
        module Client
          def perform(req, options) # rubocop:disable Metrics/AbcSize
            uri = req.uri
            request_method = req.verb.to_s.upcase

            attributes = {
              'http.method' => request_method,
              'http.scheme' => uri.scheme,
              'http.target' => uri.path,
              'http.url' => "#{uri.scheme}://#{uri.host}",
              'peer.hostname' => uri.host,
              '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.headers)
              super.tap do |response|
                annotate_span_with_response!(span, response)
              end
            end
          end

          private

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-http-0.18.0 lib/opentelemetry/instrumentation/http/patches/client.rb
opentelemetry-instrumentation-http-0.17.0 lib/opentelemetry/instrumentation/http/patches/client.rb
opentelemetry-instrumentation-http-0.16.2 lib/opentelemetry/instrumentation/http/patches/client.rb
opentelemetry-instrumentation-http-0.16.1 lib/opentelemetry/instrumentation/http/patches/client.rb
opentelemetry-instrumentation-http-0.16.0 lib/opentelemetry/instrumentation/http/patches/client.rb