From e3a50cace1d6a7dfafd8003aee21f4fa3932952d Mon Sep 17 00:00:00 2001 From: Sergiy Kukunin Date: Tue, 17 Mar 2020 13:39:30 +0200 Subject: [PATCH 1/3] Report endpoint_call.grape which includes the whole stack --- README.md | 10 +++++++++- lib/grape/endpoint.rb | 5 ++++- spec/grape/endpoint_spec.rb | 6 +++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 004c48b2ca..f7849b8dbc 100644 --- a/README.md +++ b/README.md @@ -3810,9 +3810,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 3fb4178749..a15a8cf5db 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -226,7 +226,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 37aea84bc4..8be03b411d 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -1530,11 +1530,15 @@ def memoized have_attributes(name: 'endpoint_run.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), 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(Grape::Endpoint), env: an_instance_of(Hash) }), have_attributes(name: 'endpoint_run_filters.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), From 1594413b152463c2a0606e1f87a8dce0fee964f9 Mon Sep 17 00:00:00 2001 From: Sergiy Kukunin Date: Tue, 17 Mar 2020 14:06:55 +0200 Subject: [PATCH 2/3] Fix documentation --- CHANGELOG.md | 1 + README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f6537b178..18cd6f2df7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Features * Your contribution here. +* [#2021](https://github.com/ruby-grape/grape/pull/2021): Report endpoint_call.grape which includes the whole stack - [@Kukunin](https://github.com/Kukunin). * [#2014](https://github.com/ruby-grape/grape/pull/2014): Reduce total allocated arrays - [@ericproulx](https://github.com/ericproulx). * [#2011](https://github.com/ruby-grape/grape/pull/2011): Reduce total retained regexes - [@ericproulx](https://github.com/ericproulx). diff --git a/README.md b/README.md index f7849b8dbc..221504968e 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,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) From c5c8601a597c110538d7056d22d348e416266813 Mon Sep 17 00:00:00 2001 From: Sergiy Kukunin Date: Tue, 17 Mar 2020 14:52:36 +0200 Subject: [PATCH 3/3] Make rubocop happy --- .rubocop_todo.yml | 2 +- lib/grape/endpoint.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b5140c65d0..ab6563784a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -78,7 +78,7 @@ Metrics/BlockLength: # Offense count: 10 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 305 + Max: 308 # Offense count: 32 Metrics/CyclomaticComplexity: diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index a15a8cf5db..91b2ddfcdf 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -226,7 +226,7 @@ def call(env) def call!(env) env[Grape::Env::API_ENDPOINT] = self @env = env - context = {endpoint: self, env: @env} + context = { endpoint: self, env: @env } ActiveSupport::Notifications.instrument('endpoint_call.grape', context) do @app.call(env).tap { |response| context[:response] = response } end