timber 2.0.24 → 2.1.0.rc1

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.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -2
  3. data/CHANGELOG +3 -0
  4. data/README.md +314 -59
  5. data/bin/timber +11 -2
  6. data/lib/timber.rb +2 -7
  7. data/lib/timber/cli.rb +16 -28
  8. data/lib/timber/cli/api.rb +80 -14
  9. data/lib/timber/cli/api/application.rb +30 -0
  10. data/lib/timber/cli/config_file.rb +66 -0
  11. data/lib/timber/cli/file_helper.rb +43 -0
  12. data/lib/timber/cli/installer.rb +58 -0
  13. data/lib/timber/cli/installers.rb +37 -0
  14. data/lib/timber/cli/installers/other.rb +47 -0
  15. data/lib/timber/cli/installers/rails.rb +255 -0
  16. data/lib/timber/cli/installers/root.rb +189 -0
  17. data/lib/timber/cli/io.rb +97 -0
  18. data/lib/timber/cli/io/ansi.rb +22 -0
  19. data/lib/timber/cli/io/messages.rb +213 -0
  20. data/lib/timber/cli/os_helper.rb +53 -0
  21. data/lib/timber/config.rb +97 -43
  22. data/lib/timber/config/integrations.rb +63 -0
  23. data/lib/timber/config/integrations/rack.rb +74 -0
  24. data/lib/timber/context.rb +13 -10
  25. data/lib/timber/contexts.rb +1 -0
  26. data/lib/timber/contexts/custom.rb +16 -3
  27. data/lib/timber/contexts/http.rb +10 -3
  28. data/lib/timber/contexts/organization.rb +4 -0
  29. data/lib/timber/contexts/release.rb +46 -0
  30. data/lib/timber/contexts/runtime.rb +7 -1
  31. data/lib/timber/contexts/session.rb +8 -1
  32. data/lib/timber/contexts/system.rb +5 -1
  33. data/lib/timber/contexts/user.rb +9 -2
  34. data/lib/timber/current_context.rb +43 -11
  35. data/lib/timber/events/controller_call.rb +4 -0
  36. data/lib/timber/events/custom.rb +13 -5
  37. data/lib/timber/events/exception.rb +4 -0
  38. data/lib/timber/events/http_client_request.rb +4 -0
  39. data/lib/timber/events/http_client_response.rb +4 -0
  40. data/lib/timber/events/http_server_request.rb +5 -0
  41. data/lib/timber/events/http_server_response.rb +15 -3
  42. data/lib/timber/events/sql_query.rb +3 -0
  43. data/lib/timber/events/template_render.rb +3 -0
  44. data/lib/timber/integration.rb +40 -0
  45. data/lib/timber/integrations.rb +21 -14
  46. data/lib/timber/integrations/action_controller.rb +18 -0
  47. data/lib/timber/integrations/action_controller/log_subscriber.rb +2 -0
  48. data/lib/timber/integrations/action_controller/log_subscriber/timber_log_subscriber.rb +6 -0
  49. data/lib/timber/integrations/action_dispatch.rb +23 -0
  50. data/lib/timber/integrations/action_dispatch/debug_exceptions.rb +2 -0
  51. data/lib/timber/integrations/action_view.rb +18 -0
  52. data/lib/timber/integrations/action_view/log_subscriber.rb +2 -0
  53. data/lib/timber/integrations/action_view/log_subscriber/timber_log_subscriber.rb +10 -0
  54. data/lib/timber/integrations/active_record.rb +18 -0
  55. data/lib/timber/integrations/active_record/log_subscriber.rb +2 -0
  56. data/lib/timber/integrations/active_record/log_subscriber/timber_log_subscriber.rb +8 -0
  57. data/lib/timber/integrations/rack.rb +12 -2
  58. data/lib/timber/integrations/rack/exception_event.rb +38 -5
  59. data/lib/timber/integrations/rack/http_context.rb +4 -6
  60. data/lib/timber/integrations/rack/http_events.rb +177 -27
  61. data/lib/timber/integrations/rack/middleware.rb +28 -0
  62. data/lib/timber/integrations/rack/session_context.rb +5 -6
  63. data/lib/timber/integrations/rack/user_context.rb +90 -43
  64. data/lib/timber/integrations/rails.rb +22 -0
  65. data/lib/timber/integrations/rails/rack_logger.rb +2 -0
  66. data/lib/timber/integrator.rb +18 -3
  67. data/lib/timber/log_devices/http.rb +107 -99
  68. data/lib/timber/log_devices/http/dropping_sized_queue.rb +26 -0
  69. data/lib/timber/log_devices/http/flushable_sized_queue.rb +42 -0
  70. data/lib/timber/log_entry.rb +14 -2
  71. data/lib/timber/logger.rb +51 -36
  72. data/lib/timber/overrides.rb +2 -0
  73. data/lib/timber/overrides/active_support_3_tagged_logging.rb +103 -0
  74. data/lib/timber/overrides/active_support_tagged_logging.rb +53 -90
  75. data/lib/timber/timer.rb +21 -0
  76. data/lib/timber/util/hash.rb +1 -1
  77. data/lib/timber/util/http_event.rb +16 -3
  78. data/lib/timber/version.rb +1 -1
  79. data/spec/support/timber.rb +2 -3
  80. data/spec/timber/cli/installers/rails_spec.rb +160 -0
  81. data/spec/timber/cli/installers/root_spec.rb +100 -0
  82. data/spec/timber/config_spec.rb +28 -0
  83. data/spec/timber/current_context_spec.rb +61 -12
  84. data/spec/timber/events/custom_spec.rb +13 -2
  85. data/spec/timber/events/exception_spec.rb +15 -0
  86. data/spec/timber/events/http_server_request_spec.rb +3 -3
  87. data/spec/timber/integrations/rack/http_events_spec.rb +101 -0
  88. data/spec/timber/log_devices/http_spec.rb +20 -4
  89. data/spec/timber/log_entry_spec.rb +2 -1
  90. data/spec/timber/logger_spec.rb +8 -8
  91. metadata +40 -9
  92. data/benchmarks/rails.rb +0 -122
  93. data/lib/timber/cli/application.rb +0 -28
  94. data/lib/timber/cli/install.rb +0 -196
  95. data/lib/timber/cli/io_helper.rb +0 -65
  96. data/lib/timber/cli/messages.rb +0 -180
  97. data/lib/timber/integrations/active_support/tagged_logging.rb +0 -71
@@ -0,0 +1,21 @@
1
+ module Timber
2
+ # This is an ultra-simple abstraction for timing code. This provides a little
3
+ # more control around how Timber automatically processes "timers".
4
+ #
5
+ # @example
6
+ # timer = Timber::Timer.start
7
+ # # ... code to time
8
+ # logger.info("My log message", my_event: {time_ms: timer})
9
+ module Timer
10
+ # Abstract for starting a timber. Currently this is simply calling `Time.now`.
11
+ def self.start
12
+ Time.now
13
+ end
14
+
15
+ # Get the duration in milliseconds from the object returned in {#start}
16
+ def self.duration_ms(timer)
17
+ now = Time.now
18
+ (now - timer) * 1000.0
19
+ end
20
+ end
21
+ end
@@ -16,7 +16,7 @@ module Timber
16
16
  v
17
17
  end
18
18
 
19
- if v != nil && v != "" && v != {} && v != []
19
+ if v != nil && (!v.respond_to?(:length) || v.length > 0)
20
20
  new_hash[k] = v
21
21
  end
22
22
  end
@@ -1,7 +1,9 @@
1
1
  module Timber
2
2
  module Util
3
+ # Utility module for dealing with HTTP events {Events::HTTPServerRequest},
4
+ # {Events::HTTPServerResponse}, {Events::HTTPClientRequest}, {Events::HTTPClientResponse}.
3
5
  module HTTPEvent
4
- AUTHORIZATION_HEADER = 'authorization'.freeze
6
+ HEADERS_TO_SANITIZE = ['authorization', 'x-amz-security-token'].freeze
5
7
  QUERY_STRING_LIMIT = 5_000.freeze
6
8
  STRING_CLASS_NAME = 'String'.freeze
7
9
 
@@ -15,14 +17,24 @@ module Timber
15
17
  end
16
18
  end
17
19
 
20
+ # Normalizes the body. If limit if passed it will truncate the body to that limit.
18
21
  def normalize_body(body)
19
22
  if body.respond_to?(:body)
20
23
  body = body.body.to_s
21
24
  end
22
25
 
23
- body[0..(Config.instance.http_body_limit - 1)]
26
+ limit = Config.instance.http_body_limit
27
+ if limit
28
+ body[0..(limit - 1)]
29
+ else
30
+ body
31
+ end
24
32
  end
25
33
 
34
+ # Normalizes headers to:
35
+ #
36
+ # 1. Ensure the value is UTF8, this will otherwise throw errors upon capturing.
37
+ # 2. Sanitize sensitive headers such as `Authorization` or custom headers specified in
26
38
  def normalize_headers(headers)
27
39
  if headers.is_a?(::Hash)
28
40
  h = headers.each_with_object({}) do |(k, v), h|
@@ -46,13 +58,14 @@ module Timber
46
58
  end
47
59
  end
48
60
 
49
- keys_to_sanitize = [AUTHORIZATION_HEADER] + (Config.instance.header_filters || [])
61
+ keys_to_sanitize = HEADERS_TO_SANITIZE + (Config.instance.http_header_filters || [])
50
62
  Util::Hash.sanitize(h, keys_to_sanitize)
51
63
  else
52
64
  headers
53
65
  end
54
66
  end
55
67
 
68
+ # Normalizes the HTTP method into an uppercase string.
56
69
  def normalize_method(method)
57
70
  method.is_a?(::String) ? method.upcase : method
58
71
  end
@@ -1,3 +1,3 @@
1
1
  module Timber
2
- VERSION = "2.0.24"
2
+ VERSION = "2.1.0.rc1"
3
3
  end
@@ -1,4 +1,3 @@
1
1
  require "timber"
2
-
3
- config = Timber::Config.instance
4
- config.append_metadata = true
2
+ require "timber/cli"
3
+ require "timber/cli/io"
@@ -0,0 +1,160 @@
1
+ require "spec_helper"
2
+
3
+ describe Timber::CLI::Installers::Rails, :rails_23 => true do
4
+ let(:api_key) { "abcd1234" }
5
+ let(:app) do
6
+ attributes = {
7
+ "api_key" => api_key,
8
+ "environment" => "development",
9
+ "framework_type" => "rails",
10
+ "heroku_drain_url" => "http://drain.heroku.com",
11
+ "name" => "My Rails App",
12
+ "platform_type" => "other"
13
+ }
14
+ Timber::CLI::API::Application.new(attributes)
15
+ end
16
+ let(:api) { Timber::CLI::API.new(api_key) }
17
+ let(:input) { StringIO.new }
18
+ let(:output) { StringIO.new }
19
+ let(:io) { Timber::CLI::IO.new(io_out: output, io_in: input) }
20
+ let(:installer) { described_class.new(io, api) }
21
+ let(:initial_config_contents) { "# Timber.io Ruby Configuration - Simple Structured Logging\n#\n# ^ ^ ^ ^ ___I_ ^ ^ ^ ^ ^ ^ ^\n# /|\\/|\\/|\\ /|\\ /\\-_--\\ /|\\/|\\ /|\\/|\\/|\\ /|\\/|\\\n# /|\\/|\\/|\\ /|\\ / \\_-__\\ /|\\/|\\ /|\\/|\\/|\\ /|\\/|\\\n# /|\\/|\\/|\\ /|\\ |[]| [] | /|\\/|\\ /|\\/|\\/|\\ /|\\/|\\\n# -------------------------------------------------------------------\n# Website: https://timber.io\n# Documentation: https://timber.io/docs\n# Support: support@timber.io\n# -------------------------------------------------------------------\n\nconfig = Timber::Config.instance\n\n# Add additional configuration here.\n# For a full list of configuration options and their explanations see:\n# http://www.rubydoc.info/github/timberio/timber-ruby/Timber/Config\n\n" }
22
+
23
+ describe ".run" do
24
+ it "should execute properly" do
25
+ expect(installer).to receive(:get_development_preference).exactly(1).times.and_return(:send)
26
+ expect(installer).to receive(:get_api_key_storage_preference).exactly(1).times.and_return(:environment)
27
+ expect(installer).to receive(:logrageify?).exactly(1).times.and_return(true)
28
+ expect(installer).to receive(:initializer).exactly(1).times.and_return(true)
29
+ expect(installer).to receive(:logrageify!).exactly(1).times.and_return(true)
30
+ expect(installer).to receive(:environment_file_paths).exactly(1).times.and_return(["config/environments/development.rb", "config/environments/production.rb", "config/environments/test.rb"])
31
+ expect(installer).to receive(:setup_development_environment).with("config/environments/development.rb", :send).and_return(true)
32
+ expect(installer).to receive(:setup_other_environment).with(app, "config/environments/production.rb", :environment).and_return(true)
33
+ expect(installer).to receive(:setup_test_environment).with("config/environments/test.rb").and_return(true)
34
+
35
+ installer.run(app)
36
+ end
37
+ end
38
+
39
+ describe ".initializer" do
40
+ it "should create a config file" do
41
+ config_file_path = "config/initializers/timber.rb"
42
+
43
+ expect(Timber::CLI::FileHelper).to receive(:read_or_create).
44
+ with(config_file_path, initial_config_contents).
45
+ and_return(initial_config_contents)
46
+
47
+ config_file = installer.send(:initializer)
48
+ expect(config_file.path).to eq(config_file_path)
49
+ end
50
+ end
51
+
52
+ describe ".logrageify?" do
53
+ it "should do nothing if Lograge is not detected" do
54
+ expect(installer.send(:logrageify?)).to eq(false)
55
+ expect(output.string).to eq("")
56
+ end
57
+
58
+ context "with a Lograge constant" do
59
+ around(:each) do |example|
60
+ Lograge = 1
61
+ example.run
62
+ Object.send(:remove_const, :Lograge)
63
+ end
64
+
65
+ it "should prompt for Lograge configuration and return true for y" do
66
+ input.string = "y\n"
67
+ expect(installer.send(:logrageify?)).to eq(true)
68
+ expect(output.string).to eq("\n--------------------------------------------------------------------------------\n\nWe noticed you have lograge installed. Would you like to configure \nTimber to function similarly?\n(This silences template renders, sql queries, and controller calls.\nYou can always do this later in config/initialzers/timber.rb)\n\n\e[34my) Yes, configure Timber like lograge\e[0m\n\e[34mn) No, use the Rails logging defaults\e[0m\n\nEnter your choice: (y/n) ")
69
+ end
70
+ end
71
+ end
72
+
73
+ describe ".logrageify!" do
74
+ it "should set the option in the config file" do
75
+ config_file_path = "config/initializers/timber.rb"
76
+
77
+ expect(Timber::CLI::FileHelper).to receive(:read_or_create).
78
+ with(config_file_path, initial_config_contents).
79
+ and_return(initial_config_contents)
80
+
81
+ expect(Timber::CLI::FileHelper).to receive(:read).
82
+ with(config_file_path).
83
+ and_return(initial_config_contents)
84
+
85
+ new_config_contents = "# Timber.io Ruby Configuration - Simple Structured Logging\n#\n# ^ ^ ^ ^ ___I_ ^ ^ ^ ^ ^ ^ ^\n# /|\\/|\\/|\\ /|\\ /\\-_--\\ /|\\/|\\ /|\\/|\\/|\\ /|\\/|\\\n# /|\\/|\\/|\\ /|\\ / \\_-__\\ /|\\/|\\ /|\\/|\\/|\\ /|\\/|\\\n# /|\\/|\\/|\\ /|\\ |[]| [] | /|\\/|\\ /|\\/|\\/|\\ /|\\/|\\\n# -------------------------------------------------------------------\n# Website: https://timber.io\n# Documentation: https://timber.io/docs\n# Support: support@timber.io\n# -------------------------------------------------------------------\n\nconfig = Timber::Config.instance\n\nconfig.logrageify!\n\n# Add additional configuration here.\n# For a full list of configuration options and their explanations see:\n# http://www.rubydoc.info/github/timberio/timber-ruby/Timber/Config\n\n"
86
+
87
+ expect(Timber::CLI::FileHelper).to receive(:write).
88
+ with(config_file_path, new_config_contents).
89
+ and_return(true)
90
+
91
+ expect(installer.send(:logrageify!)).to eq(true)
92
+ end
93
+ end
94
+
95
+ describe ".setup_development_environment" do
96
+ it "should setup properly" do
97
+ env_file_path = "config/environments/development.rb"
98
+
99
+ expect(Timber::CLI::FileHelper).to receive(:read).
100
+ with(env_file_path).
101
+ exactly(2).times.
102
+ and_return("\nend")
103
+
104
+ logger_code = defined?(ActiveSupport::TaggedLogging) ? "ActiveSupport::TaggedLogging.new(logger)" : "logger"
105
+ new_contents = "\n\n # Install the Timber.io logger, send logs over HTTP.\n # Note: When you are done testing, simply instantiate the logger like this:\n#\n# logger = Timber::Logger.new(STDOUT)\n#\n# Be sure to remove the \"log_device =\" and \"logger =\" lines below.\n log_device = Timber::LogDevices::HTTP.new('abcd1234')\n logger = Timber::Logger.new(log_device)\n logger.level = config.log_level\n config.logger = #{logger_code}\n\nend"
106
+
107
+ expect(Timber::CLI::FileHelper).to receive(:write).
108
+ with(env_file_path, new_contents).
109
+ and_return(true)
110
+
111
+ expect(api).to receive(:event).with(:file_written, path: env_file_path)
112
+
113
+ expect(installer.send(:setup_development_environment, env_file_path, :send)).to eq(true)
114
+ end
115
+ end
116
+
117
+ describe ".setup_test_environment" do
118
+ it "should setup properly" do
119
+ env_file_path = "config/environments/test.rb"
120
+
121
+ expect(Timber::CLI::FileHelper).to receive(:read).
122
+ with(env_file_path).
123
+ exactly(2).times.
124
+ and_return("\nend")
125
+
126
+ logger_code = defined?(ActiveSupport::TaggedLogging) ? "ActiveSupport::TaggedLogging.new(logger)" : "logger"
127
+ new_contents = "\n\n # Install the Timber.io logger but silence all logs (log to nil). We install the\n # logger to ensure the Rails.logger object exposes the proper API.\n logger = Timber::Logger.new(nil)\n logger.level = config.log_level\n config.logger = #{logger_code}\n\nend"
128
+
129
+ expect(Timber::CLI::FileHelper).to receive(:write).
130
+ with(env_file_path, new_contents).
131
+ and_return(true)
132
+
133
+ expect(api).to receive(:event).with(:file_written, path: env_file_path)
134
+
135
+ expect(installer.send(:setup_test_environment, env_file_path)).to eq(true)
136
+ end
137
+ end
138
+
139
+ describe ".setup_other_environment" do
140
+ it "should setup properly" do
141
+ env_file_path = "config/environments/production.rb"
142
+
143
+ expect(Timber::CLI::FileHelper).to receive(:read).
144
+ with(env_file_path).
145
+ exactly(2).times.
146
+ and_return("\nend")
147
+
148
+ logger_code = defined?(ActiveSupport::TaggedLogging) ? "ActiveSupport::TaggedLogging.new(logger)" : "logger"
149
+ new_contents = "\n\n # Install the Timber.io logger, send logs over HTTP.\n log_device = Timber::LogDevices::HTTP.new(ENV['TIMBER_API_KEY'])\n logger = Timber::Logger.new(log_device)\n logger.level = config.log_level\n config.logger = #{logger_code}\n\nend"
150
+
151
+ expect(Timber::CLI::FileHelper).to receive(:write).
152
+ with(env_file_path, new_contents).
153
+ and_return(true)
154
+
155
+ expect(api).to receive(:event).with(:file_written, path: env_file_path).exactly(1).times
156
+
157
+ expect(installer.send(:setup_other_environment, app, env_file_path, :environment)).to eq(true)
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Timber::CLI::Installers::Root, :rails_23 => true do
6
+ let(:api_key) { "abcd1234" }
7
+ let(:app) do
8
+ attributes = {
9
+ "api_key" => api_key,
10
+ "environment" => "development",
11
+ "framework_type" => "rails",
12
+ "heroku_drain_url" => "http://drain.heroku.com",
13
+ "name" => "My Rails App",
14
+ "platform_type" => "other"
15
+ }
16
+ Timber::CLI::API::Application.new(attributes)
17
+ end
18
+ let(:api) { Timber::CLI::API.new(api_key) }
19
+ let(:input) { StringIO.new }
20
+ let(:output) { StringIO.new }
21
+ let(:io) { Timber::CLI::IO.new(io_out: output, io_in: input) }
22
+ let(:installer) { described_class.new(io, api) }
23
+
24
+ describe ".run" do
25
+ it "should run properly" do
26
+ input.string = "y\n"
27
+
28
+ expect(installer).to receive(:install_platform).with(app).exactly(1).times.and_return(true)
29
+ expect(installer).to receive(:run_sub_installer).with(app).exactly(1).times.and_return(true)
30
+ expect(installer).to receive(:send_test_messages).exactly(1).times.and_return(true)
31
+ expect(installer).to receive(:confirm_log_delivery).exactly(1).times.and_return(true)
32
+ expect(installer).to receive(:assist_with_git).exactly(1).times.and_return(true)
33
+ expect(api).to receive(:event).with(:success).exactly(1).times
34
+ expect(installer).to receive(:collect_feedback).exactly(1).times.and_return(true)
35
+
36
+ expect(installer.run(app)).to eq(true)
37
+ end
38
+ end
39
+
40
+ describe ".install_platform" do
41
+ context "non-heroku" do
42
+ it "should do nothing" do
43
+ expect(installer.send(:install_platform, app)).to eq(true)
44
+ expect(output.string).to eq("")
45
+ end
46
+ end
47
+
48
+ context "heroku" do
49
+ before(:each) do
50
+ app.platform_type = "heroku"
51
+ end
52
+
53
+ it "should prompt for Heroku install" do
54
+ input.string = "y\n"
55
+
56
+ expect(installer.send(:install_platform, app)).to eq(true)
57
+
58
+ copy_to_clipboard_message = Timber::CLI::OSHelper.copy_to_clipboard? ? "\n \e[32m(✓ copied to clipboard)\e[0m" : ""
59
+ expected_output = "\n--------------------------------------------------------------------------------\n\nFirst, let's setup your Heroku drain. Run this command in a separate window:\n\n \e[34mheroku drains:add http://drain.heroku.com\e[0m#{copy_to_clipboard_message}\n\nReady to proceed? (y/n) "
60
+ expect(output.string).to eq(expected_output)
61
+ end
62
+ end
63
+ end
64
+
65
+ describe ".get_sub_installer" do
66
+ context "with Rails" do
67
+ around(:each) do |example|
68
+ if defined?(Rails)
69
+ example.run
70
+ else
71
+ Rails = true
72
+ example.run
73
+ Object.send(:remove_const, :Rails)
74
+ end
75
+ end
76
+
77
+ it "should return Rails" do
78
+ expect(installer.send(:get_sub_installer).class).to eq(Timber::CLI::Installers::Rails)
79
+ end
80
+ end
81
+
82
+ context "without Rails" do
83
+ around(:each) do |example|
84
+ if defined?(Rails)
85
+ OldRails = Rails
86
+ Object.send(:remove_const, :Rails)
87
+ example.run
88
+ Rails = OldRails
89
+ Object.send(:remove_const, :OldRails)
90
+ else
91
+ example.run
92
+ end
93
+ end
94
+
95
+ it "should return other" do
96
+ expect(installer.send(:get_sub_installer).class).to eq(Timber::CLI::Installers::Other)
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,28 @@
1
+ require "singleton"
2
+ require "spec_helper"
3
+
4
+ describe Timber::Config, :rails_23 => true do
5
+ let(:config) { Timber::Config.send(:new) }
6
+
7
+ describe ".logrageify!" do
8
+ it "should logrageify" do
9
+ expect(Timber::Integrations::ActionController.silence?).to eq(false)
10
+ expect(Timber::Integrations::ActionView.silence?).to eq(false)
11
+ expect(Timber::Integrations::ActiveRecord.silence?).to eq(false)
12
+ expect(Timber::Integrations::Rack::HTTPEvents.collapse_into_single_event?).to eq(false)
13
+
14
+ config.logrageify!
15
+
16
+ expect(Timber::Integrations::ActionController.silence?).to eq(true)
17
+ expect(Timber::Integrations::ActionView.silence?).to eq(true)
18
+ expect(Timber::Integrations::ActiveRecord.silence?).to eq(true)
19
+ expect(Timber::Integrations::Rack::HTTPEvents.collapse_into_single_event?).to eq(true)
20
+
21
+ # Reset
22
+ Timber::Integrations::ActionController.silence = false
23
+ Timber::Integrations::ActionView.silence = false
24
+ Timber::Integrations::ActiveRecord.silence = false
25
+ Timber::Integrations::Rack::HTTPEvents.collapse_into_single_event = false
26
+ end
27
+ end
28
+ end
@@ -1,19 +1,68 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Timber::CurrentContext, :rails_23 => true do
4
+ describe ".initialize" do
5
+ it "should not set the release context" do
6
+ context = described_class.send(:new)
7
+ expect(context.send(:hash)).to eq({})
8
+ end
9
+
10
+ context "with Heroku dyno metadata" do
11
+ around(:each) do |example|
12
+ ENV['HEROKU_SLUG_COMMIT'] = "2c3a0b24069af49b3de35b8e8c26765c1dba9ff0"
13
+ ENV['HEROKU_RELEASE_CREATED_AT'] = "2015-04-02T18:00:42Z"
14
+ ENV['HEROKU_RELEASE_VERSION'] = "v2.3.1"
15
+
16
+ example.run
17
+
18
+ ENV.delete('HEROKU_SLUG_COMMIT')
19
+ ENV.delete('HEROKU_RELEASE_CREATED_AT')
20
+ ENV.delete('HEROKU_RELEASE_VERSION')
21
+
22
+ described_class.reset
23
+ end
24
+
25
+ it "should automatically set the release context" do
26
+ context = described_class.send(:new)
27
+ expect(context.send(:hash)).to eq({:release=>{:commit_hash=>"2c3a0b24069af49b3de35b8e8c26765c1dba9ff0", :created_at=>"2015-04-02T18:00:42Z", :version=>"v2.3.1"}})
28
+ end
29
+ end
30
+
31
+ context "with genric env vars" do
32
+ around(:each) do |example|
33
+ ENV['RELEASE_COMMIT'] = "2c3a0b24069af49b3de35b8e8c26765c1dba9ff0"
34
+ ENV['RELEASE_CREATED_AT'] = "2015-04-02T18:00:42Z"
35
+ ENV['RELEASE_VERSION'] = "v2.3.1"
36
+
37
+ example.run
38
+
39
+ ENV.delete('RELEASE_COMMIT')
40
+ ENV.delete('RELEASE_CREATED_AT')
41
+ ENV.delete('RELEASE_VERSION')
42
+
43
+ described_class.reset
44
+ end
45
+
46
+ it "should automatically set the release context" do
47
+ context = described_class.send(:new)
48
+ expect(context.send(:hash)).to eq({:release=>{:commit_hash=>"2c3a0b24069af49b3de35b8e8c26765c1dba9ff0", :created_at=>"2015-04-02T18:00:42Z", :version=>"v2.3.1"}})
49
+ end
50
+ end
51
+ end
52
+
4
53
  describe ".add" do
5
54
  after(:each) do
6
55
  described_class.reset
7
56
  end
8
57
 
9
58
  it "should add the context" do
10
- expect(described_class.hash).to eq({})
59
+ expect(described_class.instance.send(:hash)).to eq({})
11
60
 
12
61
  described_class.add({build: {version: "1.0.0"}})
13
- expect(described_class.hash).to eq({:custom=>{:build=>{:version=>"1.0.0"}}})
62
+ expect(described_class.instance.send(:hash)).to eq({:custom=>{:build=>{:version=>"1.0.0"}}})
14
63
 
15
64
  described_class.add({testing: {key: "value"}})
16
- expect(described_class.hash).to eq({:custom=>{:build=>{:version=>"1.0.0"}, :testing=>{:key=>"value"}}})
65
+ expect(described_class.instance.send(:hash)).to eq({:custom=>{:build=>{:version=>"1.0.0"}, :testing=>{:key=>"value"}}})
17
66
  end
18
67
  end
19
68
 
@@ -21,37 +70,37 @@ describe Timber::CurrentContext, :rails_23 => true do
21
70
  it "should remove the context by object" do
22
71
  context = {:build=>{:version=>"1.0.0"}}
23
72
  described_class.add(context)
24
- expect(described_class.hash).to eq({:custom => context})
73
+ expect(described_class.instance.send(:hash)).to eq({:custom => context})
25
74
 
26
75
  described_class.remove(context)
27
- expect(described_class.hash).to eq({})
76
+ expect(described_class.instance.send(:hash)).to eq({})
28
77
  end
29
78
 
30
79
  it "should remove context by key" do
31
80
  context = {:build=>{:version=>"1.0.0"}}
32
81
  described_class.add(context)
33
- expect(described_class.hash).to eq({:custom => context})
82
+ expect(described_class.instance.send(:hash)).to eq({:custom => context})
34
83
 
35
84
  described_class.remove(:custom)
36
- expect(described_class.hash).to eq({})
85
+ expect(described_class.instance.send(:hash)).to eq({})
37
86
  end
38
87
  end
39
88
 
40
89
  describe ".with" do
41
90
  it "should merge the context and cleanup on block exit" do
42
- expect(described_class.hash).to eq({})
91
+ expect(described_class.instance.send(:hash)).to eq({})
43
92
 
44
93
  described_class.with({build: {version: "1.0.0"}}) do
45
- expect(described_class.hash).to eq({:custom=>{:build=>{:version=>"1.0.0"}}})
94
+ expect(described_class.instance.send(:hash)).to eq({:custom=>{:build=>{:version=>"1.0.0"}}})
46
95
 
47
96
  described_class.with({testing: {key: "value"}}) do
48
- expect(described_class.hash).to eq({:custom=>{:build=>{:version=>"1.0.0"}, :testing=>{:key=>"value"}}})
97
+ expect(described_class.instance.send(:hash)).to eq({:custom=>{:build=>{:version=>"1.0.0"}, :testing=>{:key=>"value"}}})
49
98
  end
50
99
 
51
- expect(described_class.hash).to eq({:custom=>{:build=>{:version=>"1.0.0"}}})
100
+ expect(described_class.instance.send(:hash)).to eq({:custom=>{:build=>{:version=>"1.0.0"}}})
52
101
  end
53
102
 
54
- expect(described_class.hash).to eq({})
103
+ expect(described_class.instance.send(:hash)).to eq({})
55
104
  end
56
105
  end
57
106
  end