Sha256: 0fce46ebf724a3a0de68e5cf4e7bc4b9204d96de663d34babf04616ba12ef593

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Dry
  module Monitor
    module Rack
      class Middleware
        REQUEST_START = :"rack.request.start"
        REQUEST_STOP = :"rack.request.stop"
        REQUEST_ERROR = :"rack.request.error"

        Notifications.register_event(REQUEST_START)
        Notifications.register_event(REQUEST_STOP)
        Notifications.register_event(REQUEST_ERROR)

        attr_reader :app, :notifications

        def initialize(*args, clock: CLOCK)
          @notifications, @app = *args
          @clock = clock
        end

        def new(app, *_args, clock: @clock, &_block)
          self.class.new(notifications, app, clock: clock)
        end

        def on(event_id, &block)
          notifications.subscribe(:"rack.request.#{event_id}", &block)
        end

        def instrument(event_id, *args, &block)
          notifications.instrument(:"rack.request.#{event_id}", *args, &block)
        end

        def call(env)
          notifications.start(REQUEST_START, env: env)
          response, time = @clock.measure { app.call(env) }
          notifications.stop(REQUEST_STOP, env: env, time: time, status: response[0])
          response
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-monitor-1.0.1 lib/dry/monitor/rack/middleware.rb