pliny 0.19.0 → 0.20.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d42dcfb555d6af54e5e87035c53d5fbb9de11cfe
4
- data.tar.gz: 9b498dbf53638dbc04770f93df95614fffa59591
3
+ metadata.gz: 686ea485950159a4111ea7d2a0cf08d1a9945a57
4
+ data.tar.gz: 600951ff54aabd3e2cc39094b305daed9b1fb284
5
5
  SHA512:
6
- metadata.gz: 9d85fa248efc6f4e4de6d93107a312c9a67200033f633133985246e43603a1e749eefc1175185ea4d4ea1df8ec516d14ce554ea952513192e61190b29615d1fb
7
- data.tar.gz: 0f96306f19f94bff354d95b0e4adf8f512aff8577a29fadb786fee3fc31e9462fe6c4f46b0285d27c952e9df78f7a7256d0a24eb83bfdb1422e98ab85ea26982
6
+ metadata.gz: 8f8964c9b05a857ae80961fba8551eaa8634f864faad1dc7f3305e73fbc4aeb63663e1ded6fbe7ac585042542a50d92cb7ec62b836037586d0413b17de7d512e
7
+ data.tar.gz: 5cf995f52ea9273783ec42cc4f19cb57fa7e762a6ec24cab57ca98b5653f0fcdc404f83529bebde057e95d1040ff39a58ffe3aa07f22e45953fe8831b491201c
@@ -8,6 +8,7 @@ require_relative "pliny/helpers/encode"
8
8
  require_relative "pliny/helpers/params"
9
9
  require_relative "pliny/helpers/serialize"
10
10
  require_relative "pliny/log"
11
+ require_relative "pliny/metrics/backends/logger"
11
12
  require_relative "pliny/metrics"
12
13
  require_relative "pliny/request_store"
13
14
  require_relative "pliny/rollbar_logger"
@@ -50,13 +50,23 @@ module Pliny
50
50
  end
51
51
 
52
52
  def rack_env
53
- if pliny_env == "development" || pliny_env == "test"
53
+ if env == "development" || env == "test"
54
54
  "development"
55
55
  else
56
56
  "deployment"
57
57
  end
58
58
  end
59
59
 
60
+ # DEPRECATED: pliny_env is deprecated in favour of app_env.
61
+ # See more at https://github.com/interagent/pliny/issues/277
62
+ #
63
+ # This method is kept temporary in case it is used somewhere in the app.
64
+ def pliny_env
65
+ warn "Config.pliny_env is deprecated and will be removed, " \
66
+ "use Config.app_env instead."
67
+ env
68
+ end
69
+
60
70
  private
61
71
 
62
72
  def cast(value, method)
@@ -70,6 +80,20 @@ module Pliny
70
80
  instance_eval "def #{name}?; !!@#{name} end", __FILE__, __LINE__
71
81
  end
72
82
  end
83
+
84
+ # This method helps with transition from PLINY_ENV to APP_ENV.
85
+ def env
86
+ legacy_env || app_env
87
+ end
88
+
89
+ # PLINY_ENV is deprecated, but it might be still used by someone.
90
+ def legacy_env
91
+ if ENV.key?('PLINY_ENV')
92
+ warn "PLINY_ENV is deprecated in favour of APP_ENV, " \
93
+ "update .env file or application configuration."
94
+ ENV['PLINY_ENV']
95
+ end
96
+ end
73
97
  end
74
98
 
75
99
  module ConfigHelpers
@@ -1,29 +1,44 @@
1
1
  module Pliny
2
2
  module Metrics
3
- def self.count(*names, value: 1)
4
- counts = Hash[names.map { |n| [metric_prefix(:count, n), value] }]
5
- Pliny.log(counts)
3
+ extend self
4
+
5
+ attr_accessor :backends
6
+
7
+ @backends = [Backends::Logger]
8
+
9
+ def count(*names, value: 1)
10
+ counts = Hash[names.map { |n| ["#{Config.app_name}.#{n}", value] }]
11
+
12
+ backends.each do |backend|
13
+ report_and_catch { backend.report_counts(counts) }
14
+ end
15
+
16
+ counts
6
17
  end
7
18
 
8
- def self.measure(*names, &block)
19
+ def measure(*names, &block)
9
20
  elapsed, return_value = time_elapsed(&block)
10
- measures = Hash[names.map { |n| [metric_prefix(:measure, n), elapsed] }]
11
- Pliny.log(measures)
21
+ measures = Hash[names.map { |n| ["#{Config.app_name}.#{n}", elapsed] }]
22
+
23
+ backends.each do |backend|
24
+ report_and_catch { backend.report_measures(measures) }
25
+ end
12
26
 
13
27
  return_value
14
28
  end
15
29
 
16
30
  private
17
31
 
18
- def self.metric_prefix(type, name)
19
- "#{type.to_s}##{Config.app_name}.#{name}"
20
- end
21
-
22
- def self.time_elapsed(&block)
32
+ def time_elapsed(&block)
23
33
  start = Time.now
24
34
  return_value = block.call
25
35
  [Time.now - start, return_value]
26
36
  end
37
+
38
+ def report_and_catch
39
+ yield
40
+ rescue => error
41
+ Pliny.log_exception(error)
42
+ end
27
43
  end
28
44
  end
29
-
@@ -0,0 +1,21 @@
1
+ module Pliny
2
+ module Metrics
3
+ module Backends
4
+ class Logger
5
+ def self.report_counts(counts)
6
+ Pliny.log(add_prefix(:count, counts))
7
+ end
8
+
9
+ def self.report_measures(measures)
10
+ Pliny.log(add_prefix(:measure, measures))
11
+ end
12
+
13
+ private
14
+
15
+ def self.add_prefix(type, metrics)
16
+ metrics.map { |k, v| [ "#{type}##{k}", v ] }.to_h
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.19.0"
2
+ VERSION = "0.20.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  APP_NAME=<%= app_name %>
2
2
  DATABASE_URL=postgres:///<%= app_name %>-development
3
3
  DEPLOYMENT=development
4
- PLINY_ENV=development
4
+ APP_ENV=development
5
5
  TZ=UTC
6
6
  RAISE_ERRORS=true
7
7
  FORCE_SSL=false
@@ -1,7 +1,7 @@
1
1
  APP_NAME=<%= app_name %>
2
2
  DATABASE_URL=postgres:///<%= app_name %>-test
3
3
  DEPLOYMENT=test
4
- PLINY_ENV=test
4
+ APP_ENV=test
5
5
  TZ=UTC
6
6
  RAISE_ERRORS=true
7
7
  FORCE_SSL=false
@@ -4,7 +4,7 @@ ruby "2.3.1"
4
4
  gem "multi_json"
5
5
  gem "oj"
6
6
  gem "pg"
7
- gem "pliny", "~> 0.19"
7
+ gem "pliny", "~> 0.20"
8
8
  gem "pry"
9
9
  gem "puma", "~> 2.16"
10
10
  gem "rack-ssl"
@@ -5,7 +5,7 @@
5
5
  "env": {
6
6
  "APP_NAME": "<%= app_name %>",
7
7
  "DEPLOYMENT": "production",
8
- "PLINY_ENV": "production"
8
+ "APP_ENV": "production"
9
9
  },
10
10
 
11
11
  "scripts": {
@@ -22,7 +22,7 @@ module Config
22
22
  override :db_pool, 5, int
23
23
  override :deployment, 'production', string
24
24
  override :force_ssl, true, bool
25
- override :pliny_env, 'production', string
25
+ override :app_env, 'production', string
26
26
  override :port, 5000, int
27
27
  override :pretty_json, false, bool
28
28
  override :puma_max_threads, 16, int
@@ -0,0 +1 @@
1
+ Pliny::Metrics.backends = [Pliny::Metrics::Backends::Logger]
@@ -4,49 +4,128 @@ require "pliny/config_helpers"
4
4
  describe Pliny::CastingConfigHelpers do
5
5
 
6
6
  describe "#rack_env" do
7
- it "is development if pliny_env is development" do
7
+ it "is development if app_env is development" do
8
8
  config = Class.new do
9
9
  extend Pliny::CastingConfigHelpers
10
- override :pliny_env, 'development', string
10
+ override :app_env, 'development', string
11
11
  end
12
12
 
13
13
  assert_equal "development", config.rack_env
14
14
  end
15
15
 
16
- it "is development if pliny_env is test" do
16
+ it "is development if app_env is test" do
17
17
  config = Class.new do
18
18
  extend Pliny::CastingConfigHelpers
19
- override :pliny_env, 'test', string
19
+ override :app_env, 'test', string
20
20
  end
21
21
 
22
22
  assert_equal "development", config.rack_env
23
23
  end
24
24
 
25
- it "is deployment if pliny_env is production" do
25
+ it "is deployment if app_env is production" do
26
26
  config = Class.new do
27
27
  extend Pliny::CastingConfigHelpers
28
- override :pliny_env, 'production', string
28
+ override :app_env, 'production', string
29
29
  end
30
30
 
31
31
  assert_equal "deployment", config.rack_env
32
32
  end
33
33
 
34
- it "is deployment if pliny_env is nil" do
34
+ it "is deployment if app_env is nil" do
35
35
  config = Class.new do
36
36
  extend Pliny::CastingConfigHelpers
37
- override :pliny_env, '', string
37
+ override :app_env, '', string
38
38
  end
39
39
 
40
40
  assert_equal "deployment", config.rack_env
41
41
  end
42
42
 
43
- it "is deployment if pliny_env is another value" do
43
+ it "is deployment if app_env is another value" do
44
44
  config = Class.new do
45
45
  extend Pliny::CastingConfigHelpers
46
- override :pliny_env, 'staging', string
46
+ override :app_env, 'staging', string
47
47
  end
48
48
 
49
49
  assert_equal "deployment", config.rack_env
50
50
  end
51
+
52
+ context "when legacy PLINY_ENV is still defined" do
53
+ before do
54
+ ENV['ORIGINAL_PLINY_ENV'] = ENV['PLINY_ENV']
55
+ ENV['PLINY_ENV'] = 'staging'
56
+ end
57
+
58
+ after do
59
+ ENV['PLINY_ENV'] = ENV.delete('ORIGINAL_PLINY_ENV')
60
+ end
61
+
62
+ it "uses PLINY_ENV value instead of APP_ENV" do
63
+ config = Class.new do
64
+ extend Pliny::CastingConfigHelpers
65
+ override :app_env, 'development', string
66
+ end
67
+
68
+ assert_equal "deployment", config.rack_env
69
+ end
70
+
71
+ it "displays deprecation warning" do
72
+ config = Class.new do
73
+ extend Pliny::CastingConfigHelpers
74
+ override :app_env, 'development', string
75
+ end
76
+
77
+ io = StringIO.new
78
+ $stderr = io
79
+ config.rack_env
80
+ $stderr = STDERR
81
+
82
+ assert_includes io.string, "PLINY_ENV is deprecated"
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "#pliny_env" do
88
+ it "displays deprecation warning if pliny_env is used" do
89
+ config = Class.new do
90
+ extend Pliny::CastingConfigHelpers
91
+ override :app_env, 'development', string
92
+ end
93
+
94
+ io = StringIO.new
95
+ $stderr = io
96
+ config.pliny_env
97
+ $stderr = STDERR
98
+
99
+ assert_includes io.string, "Config.pliny_env is deprecated"
100
+ end
101
+
102
+ it "returns app_env value" do
103
+ config = Class.new do
104
+ extend Pliny::CastingConfigHelpers
105
+ override :app_env, 'foo', string
106
+ end
107
+
108
+ assert_equal "foo", config.pliny_env
109
+ end
110
+
111
+ context "when legacy PLINY_ENV is still defined" do
112
+ before do
113
+ ENV['ORIGINAL_PLINY_ENV'] = ENV['PLINY_ENV']
114
+ ENV['PLINY_ENV'] = 'staging'
115
+ end
116
+
117
+ after do
118
+ ENV['PLINY_ENV'] = ENV.delete('ORIGINAL_PLINY_ENV')
119
+ end
120
+
121
+ it "returns PLINY_ENV value" do
122
+ config = Class.new do
123
+ extend Pliny::CastingConfigHelpers
124
+ override :app_env, 'development', string
125
+ end
126
+
127
+ assert_equal "staging", config.pliny_env
128
+ end
129
+ end
51
130
  end
52
131
  end
@@ -12,7 +12,7 @@ describe Pliny::Helpers::Serialize do
12
12
  end
13
13
  end
14
14
 
15
- it "sets the Content-Type" do
15
+ it "raises a runtime error" do
16
16
  assert_raises(RuntimeError) do
17
17
  get "/"
18
18
  end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe Pliny::Metrics::Backends::Logger do
4
+ let(:io) { double }
5
+
6
+ subject(:backend) { Pliny::Metrics::Backends::Logger }
7
+
8
+ before do
9
+ @stdout = Pliny.stdout
10
+ Pliny.stdout = io
11
+
12
+ allow(io).to receive(:print)
13
+ end
14
+
15
+ after do
16
+ Pliny.stdout = @stdout
17
+ end
18
+
19
+ context "#count" do
20
+ it "logs a single key with a value" do
21
+ backend.report_counts('app.foo' => 1)
22
+ expect(io).to have_received(:print).with("count#app.foo=1\n")
23
+ end
24
+
25
+ it "logs multiple keys with values" do
26
+ backend.report_counts('app.foo' => 1, 'app.bar' => 2)
27
+ expect(io).to have_received(:print).with(
28
+ "count#app.foo=1 count#app.bar=2\n")
29
+ end
30
+ end
31
+
32
+ context "#measure" do
33
+ it "logs a single key with a value" do
34
+ backend.report_measures('pliny.foo' => 0.001)
35
+ expect(io).to have_received(:print).with("measure#pliny.foo=0.001\n")
36
+ end
37
+
38
+ it "logs multiple keys with values" do
39
+ backend.report_measures('pliny.foo' => 0.3, 'pliny.bar' => 0.5)
40
+ expect(io).to have_received(:print).with(
41
+ "measure#pliny.foo=0.300 measure#pliny.bar=0.500\n")
42
+ end
43
+ end
44
+ end
@@ -1,61 +1,80 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Pliny::Metrics do
4
- let(:io) { double }
5
-
6
4
  subject(:metrics) { Pliny::Metrics }
7
5
 
8
- before do
9
- @stdout = Pliny.stdout
10
- Pliny.stdout = io
6
+ let(:test_backend) do
7
+ double(:test_backend, report_counts: nil, report_measures: nil)
8
+ end
11
9
 
12
- allow(io).to receive(:print)
10
+ before do
13
11
  allow(Config).to receive(:app_name).and_return('pliny')
14
12
  end
15
13
 
16
- after do
17
- Pliny.stdout = @stdout
14
+ around do |example|
15
+ original_backends = Pliny::Metrics.backends
16
+ begin
17
+ example.run
18
+ ensure
19
+ Pliny::Metrics.backends = original_backends
20
+ end
21
+ end
22
+
23
+ it "uses the logger as the default backend" do
24
+ assert_equal(metrics.backends, [Pliny::Metrics::Backends::Logger])
18
25
  end
19
26
 
20
- context "#count" do
21
- it "counts a single key with a default value" do
27
+ describe "#count" do
28
+ before { Pliny::Metrics.backends = [test_backend] }
29
+
30
+ it "sends a hash to the backend with a default value" do
22
31
  metrics.count(:foo)
23
- expect(io).to have_received(:print).with("count#pliny.foo=1\n")
32
+ expect(test_backend).to have_received(:report_counts).once.with("pliny.foo" => 1)
24
33
  end
25
34
 
26
- it "counts a single key with a provided value" do
35
+ it "sends a hash to the backend with a provided value" do
27
36
  metrics.count(:foo, value: 2)
28
- expect(io).to have_received(:print).with("count#pliny.foo=2\n")
37
+ expect(test_backend).to have_received(:report_counts).once.with("pliny.foo" => 2)
29
38
  end
30
39
 
31
- it "counts multiple keys" do
40
+ it "sends a hash with multiple key counts to the backend" do
32
41
  metrics.count(:foo, :bar)
33
- expect(io).to have_received(:print).with(
34
- "count#pliny.foo=1 count#pliny.bar=1\n")
42
+ expect(test_backend).to have_received(:report_counts).once.with(
43
+ "pliny.foo" => 1,
44
+ "pliny.bar" => 1
45
+ )
35
46
  end
36
47
  end
37
48
 
38
- context "#measure" do
49
+ describe "#measure" do
39
50
  before do
40
51
  Timecop.freeze(Time.now)
52
+ Pliny::Metrics.backends = [test_backend]
41
53
  end
42
54
 
43
55
  it "measures a single key" do
44
56
  metrics.measure(:foo) { }
45
- expect(io).to have_received(:print).with("measure#pliny.foo=0.000\n")
57
+ expect(test_backend).to have_received(:report_measures).once.with(
58
+ "pliny.foo" => 0
59
+ )
46
60
  end
47
61
 
48
62
  it "measures a single key over a minute" do
49
63
  metrics.measure(:foo) do
50
64
  Timecop.travel(60)
51
65
  end
52
- expect(io).to have_received(:print).with("measure#pliny.foo=60.000\n")
66
+
67
+ expect(test_backend).to have_received(:report_measures) do |opts|
68
+ assert(60 <= opts['pliny.foo'] && opts['pliny.foo'] <= 61)
69
+ end
53
70
  end
54
71
 
55
72
  it "measures multiple keys" do
56
73
  metrics.measure(:foo, :bar) { }
57
- expect(io).to have_received(:print).with(
58
- "measure#pliny.foo=0.000 measure#pliny.bar=0.000\n")
74
+ expect(test_backend).to have_received(:report_measures).once.with(
75
+ "pliny.foo" => 0,
76
+ "pliny.bar" => 0
77
+ )
59
78
  end
60
79
  end
61
80
  end
@@ -17,7 +17,7 @@ describe Pliny::Middleware::CORS do
17
17
  get "/"
18
18
  assert_equal 200, last_response.status
19
19
  assert_equal "hi", last_response.body
20
- assert_equal nil, last_response.headers["Access-Control-Allow-Origin"]
20
+ assert_nil last_response.headers["Access-Control-Allow-Origin"]
21
21
  end
22
22
 
23
23
  it "intercepts OPTION requests to render a stub (preflight request)" do
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.19.0
4
+ version: 0.20.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-10-12 00:00:00.000000000 Z
12
+ date: 2016-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -362,6 +362,7 @@ files:
362
362
  - lib/pliny/helpers/serialize.rb
363
363
  - lib/pliny/log.rb
364
364
  - lib/pliny/metrics.rb
365
+ - lib/pliny/metrics/backends/logger.rb
365
366
  - lib/pliny/middleware/cors.rb
366
367
  - lib/pliny/middleware/instruments.rb
367
368
  - lib/pliny/middleware/request_id.rb
@@ -408,6 +409,7 @@ files:
408
409
  - lib/template/config/config.rb
409
410
  - lib/template/config/initializers/database.rb
410
411
  - lib/template/config/initializers/log.rb
412
+ - lib/template/config/initializers/metrics.rb
411
413
  - lib/template/config/initializers/rollbar.rb
412
414
  - lib/template/config/puma.rb
413
415
  - lib/template/db/schema.sql
@@ -448,6 +450,7 @@ files:
448
450
  - spec/helpers/serialize_spec.rb
449
451
  - spec/integration_spec.rb
450
452
  - spec/log_spec.rb
453
+ - spec/metrics/backends/logger_spec.rb
451
454
  - spec/metrics_spec.rb
452
455
  - spec/middleware/cors_spec.rb
453
456
  - spec/middleware/instruments_spec.rb