Skip to content

Commit

Permalink
Merge pull request #1759 from zvkemp/instrument-serialization
Browse files Browse the repository at this point in the history
Instrument serialization and rendering as 'format_response.grape'
  • Loading branch information
dblock authored May 5, 2018
2 parents 786fbcc + 7f9e329 commit c951628
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ matrix:
gemfile: gemfiles/rack_edge.gemfile
- rvm: 2.3.5
gemfile: gemfiles/rack_1.5.2.gemfile
- rvm: 2.3.5
gemfile: gemfiles/rails_edge.gemfile
- rvm: 2.3.5
gemfile: gemfiles/rails_5.gemfile
- rvm: 2.2.8
Expand Down
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ appraise 'rack-1.5.2' do
end

appraise 'rails-edge' do
gem 'arel', github: 'rails/arel'
gem 'rails', github: 'rails/rails'
end

appraise 'rack-edge' do
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

#### Features

* Your contribution here.
* [#1759](https://github.com/ruby-grape/grape/pull/1759): Instrument serialization as `'format_response.grape'` - [@zvkemp](https://github.com/zvkemp).

#### Fixes

* [#1759](https://github.com/ruby-grape/grape/pull/1759): Update appraisal for rails_edge - [@zvkemp](https://github.com/zvkemp).
* Your contribution here.

### 1.0.3 (4/23/2018)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
- [endpoint_render.grape](#endpoint_rendergrape)
- [endpoint_run_filters.grape](#endpoint_run_filtersgrape)
- [endpoint_run_validators.grape](#endpoint_run_validatorsgrape)
- [format_response.grape](#format_responsegrape)
- [Monitoring Products](#monitoring-products)
- [Contributing to Grape](#contributing-to-grape)
- [License](#license)
Expand Down Expand Up @@ -3484,6 +3485,13 @@ The execution of validators.
* *validators* - The validators being executed
* *request* - The request being validated

#### format_response.grape

Serialization or template rendering.

* *env* - The request environment
* *formatter* - The formatter object (e.g., `Grape::Formatter::Json`)

See the [ActiveSupport::Notifications documentation](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) for information on how to subscribe to these events.

### Monitoring Products
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source 'https://rubygems.org'

gem 'arel', github: 'rails/arel'
gem 'rails', github: 'rails/rails'

group :development, :test do
gem 'bundler'
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def build_formatted_response(status, headers, bodies)
else
# Allow content-type to be explicitly overwritten
formatter = fetch_formatter(headers, options)
bodymap = bodies.collect { |body| formatter.call(body, env) }
bodymap = ActiveSupport::Notifications.instrument('format_response.grape', formatter: formatter, env: env) do
bodies.collect { |body| formatter.call(body, env) }
end
Rack::Response.new(bodymap, status, headers)
end
rescue Grape::Exceptions::InvalidFormatter => e
Expand Down
8 changes: 6 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,9 @@ def memoized
filters: [],
type: :after }),
have_attributes(name: 'endpoint_run.grape', payload: { endpoint: a_kind_of(Grape::Endpoint),
env: an_instance_of(Hash) })
env: an_instance_of(Hash) }),
have_attributes(name: 'format_response.grape', payload: { env: an_instance_of(Hash),
formatter: a_kind_of(Module) })
)

# In order that events were initialized
Expand All @@ -1515,7 +1517,9 @@ def memoized
have_attributes(name: 'endpoint_render.grape', payload: { endpoint: a_kind_of(Grape::Endpoint) }),
have_attributes(name: 'endpoint_run_filters.grape', payload: { endpoint: a_kind_of(Grape::Endpoint),
filters: [],
type: :after })
type: :after }),
have_attributes(name: 'format_response.grape', payload: { env: an_instance_of(Hash),
formatter: a_kind_of(Module) })
)
end
end
Expand Down

0 comments on commit c951628

Please sign in to comment.