Sha256: 6a35001f1362136ee1635291d7d84d78b4f4ce238e42497af9e99cae8b9cc7b2

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

require 'rack/utils'
require 'dry/monitor/notifications'

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

        attr_reader :notifications

        def initialize(*args)
          @notifications, @app = *args
        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

4 entries across 4 versions & 1 rubygems

Version Path
dry-monitor-0.2.0 lib/dry/monitor/rack/middleware.rb
dry-monitor-0.1.2 lib/dry/monitor/rack/middleware.rb
dry-monitor-0.1.1 lib/dry/monitor/rack/middleware.rb
dry-monitor-0.1.0 lib/dry/monitor/rack/middleware.rb