Skip to content

Commit 8fbed0d

Browse files
authored
Hook readme (#2609)
* Add rails_performance to monitoring products * Update README - subscribe to hooks * Add a small example for hooks
1 parent a113803 commit 8fbed0d

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

README.md

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@
139139
- [Reloading in Rails Applications](#reloading-in-rails-applications)
140140
- [Performance Monitoring](#performance-monitoring)
141141
- [Active Support Instrumentation](#active-support-instrumentation)
142-
- [endpoint_run.grape](#endpoint_rungrape)
143-
- [endpoint_render.grape](#endpoint_rendergrape)
144-
- [endpoint_run_filters.grape](#endpoint_run_filtersgrape)
145-
- [endpoint_run_validators.grape](#endpoint_run_validatorsgrape)
146-
- [format_response.grape](#format_responsegrape)
142+
- [Hook Points](#hook-points)
143+
- [endpoint_run.grape](#endpoint_rungrape)
144+
- [endpoint_render.grape](#endpoint_rendergrape)
145+
- [endpoint_run_filters.grape](#endpoint_run_filtersgrape)
146+
- [endpoint_run_validators.grape](#endpoint_run_validatorsgrape)
147+
- [format_response.grape](#format_responsegrape)
148+
- [Subscribe to Hooks](#subscribe-to-hooks)
147149
- [Monitoring Products](#monitoring-products)
148150
- [Contributing to Grape](#contributing-to-grape)
149151
- [Security](#security)
@@ -4125,35 +4127,38 @@ See [StackOverflow #3282655](http://stackoverflow.com/questions/3282655/ruby-on-
41254127

41264128
Grape has built-in support for [ActiveSupport::Notifications](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) which provides simple hook points to instrument key parts of your application.
41274129

4128-
The following are currently supported:
41294130

4130-
#### endpoint_run.grape
4131+
#### Hook Points
4132+
4133+
The following hook points are currently supported:
4134+
4135+
##### endpoint_run.grape
41314136

41324137
The main execution of an endpoint, includes filters and rendering.
41334138

41344139
* *endpoint* - The endpoint instance
41354140

4136-
#### endpoint_render.grape
4141+
##### endpoint_render.grape
41374142

41384143
The execution of the main content block of the endpoint.
41394144

41404145
* *endpoint* - The endpoint instance
41414146

4142-
#### endpoint_run_filters.grape
4147+
##### endpoint_run_filters.grape
41434148

41444149
* *endpoint* - The endpoint instance
41454150
* *filters* - The filters being executed
41464151
* *type* - The type of filters (before, before_validation, after_validation, after)
41474152

4148-
#### endpoint_run_validators.grape
4153+
##### endpoint_run_validators.grape
41494154

41504155
The execution of validators.
41514156

41524157
* *endpoint* - The endpoint instance
41534158
* *validators* - The validators being executed
41544159
* *request* - The request being validated
41554160

4156-
#### format_response.grape
4161+
##### format_response.grape
41574162

41584163
Serialization or template rendering.
41594164

@@ -4162,12 +4167,44 @@ Serialization or template rendering.
41624167

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

4170+
#### Subscribe to Hooks
4171+
4172+
Once subscribed to the instrumentation, you can intercept the events reported above.
4173+
4174+
```ruby
4175+
ActiveSupport::Notifications.subscribe(/<api_path>/) do |name, start, finish, id, payload|
4176+
# your code to intercept the notification
4177+
end
4178+
```
4179+
4180+
The request data, the API’s internal data, and the response can be retrieved from the payload.
4181+
4182+
You can use `payload.fetch(:endpoint)` or directly `payload[:endpoint]`.
4183+
4184+
The `:endpoint` contains the data currently being processed, and access to attributes such as `body`, `request`, `params`, `headers`, `cookies` and `response_cookies`
4185+
4186+
For example, `payload[:endpoint].body` provides the current state of the response.
4187+
4188+
```ruby
4189+
ActiveSupport::Notifications.subscribe(/v1/) do |name, start, finish, id, payload|
4190+
hook_record = {
4191+
hook: name
4192+
status: payload[:env]&.dig("api.endpoint")&.status
4193+
format: payload[:env]&.dig("api.format")
4194+
body: payload[:endpoint]&.body
4195+
duration: (finish - start) * 1000
4196+
}
4197+
# your code to save the notification
4198+
end
4199+
```
4200+
41654201
### Monitoring Products
41664202

41674203
Grape integrates with following third-party tools:
41684204

41694205
* **New Relic** - [built-in support](https://docs.newrelic.com/docs/agents/ruby-agent/frameworks/grape-instrumentation) from v3.10.0 of the official [newrelic_rpm](https://github.com/newrelic/rpm) gem, also [newrelic-grape](https://github.com/xinminlabs/newrelic-grape) gem
41704206
* **Librato Metrics** - [grape-librato](https://github.com/seanmoon/grape-librato) gem
4207+
* **Rails Performance** - [rails_performance](https://github.com/igorkasyanchuk/rails_performance) gem
41714208
* **[Skylight](https://www.skylight.io/)** - [skylight](https://github.com/skylightio/skylight-ruby) gem, [documentation](https://docs.skylight.io/grape/)
41724209
* **[AppSignal](https://www.appsignal.com)** - [appsignal-ruby](https://github.com/appsignal/appsignal-ruby) gem, [documentation](http://docs.appsignal.com/getting-started/supported-frameworks.html#grape)
41734210
* **[ElasticAPM](https://www.elastic.co/products/apm)** - [elastic-apm](https://github.com/elastic/apm-agent-ruby) gem, [documentation](https://www.elastic.co/guide/en/apm/agent/ruby/3.x/getting-started-rack.html#getting-started-grape)

0 commit comments

Comments
 (0)