Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report endpoint_call.grape which includes the whole stack #2021

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Metrics/BlockLength:
# Offense count: 10
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 305
Max: 308

# Offense count: 32
Metrics/CyclomaticComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
* [#2015](https://github.com/ruby-grape/grape/pull/2014): Reduce MatchData allocation - [@ericproulx](https://github.com/ericproulx).
* [#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).
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -3810,9 +3811,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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "inaccurate" is incorrect. I think here we want to highlight what this does, as opposed to what it doesn't. Maybe "Execution of the endpoint, excluding filters, middlewares or execution inside rescue_from blocks." + explain what happens during an exception?

One more difference that it returns original exceptions, before `rescue_from` handlers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be part of the text above. But this is unclear. What does "returns" mean here?


* *endpoint* - The endpoint instance

Expand Down
5 changes: 4 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down