Sha256: 28343f1af1d9c2bf4dc52e9f2a312bf3fe82e0f45781071085d5e972d8d3af96

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

module Sapience
  module Extensions
    module Grape
      class Notifications < ::Sapience::Extensions::Notifications
        # Options:
        #
        # *<tt>:metric_name</tt> - the metric name, defaults to "grape.request"
        # *<tt>:tags</tt> - additional tags
        def initialize(opts = {})
          super
          @metric_name = opts[:metric_name] || "grape.request"

          Sapience::Extensions::Notifications.subscribe "endpoint_run.grape" do |event|
            record event
          end
        end

        private

        def record(event) # rubocop:disable AbcSize
          return unless record?

          payload  = event.payload
          endpoint = payload[:endpoint]
          route    = endpoint.route
          version  = route.version
          method   = route.request_method.downcase
          format   = payload[:endpoint].instance_variable_get(:@app).instance_variable_get(:@app).options[:format]
          path = route.pattern.path.dup

          path.sub!(/\(\.#{format}\)$/, "")
          path.sub!(":version/", "") if version
          path.gsub!(/:(\w+)/) { |m| m[1..-1].upcase }
          path.gsub!(/[^\w\/\-]+/, "_")

          tags = self.tags + %W(method:#{method} format:#{format} path:#{path} status:#{endpoint.status})
          tags.push "version:#{version}" if version
          metrics.batch do
            metrics.increment metric_name, tags: tags
            metrics.timing "#{metric_name}.time", event.duration, tags: tags
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sapience-1.0.11 lib/sapience/extensions/grape/notifications.rb
sapience-1.0.10 lib/sapience/extensions/grape/notifications.rb
sapience-1.0.9 lib/sapience/extensions/grape/notifications.rb
sapience-1.0.8 lib/sapience/extensions/grape/notifications.rb
sapience-1.0.7 lib/sapience/extensions/grape/notifications.rb
sapience-1.0.6 lib/sapience/extensions/grape/notifications.rb
sapience-1.0.5 lib/sapience/extensions/grape/notifications.rb
sapience-1.0.4 lib/sapience/extensions/grape/notifications.rb