Sha256: df2b857b16e7d5bbbc2344d1157cf6517f07a092818635d142549a9f355bc7ce

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

require 'rack/utils'

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

        attr_reader :app
        attr_reader :notifications

        def initialize(*args)
          @notifications, @app = *args

          notifications.event(REQUEST_START)
          notifications.event(REQUEST_STOP)
          notifications.event(REQUEST_ERROR)
        end

        def new(app)
          self.class.new(notifications, app)
        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

2 entries across 2 versions & 1 rubygems

Version Path
dry-monitor-0.0.3 lib/dry/monitor/rack/middleware.rb
dry-monitor-0.0.2 lib/dry/monitor/rack/middleware.rb