From 9e6e0ec2b29a6ed89d0b37602652e9c159472623 Mon Sep 17 00:00:00 2001 From: Timothy Kovalev Date: Fri, 19 May 2017 13:39:47 +0300 Subject: [PATCH] Instrument validators with ActiveSupport::Notifications --- .rubocop_todo.yml | 2 +- CHANGELOG.md | 1 + README.md | 8 ++++++++ lib/grape/endpoint.rb | 20 +++++++++++--------- spec/grape/endpoint_spec.rb | 6 ++++++ 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1d7f66c286..605eb31184 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -23,7 +23,7 @@ Metrics/BlockNesting: # Offense count: 8 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 281 + Max: 283 # Offense count: 26 Metrics/CyclomaticComplexity: diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dccef1db0..afb0691242 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * [#1594](https://github.com/ruby-grape/grape/pull/1594): Replace `Hashie::Mash` parameters with `ActiveSupport::HashWithIndifferentAccess` - [@james2m](https://github.com/james2m), [@dblock](https://github.com/dblock). * [#1622](https://github.com/ruby-grape/grape/pull/1622): Add `except_values` validator to replace `except` option of `values` validator - [@jlfaber](https://github.com/jlfaber). +* [#1635](https://github.com/ruby-grape/grape/pull/1635): Instrument validators with ActiveSupport::Notifications - [@ktimothy](https://github.com/ktimothy). * Your contribution here. #### Fixes diff --git a/README.md b/README.md index c7dda05922..ffaca5901b 100644 --- a/README.md +++ b/README.md @@ -3367,6 +3367,14 @@ The execution of the main content block of the endpoint. * *filters* - The filters being executed * *type* - The type of filters (before, before_validation, after_validation, after) +#### endpoint_run_validators.grape + +The execution of validators. + +* *endpoint* - The endpoint instance +* *validators* - The validators being executed +* *request* - The request being validated + See the [ActiveSupport::Notifications documentation](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) for information on how to subscribe to these events. ### Monitoring Products diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 3ff2d6c380..c91dc883c0 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -336,15 +336,17 @@ def lazy_initialize! def run_validators(validators, request) validation_errors = [] - validators.each do |validator| - begin - validator.validate(request) - rescue Grape::Exceptions::Validation => e - validation_errors << e - break if validator.fail_fast? - rescue Grape::Exceptions::ValidationArrayErrors => e - validation_errors += e.errors - break if validator.fail_fast? + ActiveSupport::Notifications.instrument('endpoint_run_validators.grape', endpoint: self, validators: validators, request: request) do + validators.each do |validator| + begin + validator.validate(request) + rescue Grape::Exceptions::Validation => e + validation_errors << e + break if validator.fail_fast? + rescue Grape::Exceptions::ValidationArrayErrors => e + validation_errors += e.errors + break if validator.fail_fast? + end end end diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index 1af9b73b94..4902a3efc8 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -1403,6 +1403,9 @@ def memoized have_attributes(name: 'endpoint_run_filters.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), filters: [], type: :before_validation }), + have_attributes(name: 'endpoint_run_validators.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), + validators: [], + request: a_kind_of(Grape::Request) }), have_attributes(name: 'endpoint_run_filters.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), filters: [], type: :after_validation }), @@ -1424,6 +1427,9 @@ def memoized have_attributes(name: 'endpoint_run_filters.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), filters: [], type: :before_validation }), + have_attributes(name: 'endpoint_run_validators.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), + validators: [], + request: a_kind_of(Grape::Request) }), have_attributes(name: 'endpoint_run_filters.grape', payload: { endpoint: a_kind_of(Grape::Endpoint), filters: [], type: :after_validation }),