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

filters: add optional DependencyFilter #328

Merged
merged 1 commit into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Airbrake Ruby Changelog

* Added the `versions` option
([#327](https://github.com/airbrake/airbrake-ruby/pull/327))
* Added `DependencyFilter` (optional)
([#328](https://github.com/airbrake/airbrake-ruby/pull/328))

### [v2.9.0][v2.9.0] (April 26, 2018)

Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Key features
* Severity support<sup>[[link](#setting-severity)]</sup>
* Support for code hunks (lines of code surrounding each backtrace frame)<sup>[[link](#code_hunks)]</sup>
* Ability to add context to reported exceptions<sup>[[link](#airbrakemerge_context)]</sup>
* Dependency tracking<sup>[[link](#airbrakefiltersdependencyfilter)]</sup>
* Last but not least, we follow semantic versioning 2.0.0<sup>[[link][semver2]]</sup>

Installation
Expand Down Expand Up @@ -568,14 +569,23 @@ every type of application, so there's no need to include them only to clutter
the notice object. You have to include such filters manually, if you think you
need them. The list of optional filters include:

* Airbrake::Filters::ThreadFilter
###### Airbrake::Filters::ThreadFilter

Adding them is as easy as:
Attaches thread & fiber local variables along with general thread information.

```ruby
Airbrake.add_filter(Airbrake::Filters::ThreadFilter.new)
```

###### Airbrake::Filters::DependencyFilter

Attaches loaded dependencies to the notice object (under
`context/versions/dependencies`).

```ruby
Airbrake.add_filter(Airbrake::Filters::DependencyFilter.new)
```

##### Using classes for building filters

For more complex filters you can use the special API. Simply pass an object that
Expand Down
1 change: 1 addition & 0 deletions lib/airbrake-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require 'airbrake-ruby/filters/thread_filter'
require 'airbrake-ruby/filters/context_filter'
require 'airbrake-ruby/filters/exception_attributes_filter'
require 'airbrake-ruby/filters/dependency_filter'
require 'airbrake-ruby/filter_chain'
require 'airbrake-ruby/notifier'
require 'airbrake-ruby/code_hunk'
Expand Down
27 changes: 27 additions & 0 deletions lib/airbrake-ruby/filters/dependency_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Airbrake
module Filters
# Attaches loaded dependencies to the notice object.
class DependencyFilter
def initialize
@weight = 117
end

def call(notice)
deps = {}
Gem.loaded_specs.map.with_object(deps) do |(name, spec), h|
h[name] = "#{spec.version}#{git_version(spec)}"
end

notice[:context][:versions] = {} unless notice[:context].key?(:versions)
notice[:context][:versions][:dependencies] = deps
end

private

def git_version(spec)
return unless spec.respond_to?(:git_version) || spec.git_version
spec.git_version.to_s
end
end
end
end
16 changes: 16 additions & 0 deletions spec/filters/dependency_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

RSpec.describe Airbrake::Filters::DependencyFilter do
let(:notice) do
Airbrake::Notice.new(Airbrake::Config.new, AirbrakeTestError.new)
end

describe "#call" do
it "attaches loaded dependencies to context/versions/dependencies" do
subject.call(notice)
expect(notice[:context][:versions][:dependencies]).to include(
'airbrake-ruby' => Airbrake::AIRBRAKE_RUBY_VERSION
)
end
end
end