Sha256: 054afcc7d9b1695cae5d20992260b8bcfcf23f9c571fe58aa6efc015b5212688

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'grape'
require 'active_support'

module Appsignal
  module Grape
    class Middleware < ::Grape::Middleware::Base
      def initialize(app)
        @app = app
      end

      def call(env)
        req = ::Rack::Request.new(env)
        method = env['REQUEST_METHOD']

        api_endpoint = env['api.endpoint']

        puts api_endpoint.method_name

        method_name = api_endpoint.method_name.gsub(/ ?[ \/]/, '/') if api_endpoint && api_endpoint.method_name

        request_path = api_endpoint.routes.first.route_path[1..-1].sub(/\(\.:format\)\z/, "")

        metric_name  = "process_action.grape.#{req.request_method}.#{request_path}"
        metric_name = metric_name.gsub(/\/:?/, '.')

        action = "Grape"
        action = action + "(#{api_endpoint.settings[:version].first})" if
          api_endpoint.settings[:version] && api_endpoint.settings[:version].first
        action = action + "(#{api_endpoint.settings[:root_prefix]})" if
          api_endpoint.settings  && api_endpoint.settings[:root_prefix]

        action = action + "::#{method_name}"
        #action = action.gsub(/  ?/, '/')

        ActiveSupport::Notifications.instrument(metric_name, { method: method, path: request_path, action: action, class: "API" } ) do |payload|
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-appsignal-0.4.0 lib/grape-appsignal/middleware.rb