exception_dog 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4dba9c5920bdfc277211b1438d705ae428d140ac
4
+ data.tar.gz: 510cb625d0d1a6c954433786f20c71dd7ca4eaa5
5
+ SHA512:
6
+ metadata.gz: f93d08ba1f74f37f9953ddd2c1d643fffcccc9e35acc379a50b108573a2a337246bbc75e324b640955ae6b316cff565cc398eedb14df8d1735f244a5515be08e
7
+ data.tar.gz: 11b0eaa60a752fce420f0223ad185d08eca59ead627b0c8cdebf7255127f070d7b013441313cbddd7bf9361d5145ad362ca1a6052a2f1f23a0ec9822d6480a4e
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in exception_dog.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ exception_dog (0.1.0)
5
+ dogstatsd-ruby
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ dogstatsd-ruby (4.0.0)
15
+ hashdiff (0.3.7)
16
+ minitest (5.11.3)
17
+ public_suffix (3.0.3)
18
+ rake (10.5.0)
19
+ safe_yaml (1.0.4)
20
+ webmock (3.4.2)
21
+ addressable (>= 2.3.6)
22
+ crack (>= 0.3.2)
23
+ hashdiff
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ exception_dog!
31
+ minitest (~> 5.0)
32
+ rake (~> 10.0)
33
+ webmock
34
+
35
+ BUNDLED WITH
36
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Marcus Baguley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # ExceptionDog
2
+
3
+ Exception dog is a simple exception notifier gem that pushes exceptions out to Dotadog as metric events. You can set up rules within datadog to push new occurrences of the events into slack for instance, to have a reasonably good exception service.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'exception_dog'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ `exception_dog` can be configured to use the datadog agent or the public cloud API.
16
+ You can configure in an initialiser, for example: in a file `config/initializers/exception_dog.rb`
17
+
18
+ Agent Configuration Example
19
+ ```
20
+ ExceptionDog.configure do |config|
21
+ config.environment = ENV["RAILS_ENV"]
22
+ config.notifier = Rails.env.test? ? "ExceptionDog::LogNotifier" : "ExceptionDog::AgentNotifier"
23
+ config.agent_host = 'localhost'
24
+ config.agent_port = 8125
25
+ config.logger = Rails.logger
26
+ config.service_name = Rails.application.class.parent.name.underscore
27
+ end
28
+ ```
29
+
30
+ Cloud API configuration
31
+
32
+ ```
33
+ ExceptionDog.configure do |config|
34
+ config.environment = ENV["RAILS_ENV"]
35
+ config.api_key = ENV["DATA_DOG_API_KEY"]
36
+ config.notifier = Rails.env.test? ? "ExceptionDog::LogNotifier" : "ExceptionDog::HttpNotifier"
37
+ config.logger = Rails.logger
38
+ config.service_name = Rails.application.class.parent.name.underscore
39
+ end
40
+ ```
41
+
42
+ Middleware Configuration
43
+ You can set up a simple middleware to catch and report exceptions for Rack based apps.
44
+
45
+ ```
46
+ require 'exception_dog/integrations/rack'
47
+ Rails.application.config.middleware.insert_after ActionDispatch::ShowExceptions, ExceptionDog::Integrations::Rack
48
+ ```
49
+
50
+ ## Running tests
51
+
52
+ ```
53
+ ruby -Ilib -Itest test/*.rb
54
+ ```
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exception_dog"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "exception_dog/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "exception_dog"
8
+ spec.version = ExceptionDog::VERSION
9
+ spec.authors = ["Marcus Baguley"]
10
+ spec.email = ["marcus.baguley@gmail.com"]
11
+
12
+ spec.summary = %q{Wrapper for sending exceptions to datadog as events}
13
+ spec.description = %q{Wrapper for sending exceptions to datadog as events}
14
+ spec.homepage = "https://github.com/marcusbaguley/exception_dog"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "dogstatsd-ruby"
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "webmock"
29
+ end
@@ -0,0 +1,33 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'datadog/statsd'
5
+
6
+ module ExceptionDog
7
+ class AgentNotifier
8
+
9
+ attr_reader :configuration
10
+ attr_reader :logger
11
+
12
+ OPTS_WHITELIST = [:priority, :tags, :aggregation_key, :source_type_name, :alert_type]
13
+
14
+ def initialize(configuration)
15
+ @configuration = configuration
16
+ @logger = configuration.logger
17
+ end
18
+
19
+ def notify(title, text, opts)
20
+ logger.info "ExceptionDog::send_to_agent"
21
+ @@socket ||= Datadog::Statsd.new(configuration.agent_host, configuration.agent_port, logger: configuration.logger)
22
+ response = @@socket.event(title, text, opts)
23
+ end
24
+
25
+ def errors
26
+ @errors = []
27
+ @errors << "Invalid host setup" unless configuration.agent_port && configuration.agent_host
28
+ @errors
29
+ end
30
+
31
+ end
32
+ end
33
+
@@ -0,0 +1,55 @@
1
+ module ExceptionDog
2
+ class Handler
3
+ MAX_LINE_LENGTH = 80
4
+ MAX_TITLE_LENGTH = 100
5
+ MAX_TEXT_LEGNTH = 4000
6
+ BACKTRACE_LINES = 7
7
+ attr_reader :configuration
8
+ attr_reader :logger
9
+
10
+ def initialize(configuration)
11
+ @configuration = configuration
12
+ @logger = @configuration.logger
13
+ @notifier = Object.const_get(@configuration.notifier).new(configuration)
14
+ end
15
+
16
+
17
+ def notify(exception, data)
18
+ attach_dd_trace_id(data) if self.class.dd_trace_enabled
19
+ title = exception.message[0..MAX_TITLE_LENGTH]
20
+ text = exception_text(exception, data)[0..MAX_TEXT_LEGNTH]
21
+ opts = {}
22
+ opts[:priority] ||= 'normal'
23
+ opts[:tags] = ["environment:#{configuration.environment}", "service:#{configuration.service_name}"] + configuration.tags
24
+ opts[:aggregation_key] = aggregation_key(exception)
25
+ opts[:source_type_name] = configuration.source_type_name
26
+ opts[:alert_type] = 'error'
27
+ @notifier.notify(title, text, opts)
28
+ end
29
+
30
+ def exception_text(exception, data)
31
+ detail = [exception.class.name[0..MAX_LINE_LENGTH], exception.message[0..MAX_LINE_LENGTH]]
32
+ data.each do |key, val|
33
+ detail << "#{key}: #{val && val.to_s[0..MAX_LINE_LENGTH]}"
34
+ end
35
+ (detail + (exception.backtrace || []))[0..BACKTRACE_LINES].compact.join("\n")
36
+ end
37
+
38
+ def aggregation_key(exception)
39
+ "#{exception.class.name}-#{exception.message}-#{exception.backtrace&.first}".hash.to_s
40
+ end
41
+
42
+ def attach_dd_trace_id(data)
43
+ data[:trace_id] = self.class.current_trace_id
44
+ end
45
+
46
+ def self.current_trace_id
47
+ context = Thread.current[:datadog_context]
48
+ context&.trace_id
49
+ end
50
+
51
+ def self.dd_trace_enabled
52
+ @dd_trace_enabled ||= Object.const_get('Datadog::Context') rescue false
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,43 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'datadog/statsd'
5
+
6
+ module ExceptionDog
7
+ class HttpNotifier
8
+
9
+ attr_reader :configuration
10
+ attr_reader :logger
11
+
12
+ def initialize(configuration)
13
+ @configuration = configuration
14
+ @logger = configuration.logger
15
+ end
16
+
17
+ def notify(title, text, opts)
18
+ uri = URI.parse("https://api.datadoghq.com/api/v1/events?api_key=#{configuration.api_key}")
19
+ logger = configuration.logger
20
+ header = {'Content-Type': 'application/json'}
21
+ http = Net::HTTP.new(uri.host, uri.port)
22
+ http.use_ssl = true
23
+ request = Net::HTTP::Post.new(uri.request_uri, header)
24
+ request.body = {title: title, text: text}.merge(opts).to_json
25
+ logger.info "ExceptionDog::send_to_api"
26
+ begin
27
+ response = http.request(request)
28
+ logger.debug response.body if response.respond_to?(:body)
29
+ logger.debug "ExceptionDog:Response: #{response.inspect}"
30
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
31
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
32
+ logger.error("ExceptionDog::Failed to send to datadog")
33
+ logger.error(e)
34
+ end
35
+ end
36
+
37
+ def errors
38
+ @errors = []
39
+ @errors << "Invalid API Key" unless configuration.api_key && configuration.api_key =~ /[0-9a-f]{32}/i
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,21 @@
1
+ module ExceptionDog::Integrations
2
+ class Rack
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ begin
9
+ response = @app.call(env)
10
+ rescue Exception => raised
11
+ data = {
12
+ request_uri: env["REQUEST_URI"],
13
+ remote_ip: env["HTTP_X_FORWARDED_FOR"] || env["HTTP_FORWARDED_FOR"]
14
+ }
15
+ ExceptionDog.notify(raised, data)
16
+ raise raised
17
+ end
18
+ response
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'datadog/statsd'
5
+
6
+ module ExceptionDog
7
+ class LogNotifier
8
+
9
+ attr_reader :configuration
10
+ attr_reader :logger
11
+
12
+ def initialize(configuration)
13
+ @configuration = configuration
14
+ @logger = configuration.logger
15
+ end
16
+
17
+ def notify(title, text, opts)
18
+ logger.info "#{title}, #{text}, #{opts}"
19
+ @@last_log = [title, text, opts]
20
+ end
21
+
22
+ def self.last_log
23
+ @@last_log
24
+ end
25
+
26
+ def errors
27
+ []
28
+ end
29
+
30
+ end
31
+ end
32
+
@@ -0,0 +1,3 @@
1
+ module ExceptionDog
2
+ VERSION = "0.3.2"
3
+ end
@@ -0,0 +1,74 @@
1
+ require "rubygems"
2
+
3
+ require "exception_dog/version"
4
+ require "exception_dog/agent_notifier"
5
+ require "exception_dog/http_notifier"
6
+ require "exception_dog/log_notifier"
7
+ require "exception_dog/handler"
8
+
9
+ module ExceptionDog
10
+ class Configuration
11
+ attr_accessor :api_key
12
+ attr_accessor :app_name
13
+ attr_accessor :source_type_name
14
+ attr_accessor :alert_type
15
+ attr_accessor :environment
16
+ attr_accessor :logger
17
+ attr_accessor :service_name
18
+ attr_accessor :tags
19
+ attr_accessor :test_mode
20
+ attr_accessor :agent_host
21
+ attr_accessor :agent_port
22
+ attr_accessor :notifier
23
+ attr_accessor :notifier_instance
24
+
25
+ def initialize
26
+ self.source_type_name = 'my_apps'
27
+ self.alert_type = 'error'
28
+ self.environment = 'prod'
29
+ self.test_mode = false
30
+ self.agent_host = 'localhost'
31
+ self.agent_port = 8125
32
+ self.tags = []
33
+ self.logger = Logger.new(STDOUT)
34
+ end
35
+
36
+ def errors
37
+ @errors = []
38
+ @errors << "No service_name supplied" unless service_name
39
+ @errors << "No notifier configured" unless notifier
40
+ @errors
41
+ end
42
+
43
+ def valid?
44
+ errors.empty?
45
+ end
46
+ end
47
+
48
+ class << self
49
+
50
+ def configuration
51
+ @configuration
52
+ end
53
+
54
+ def configure
55
+ @configuration = Configuration.new
56
+ yield @configuration
57
+ @configuration.logger ||= Logger.new(STDOUT)
58
+ if !configuration.valid?
59
+ @configuration.logger.error "Invalid ExceptionDog config #{configuration.errors.inspect}"
60
+ @configuration.notifier = "LogNotifier"
61
+ end
62
+ @handler = Handler.new(configuration)
63
+ @configuration
64
+ end
65
+
66
+ def notify(exception, data = {})
67
+ @handler.notify(exception, data)
68
+ end
69
+
70
+ def default_hostname
71
+ Socket.gethostname;
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exception_dog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Marcus Baguley
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dogstatsd-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Wrapper for sending exceptions to datadog as events
84
+ email:
85
+ - marcus.baguley@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - exception_dog.gemspec
100
+ - lib/exception_dog.rb
101
+ - lib/exception_dog/agent_notifier.rb
102
+ - lib/exception_dog/handler.rb
103
+ - lib/exception_dog/http_notifier.rb
104
+ - lib/exception_dog/integrations/rack.rb
105
+ - lib/exception_dog/log_notifier.rb
106
+ - lib/exception_dog/version.rb
107
+ homepage: https://github.com/marcusbaguley/exception_dog
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.5.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Wrapper for sending exceptions to datadog as events
131
+ test_files: []