Skip to content

Commit

Permalink
Added the ability to send stash for resources
Browse files Browse the repository at this point in the history
This stash can be accessed from performance filters.
  • Loading branch information
kyrylo committed May 9, 2019
1 parent 9fdcbf3 commit 870b680
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 6 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ Airbrake Ruby Changelog

### master

* Added `Airbrake::Query#stash`, `Airbrake::Request#stash`,
`Airbrake::PerformanceBreakdown#stash` that allows storing arbitrary
information in these structures
([#481](https://github.com/airbrake/airbrake-ruby/pull/481))
* Added the ability to attach objects to stash to `Airbrake.notify_query`,
`Airbrake.notify_request`, `Airbrake.notify_performance_breakdown`
([#481](https://github.com/airbrake/airbrake-ruby/pull/481))

Example:
```ruby
query_info = { query: '...', ...}
stash = { request_id: 123 }
Airbrake.notify_query(query_info, stash)
```

This stash can be accessed from performance filters.

### [v4.3.0][v4.3.0] (April 30, 2019)

* Added `Airbrake::TimedTrace` for measuring performance of arbitrary code
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,22 @@ Airbrake.notify_request(
)
```


Optionally, you can attach information to the stash (`request_id` in this
example).

```ruby
Airbrake.notify_request(
{
# normal params
},
request_id: 123
)
```

This stash can be accessed from performance filters as
`resource.stash[:request_id]`.

When [`config.performance_stats = false`](#performance_stats), it always returns
a rejected promise.

Expand All @@ -752,6 +768,21 @@ Airbrake.notify_query(
)
```

Optionally, you can attach information to the stash (`request_id` in this
example).

```ruby
Airbrake.notify_query(
{
# normal params
},
request_id: 123
)
```

This stash can be accessed from performance filters as
`resource.stash[:request_id]`.

When [`config.performance_stats = false`](#performance_stats), it always returns
a rejected promise.

Expand All @@ -774,6 +805,27 @@ Airbrake.notify_performance_breakdown(
)
```

Optionally, you can attach information to the stash (`request_id` in this
example).

```ruby
Airbrake.notify_performance_breakdown(
{
# normal params
},
request_id: 123
)
```

This stash can be accessed from performance filters as
`resource.stash[:request_id]`.

When [`config.performance_stats = false`](#performance_stats), it always returns
a rejected promise.

When [`config.performance_stats = true`](#performance_stats), then it aggregates
statistics and sends as a batch every 15 seconds.

#### Airbrake.add_performance_filter

Adds a performance filter that filters performance data. Works exactly like
Expand Down
24 changes: 18 additions & 6 deletions lib/airbrake-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,15 @@ def merge_context(context)
# (optional)
# @option request_info [Date] :start_time When the request started
# @option request_info [Time] :end_time When the request ended (optional)
# @param [Hash] stash What needs to be appeneded to the stash, so it's
# available in filters
# @return [void]
# @since v3.0.0
# @see Airbrake::PerformanceNotifier#notify
def notify_request(request_info)
performance_notifier.notify(Request.new(request_info))
def notify_request(request_info, stash = {})
request = Request.new(request_info)
request.stash.merge!(stash)
performance_notifier.notify(request)
end

# Increments SQL statistics of a certain +query+ that was invoked on
Expand All @@ -378,11 +382,15 @@ def notify_request(request_info)
# @option request_info [String] :query The query that was executed
# @option request_info [Date] :start_time When the query started executing
# @option request_info [Time] :end_time When the query finished (optional)
# @param [Hash] stash What needs to be appeneded to the stash, so it's
# available in filters
# @return [void]
# @since v3.2.0
# @see Airbrake::PerformanceNotifier#notify
def notify_query(query_info)
performance_notifier.notify(Query.new(query_info))
def notify_query(query_info, stash = {})
query = Query.new(query_info)
query.stash.merge!(stash)
performance_notifier.notify(query)
end

# Increments performance breakdown statistics of a certain route.
Expand All @@ -403,10 +411,14 @@ def notify_query(query_info)
# @option breakdown_info [String] :response_type
# @option breakdown_info [Array<Hash{Symbol=>Float}>] :groups
# @option breakdown_info [Date] :start_time
# @param [Hash] stash What needs to be appeneded to the stash, so it's
# available in filters
# @return [void]
# @since v4.2.0
def notify_performance_breakdown(breakdown_info)
performance_notifier.notify(PerformanceBreakdown.new(breakdown_info))
def notify_performance_breakdown(breakdown_info, stash = {})
performance_breakdown = PerformanceBreakdown.new(breakdown_info)
performance_breakdown.stash.merge!(stash)
performance_notifier.notify(performance_breakdown)
end

# Runs a callback before {.notify_request} or {.notify_query} kicks in. This
Expand Down
108 changes: 108 additions & 0 deletions spec/airbrake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,114 @@
end
end

describe "#notify_request" do
context "when :stash key is not provided" do
it "doesn't add anything to the stash of the request" do
expect(described_class.performance_notifier).to receive(:notify) do |request|
expect(request.stash).to be_empty
end

described_class.notify_request(
method: 'GET',
route: '/',
status_code: 200,
start_time: Time.now
)
end
end

context "when :stash key is provided" do
it "adds the value as the stash of the request" do
expect(described_class.performance_notifier).to receive(:notify) do |request|
expect(request.stash).to eq(request_id: 1)
end

described_class.notify_request(
{
method: 'GET',
route: '/',
status_code: 200,
start_time: Time.now
},
request_id: 1
)
end
end
end

describe "#notify_query" do
context "when :stash key is not provided" do
it "doesn't add anything to the stash of the query" do
expect(described_class.performance_notifier).to receive(:notify) do |query|
expect(query.stash).to be_empty
end

described_class.notify_query(
method: 'GET',
route: '/',
query: '',
start_time: Time.now
)
end
end

context "when :stash key is provided" do
it "adds the value as the stash of the query" do
expect(described_class.performance_notifier).to receive(:notify) do |query|
expect(query.stash).to eq(request_id: 1)
end

described_class.notify_query(
{
method: 'GET',
route: '/',
query: '',
start_time: Time.now
},
request_id: 1
)
end
end
end

describe "#notify_performance_breakdown" do
context "when :stash key is not provided" do
it "doesn't add anything to the stash of the performance breakdown" do
expect(described_class.performance_notifier).to receive(:notify) do |query|
expect(query.stash).to be_empty
end

described_class.notify_query(
method: 'GET',
route: '/',
query: '',
start_time: Time.now
)
end
end

context "when :stash key is provided" do
it "adds the value as the stash of the performance breakdown" do
expect(
described_class.performance_notifier
).to receive(:notify) do |performance_breakdown|
expect(performance_breakdown.stash).to eq(request_id: 1)
end

described_class.notify_performance_breakdown(
{
method: 'GET',
route: '/',
response_type: :html,
groups: {},
start_time: Time.now
},
request_id: 1
)
end
end
end

describe ".performance_notifier" do
it "returns a performance notifier" do
expect(described_class.performance_notifier)
Expand Down

0 comments on commit 870b680

Please sign in to comment.