Sha256: 737904f8f95e52649fbf9f889b5e46759e9e4ef6a587f3286deb9a75199bcf48

Contents?: true

Size: 1.95 KB

Versions: 13

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module HTTP
  module Features
    # Instrument requests and responses. Expects an
    # ActiveSupport::Notifications-compatible instrumenter. Defaults to use a
    # namespace of 'http' which may be overridden with a `:namespace` param.
    # Emits a single event like `"request.{namespace}"`, eg `"request.http"`.
    # Be sure to specify the instrumenter when enabling the feature:
    #
    #    HTTP
    #      .use(instrumentation: {instrumenter: ActiveSupport::Notifications.instrumenter})
    #      .get("https://example.com/")
    #
    # Emits two events on every request:
    #
    #  * `start_request.http` before the request is made, so you can log the reqest being started
    #  * `request.http` after the response is recieved, and contains `start`
    #    and `finish` so the duration of the request can be calculated.
    #
    class Instrumentation < Feature
      attr_reader :instrumenter, :name

      def initialize(instrumenter: NullInstrumenter.new, namespace: "http")
        @instrumenter = instrumenter
        @name = "request.#{namespace}"
      end

      def wrap_request(request)
        # Emit a separate "start" event, so a logger can print the request
        # being run without waiting for a response
        instrumenter.instrument("start_#{name}", :request => request)
        instrumenter.start(name, :request => request)
        request
      end

      def wrap_response(response)
        instrumenter.finish(name, :response => response)
        response
      end

      HTTP::Options.register_feature(:instrumentation, self)

      class NullInstrumenter
        def instrument(name, payload = {})
          start(name, payload)
          begin
            yield payload if block_given?
          ensure
            finish name, payload
          end
        end

        def start(_name, _payload)
          true
        end

        def finish(_name, _payload)
          true
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/features/instrumentation.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/features/instrumentation.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/features/instrumentation.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/features/instrumentation.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/features/instrumentation.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/features/instrumentation.rb
http-5.1.1 lib/http/features/instrumentation.rb
http-5.1.0 lib/http/features/instrumentation.rb
http-5.0.4 lib/http/features/instrumentation.rb
http-5.0.3 lib/http/features/instrumentation.rb
http-5.0.2 lib/http/features/instrumentation.rb
http-5.0.1 lib/http/features/instrumentation.rb
http-5.0.0 lib/http/features/instrumentation.rb