pliny 0.16.3 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 548c79d9c73c40bd3d9564f62ac43fafe8258720
4
- data.tar.gz: 163711ca7aa133303c6fd3c31d47444d7880b14a
3
+ metadata.gz: 9a23d5c9332e98aa75fbe6d56ec858314aeafe88
4
+ data.tar.gz: 832352cd2b15849d452783fa87638b4d8fc7fa68
5
5
  SHA512:
6
- metadata.gz: a21bb401dceadc0331e47a3b0484609a69b890c3e3ea03ab671888fa5993f252c7dc9a4d186717b80c20caa1565b86b3599017037f2343acad9bff5ef7050187
7
- data.tar.gz: 2e4571958fa398ac04e7db0ff797a90fe242756d816a0f107735446e1c2c37db2c9a74a4b78804c08c9309394c0b7ffee3210cb723b181df3ed43520e0299ff5
6
+ metadata.gz: caef1b86bf0355f08804ff6d771dc3fb93f8520fc77332a5dd1dd25aad319d70802356198772a12cc70191f0118a6bf1747c50f0dd0362f958b2ff674e828260
7
+ data.tar.gz: 4253d293b4fac10cdd17d4291ffb77e44fa11c7fd5e31ada8caa0ce15997a7825eb86ad2b779e8658d7ee7e195cb47c5693fc72859dde4f79c3daa8ee2019ea0
data/lib/pliny.rb CHANGED
@@ -2,6 +2,7 @@ require "multi_json"
2
2
  require "sinatra/base"
3
3
 
4
4
  require_relative "pliny/version"
5
+ require_relative "pliny/error_reporter"
5
6
  require_relative "pliny/errors"
6
7
  require_relative "pliny/helpers/encode"
7
8
  require_relative "pliny/helpers/params"
@@ -0,0 +1,42 @@
1
+ require 'rollbar/exception_reporter'
2
+ require 'rollbar/request_data_extractor'
3
+
4
+ class Pliny::ErrorReporter
5
+ def self.notify(exception, context: {}, rack_env: {})
6
+ Pliny.log_exception(exception)
7
+
8
+ REPORTERS.each do |reporter|
9
+ begin
10
+ reporter.new.notify(exception, context: context, rack_env: rack_env)
11
+ rescue
12
+ Pliny.log_exception($!)
13
+ end
14
+ end
15
+ end
16
+
17
+ class RollbarReporter
18
+ include ::Rollbar::ExceptionReporter
19
+ include ::Rollbar::RequestDataExtractor
20
+
21
+ def notify(exception, context:, rack_env:)
22
+ Rollbar.reset_notifier!
23
+ scope = fetch_scope(context: context, rack_env: rack_env)
24
+ Rollbar.scoped(scope) do
25
+ report_exception_to_rollbar(rack_env, exception)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def fetch_scope(context:, rack_env:)
32
+ {
33
+ request: proc { extract_request_data_from_rack(rack_env) }
34
+ }
35
+ rescue Exception => e
36
+ report_exception_to_rollbar(rack_env, e)
37
+ raise
38
+ end
39
+ end
40
+
41
+ REPORTERS = [RollbarReporter]
42
+ end
data/lib/pliny/log.rb CHANGED
@@ -1,8 +1,34 @@
1
1
  module Pliny
2
2
  module Log
3
3
  def log(data, &block)
4
- data = default_context.merge(log_context.merge(local_context.merge(data)))
5
- log_to_stream(stdout || $stdout, data, &block)
4
+ log_to_stream(stdout || $stdout, merge_log_contexts(data), &block)
5
+ end
6
+
7
+ def log_exception(e, data = {})
8
+ exception_id = e.object_id
9
+
10
+ # Log backtrace in reverse order for easier digestion.
11
+ if e.backtrace
12
+ e.backtrace.reverse.each do |backtrace|
13
+ log_to_stream(stderr || $stderr, merge_log_contexts(
14
+ exception_id: exception_id,
15
+ backtrace: backtrace
16
+ ))
17
+ end
18
+ end
19
+
20
+ # then log the exception message last so that it's as close to the end of
21
+ # a log trace as possible
22
+ data.merge!(
23
+ exception: true,
24
+ class: e.class.name,
25
+ message: e.message,
26
+ exception_id: exception_id
27
+ )
28
+
29
+ data[:status] = e.status if e.respond_to?(:status)
30
+
31
+ log_to_stream(stderr || $stderr, merge_log_contexts(data))
6
32
  end
7
33
 
8
34
  def context(data, &block)
@@ -30,8 +56,20 @@ module Pliny
30
56
  @stdout
31
57
  end
32
58
 
59
+ def stderr=(stream)
60
+ @stderr = stream
61
+ end
62
+
63
+ def stderr
64
+ @stderr
65
+ end
66
+
33
67
  private
34
68
 
69
+ def merge_log_contexts(data)
70
+ default_context.merge(log_context.merge(local_context.merge(data)))
71
+ end
72
+
35
73
  def local_context
36
74
  RequestStore.store[:local_context] ||= {}
37
75
  end
@@ -8,25 +8,12 @@ module Pliny::Middleware
8
8
  def call(env)
9
9
  @app.call(env)
10
10
  rescue Pliny::Errors::Error => e
11
- # blank out this field so that the error is not picked up by Rollbar
12
- env["sinatra.error"] = nil
13
-
14
11
  Pliny::Errors::Error.render(e)
15
- rescue Exception => e
16
- if @raise
17
- raise
18
- else
19
- dump_error(e, env)
20
- Pliny::Errors::Error.render(Pliny::Errors::InternalServerError.new)
21
- end
22
- end
23
-
24
- private
12
+ rescue => e
13
+ raise if @raise
25
14
 
26
- # pulled from Sinatra
27
- def dump_error(e, env)
28
- message = ["#{e.class} - #{e.message}:", *e.backtrace].join("\n\t")
29
- env['rack.errors'].puts(message)
15
+ Pliny::ErrorReporter.notify(e, rack_env: env)
16
+ Pliny::Errors::Error.render(Pliny::Errors::InternalServerError.new)
30
17
  end
31
18
  end
32
19
  end
data/lib/pliny/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.16.3"
2
+ VERSION = "0.17.0"
3
3
  end
@@ -1 +1 @@
1
- 2.3.0
1
+ 2.3.1
data/lib/template/Gemfile CHANGED
@@ -1,17 +1,17 @@
1
1
  source "https://rubygems.org"
2
- ruby "2.3.0"
2
+ ruby "2.3.1"
3
3
 
4
4
  gem "multi_json"
5
5
  gem "oj"
6
6
  gem "pg"
7
- gem "pliny", "~> 0.16"
7
+ gem "pliny", "~> 0.17"
8
8
  gem "pry"
9
- gem "puma", "~> 2.15"
9
+ gem "puma", "~> 2.16"
10
10
  gem "rack-ssl"
11
11
  gem "rack-timeout", "~> 0.4"
12
12
  gem "rake"
13
13
  gem "rollbar", require: "rollbar/middleware/sinatra"
14
- gem "sequel", "~> 4.30"
14
+ gem "sequel", "~> 4.34"
15
15
  gem "sequel-paranoid"
16
16
  gem "sequel_pg", "~> 1.6", require: "sequel"
17
17
  gem "sinatra", "~> 1.4", require: "sinatra/base"
@@ -1,5 +1,6 @@
1
1
  require_relative 'lib/application'
2
2
 
3
3
  $stdout.sync = true
4
+ $stderr.sync = true
4
5
 
5
6
  run Routes
@@ -14,23 +14,22 @@ module Config
14
14
 
15
15
  # Optional -- value is returned or `nil` if it wasn't present.
16
16
  optional :app_name, string
17
- optional :placeholder, string
18
17
  optional :versioning_default, string
19
18
  optional :versioning_app_name, string
20
19
 
21
20
  # Override -- value is returned or the set default.
22
- override :database_timeout, 10, int
23
- override :db_pool, 5, int
21
+ override :database_timeout, 10, int
22
+ override :db_pool, 5, int
24
23
  override :deployment, 'production', string
25
- override :force_ssl, true, bool
26
- override :pliny_env, 'development', string
27
- override :port, 5000, int
28
- override :pretty_json, false, bool
29
- override :puma_max_threads, 16, int
30
- override :puma_min_threads, 1, int
31
- override :puma_workers, 3, int
32
- override :raise_errors, false, bool
24
+ override :force_ssl, true, bool
25
+ override :pliny_env, 'production', string
26
+ override :port, 5000, int
27
+ override :pretty_json, false, bool
28
+ override :puma_max_threads, 16, int
29
+ override :puma_min_threads, 1, int
30
+ override :puma_workers, 3, int
31
+ override :raise_errors, false, bool
33
32
  override :root, File.expand_path("../../", __FILE__), string
34
- override :timeout, 10, int
35
- override :versioning, false, bool
33
+ override :timeout, 10, int
34
+ override :versioning, false, bool
36
35
  end
@@ -1,5 +1,6 @@
1
1
  Rollbar.configure do |config|
2
2
  config.enabled = ENV.has_key?('ROLLBAR_ACCESS_TOKEN')
3
+ config.disable_rack_monkey_patch = true
3
4
  config.access_token = ENV["ROLLBAR_ACCESS_TOKEN"]
4
5
  config.environment = ENV['ROLLBAR_ENV']
5
6
  config.logger = Pliny::RollbarLogger.new
@@ -1,5 +1,4 @@
1
1
  Routes = Rack::Builder.new do
2
- use Rollbar::Middleware::Sinatra
3
2
  use Pliny::Middleware::CORS
4
3
  use Pliny::Middleware::RequestID
5
4
  use Pliny::Middleware::Instruments
@@ -0,0 +1,24 @@
1
+
2
+ require "spec_helper"
3
+
4
+ describe Pliny::ErrorReporter do
5
+ subject(:reporter) { described_class }
6
+
7
+ describe ".notify" do
8
+ let(:exception) { RuntimeError.new }
9
+ let(:context) { { context: "foo" } }
10
+ let(:rack_env) { { rack_env: "bar" } }
11
+
12
+ subject(:notify_reporter) do
13
+ reporter.notify(exception, context: context, rack_env: rack_env)
14
+ end
15
+
16
+ it "notifies rollbar" do
17
+ any_instance_of(Pliny::ErrorReporter::RollbarReporter) do |klass|
18
+ stub(klass).notify(exception, context: context, rack_env: rack_env)
19
+ end
20
+
21
+ notify_reporter
22
+ end
23
+ end
24
+ end
data/spec/log_spec.rb CHANGED
@@ -4,6 +4,7 @@ describe Pliny::Log do
4
4
  before do
5
5
  @io = StringIO.new
6
6
  Pliny.stdout = @io
7
+ Pliny.stderr = @io
7
8
  stub(@io).print
8
9
  end
9
10
 
@@ -73,4 +74,11 @@ describe Pliny::Log do
73
74
  end
74
75
  Pliny.log(foo: "bar")
75
76
  end
77
+
78
+ it "logs exceptions" do
79
+ Pliny::RequestStore.store[:log_context] = { app: "pliny" }
80
+ e = RuntimeError.new
81
+ mock(@io).print "app=pliny exception class=RuntimeError message=RuntimeError exception_id=#{e.object_id}\n"
82
+ Pliny.log_exception(e)
83
+ end
76
84
  end
@@ -28,6 +28,7 @@ describe Pliny::Middleware::RescueErrors do
28
28
 
29
29
  it "intercepts exceptions and renders" do
30
30
  @app = new_rack_app
31
+ mock(Pliny::ErrorReporter).notify.with_any_args
31
32
  get "/"
32
33
  assert_equal 500, last_response.status
33
34
  error_json = MultiJson.decode(last_response.body)
data/spec/spec_helper.rb CHANGED
@@ -43,3 +43,4 @@ RSpec.configure do |config|
43
43
  end
44
44
 
45
45
  Pliny.stdout = StringIO.new
46
+ Pliny.stderr = StringIO.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pliny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.3
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur Leach
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-04 00:00:00.000000000 Z
12
+ date: 2016-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -305,6 +305,26 @@ dependencies:
305
305
  - - ">="
306
306
  - !ruby/object:Gem::Version
307
307
  version: 0.17.1
308
+ - !ruby/object:Gem::Dependency
309
+ name: rollbar
310
+ requirement: !ruby/object:Gem::Requirement
311
+ requirements:
312
+ - - "~>"
313
+ - !ruby/object:Gem::Version
314
+ version: '2.11'
315
+ - - ">="
316
+ - !ruby/object:Gem::Version
317
+ version: 2.11.0
318
+ type: :development
319
+ prerelease: false
320
+ version_requirements: !ruby/object:Gem::Requirement
321
+ requirements:
322
+ - - "~>"
323
+ - !ruby/object:Gem::Version
324
+ version: '2.11'
325
+ - - ">="
326
+ - !ruby/object:Gem::Version
327
+ version: 2.11.0
308
328
  - !ruby/object:Gem::Dependency
309
329
  name: sequel
310
330
  requirement: !ruby/object:Gem::Requirement
@@ -354,6 +374,7 @@ files:
354
374
  - lib/pliny/commands/updater.rb
355
375
  - lib/pliny/config_helpers.rb
356
376
  - lib/pliny/db_support.rb
377
+ - lib/pliny/error_reporter.rb
357
378
  - lib/pliny/errors.rb
358
379
  - lib/pliny/helpers/encode.rb
359
380
  - lib/pliny/helpers/params.rb
@@ -438,6 +459,7 @@ files:
438
459
  - spec/commands/updater_spec.rb
439
460
  - spec/config_helpers_spec.rb
440
461
  - spec/db_support_spec.rb
462
+ - spec/error_reporter_spec.rb
441
463
  - spec/errors_spec.rb
442
464
  - spec/helpers/encode_spec.rb
443
465
  - spec/helpers/serialize_spec.rb