diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b6477131a..4bf22d1f5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -72,7 +72,7 @@ Metrics/BlockLength: # Offense count: 9 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 298 + Max: 308 # Offense count: 30 # Configuration parameters: IgnoredMethods. diff --git a/README.md b/README.md index 2e4f7d533..86140ae11 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ - [Reloading in Rails Applications](#reloading-in-rails-applications) - [Performance Monitoring](#performance-monitoring) - [Active Support Instrumentation](#active-support-instrumentation) + - [endpoint_call.grape](#endpoint_callgrape) - [endpoint_run.grape](#endpoint_rungrape) - [endpoint_render.grape](#endpoint_rendergrape) - [endpoint_run_filters.grape](#endpoint_run_filtersgrape) @@ -3928,9 +3929,17 @@ Grape has built-in support for [ActiveSupport::Notifications](http://api.rubyonr The following are currently supported: +#### endpoint_call.grape + +The main execution of an endpoint, includes filters, rendering and all middlewares. + +* *endpoint* - The endpoint instance +* *response* - A typical Rack response, for example `[404, {"Content-Type"=>"application/json; charset=UTF-8"}, ["{\"error\":\"not_found\"}"]]` + #### endpoint_run.grape -The main execution of an endpoint, includes filters and rendering. +Similar to `endpoint_call.grape` but does not include middlewares, thus might be inaccurate. +One more difference that it returns original exceptions, before `rescue_from` handlers. * *endpoint* - The endpoint instance diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 35f5a3fc2..efeb9c001 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -222,7 +222,10 @@ def call(env) def call!(env) env[Grape::Env::API_ENDPOINT] = self @env = env - @app.call(env) + context = { endpoint: self, env: @env } + ActiveSupport::Notifications.instrument('endpoint_call.grape', context) do + @app.call(env).tap { |response| context[:response] = response } + end end # Return the collection of endpoints within this endpoint. diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 20efdc505..09cf2860e 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -1031,11 +1031,15 @@ def memoized have_attributes(name: 'endpoint_run.grape', payload: { endpoint: a_kind_of(described_class), env: an_instance_of(Hash) }), have_attributes(name: 'format_response.grape', payload: { env: an_instance_of(Hash), - formatter: a_kind_of(Module) }) + formatter: a_kind_of(Module) }), + have_attributes(name: 'endpoint_call.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), + env: an_instance_of(Hash), + response: match([200, be_a(Hash), be_a(Array)]) }) ) # In order that events were initialized expect(@events.sort_by(&:time)).to contain_exactly( + have_attributes(name: 'endpoint_call.grape'), have_attributes(name: 'endpoint_run.grape', payload: { endpoint: a_kind_of(described_class), env: an_instance_of(Hash) }), have_attributes(name: 'endpoint_run_filters.grape', payload: { endpoint: a_kind_of(described_class),