Skip to content

Commit

Permalink
filters: add optional DependencyFilter
Browse files Browse the repository at this point in the history
The filter allows keeping track of the app's dependencies.
  • Loading branch information
kyrylo committed May 2, 2018
1 parent 6c262eb commit d39d2f6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
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
17 changes: 15 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,26 @@ 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)
```

Adding them is as easy as:


##### 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

0 comments on commit d39d2f6

Please sign in to comment.