timber 1.1.3 → 1.1.4

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: cfc7a63ba3d0400f2c4dd67bc0844126e5d155fe
4
- data.tar.gz: 1ccf7ffb850c87ae7ccc51c04daaa94b1d685adf
3
+ metadata.gz: a530de0193b8608dad9cf57cd4695260fa861eda
4
+ data.tar.gz: 30d326dde43b0fa6ecaa68dc3361a2379bbc8a54
5
5
  SHA512:
6
- metadata.gz: a207f1fccdd43e25fbb3e7c9b31809f00cd53884f09d1ca21201348330b3ac66999f422a6e9a9eb8831b807dc1cc53e2731e2c5f0f0e8e2c5bfe96e992e69f65
7
- data.tar.gz: 29da7a68133255e47dbbb58498d120b308176d6d5a1f554a4661c3d9820b4e80043ac4a94da740ae10ccf0be2485404815e13860412376d54f8333b7c2c96af6
6
+ metadata.gz: fe81f00a478333a7a51b95b7136d14f843e462e71dda131196ed0fbdc53fe86c52b5ea5e163926cefe09b0ca1c3a493fc8fed7e1de96c7707fedb0d556d8028e
7
+ data.tar.gz: 8e0af30d2ecb86e448b4d3d90f679ec00adca9fbf204a496cb694ac9c476fea63760b925f3a06455e3633e4ad6175cec46858e02f0c7168294b4dfe02f2cbb31
data/README.md CHANGED
@@ -300,7 +300,7 @@ gem 'timber'
300
300
 
301
301
  <details><summary><strong>Rails (all versions, including edge)</strong></summary><p>
302
302
 
303
- 👉 **Prefer examples?** Checkout our the [Timber install example pull request](https://github.com/timberio/ruby-rails-example-app/pulls/1)
303
+ 👉 **Prefer examples?** Checkout our the [Timber install example pull request](https://github.com/timberio/ruby-rails-example-app/pull/1/files)
304
304
 
305
305
  ---
306
306
 
@@ -18,7 +18,7 @@ module Timber
18
18
  end
19
19
 
20
20
  def as_json(_options = {})
21
- {Timber::Object.try(type, :to_sym) => data}
21
+ {Timber::Util::Object.try(type, :to_sym) => data}
22
22
  end
23
23
  end
24
24
  end
@@ -26,7 +26,7 @@ module Timber
26
26
  end
27
27
 
28
28
  def as_json(_options = {})
29
- {id: Timber::Object.try(id, :to_s), name: name}
29
+ {id: Timber::Util::Object.try(id, :to_s), name: name}
30
30
  end
31
31
  end
32
32
  end
@@ -12,7 +12,7 @@ module Timber
12
12
  end
13
13
 
14
14
  def as_json(_options = {})
15
- {pid: Timber::Object.try(pid, :to_s)}
15
+ {pid: Timber::Util::Object.try(pid, :to_s)}
16
16
  end
17
17
  end
18
18
  end
@@ -19,15 +19,16 @@ module Timber
19
19
  class User < Context
20
20
  @keyspace = :user
21
21
 
22
- attr_reader :id, :name
22
+ attr_reader :id, :name, :email
23
23
 
24
24
  def initialize(attributes)
25
25
  @id = attributes[:id]
26
26
  @name = attributes[:name]
27
+ @email = attributes[:email]
27
28
  end
28
29
 
29
30
  def as_json(_options = {})
30
- {id: Timber::Object.try(id, :to_s), name: name}
31
+ {id: Timber::Util::Object.try(id, :to_s), name: name, email: email}
31
32
  end
32
33
  end
33
34
  end
@@ -26,7 +26,7 @@ module Timber
26
26
  end
27
27
 
28
28
  def to_hash
29
- {Timber::Object.try(type, :to_sym) => data}
29
+ {Timber::Util::Object.try(type, :to_sym) => data}
30
30
  end
31
31
  alias to_h to_hash
32
32
 
@@ -0,0 +1,52 @@
1
+ module Timber
2
+ module Probes
3
+ # Responsible for automatically tracking controller call and http response events
4
+ # for applications that use `ActionController`.
5
+ class ActionControllerUserContext < Probe
6
+ module AroundFilter
7
+ def self.included(klass)
8
+ klass.class_eval do
9
+ if klass.respond_to?(:around_action)
10
+ around_action :_timber_capture_user_context
11
+ else
12
+ around_filter :_timber_capture_user_context
13
+ end
14
+
15
+ private
16
+ def _timber_capture_user_context
17
+ if respond_to?(:current_user, true)
18
+ id = Timber::Util::Object.try(current_user, :id)
19
+ name = Timber::Util::Object.try(current_user, :name)
20
+ if !name
21
+ first_name = Timber::Util::Object.try(current_user, :first_name)
22
+ last_name = Timber::Util::Object.try(current_user, :last_name)
23
+ if first_name || last_name
24
+ name = [first_name, last_name].compact.join(" ")
25
+ end
26
+ end
27
+ email = Timber::Util::Object.try(current_user, :email)
28
+ user_context = Timber::Contexts::User.new(:id => id, :name => name, :email => email)
29
+ Timber::CurrentContext.with(user_context) do
30
+ yield
31
+ end
32
+ else
33
+ yield
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def initialize
41
+ require "action_controller"
42
+ rescue LoadError => e
43
+ raise RequirementNotMetError.new(e.message)
44
+ end
45
+
46
+ def insert!
47
+ return true if ActionController::Base.include?(AroundFilter)
48
+ ActionController::Base.send(:include, AroundFilter)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -9,14 +9,16 @@ module Timber
9
9
  def sql(event)
10
10
  super(event)
11
11
 
12
- payload = event.payload
13
- event = Events::SQLQuery.new(
14
- sql: payload[:sql],
15
- time_ms: event.duration,
16
- message: @message
17
- )
12
+ if @message
13
+ payload = event.payload
14
+ event = Events::SQLQuery.new(
15
+ sql: payload[:sql],
16
+ time_ms: event.duration,
17
+ message: @message
18
+ )
18
19
 
19
- logger.debug event
20
+ logger.debug event
21
+ end
20
22
  end
21
23
 
22
24
  private
data/lib/timber/probes.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "timber/probes/action_controller_log_subscriber"
2
+ require "timber/probes/action_controller_user_context"
2
3
  require "timber/probes/action_dispatch_debug_exceptions"
3
4
  require "timber/probes/action_view_log_subscriber"
4
5
  require "timber/probes/active_record_log_subscriber"
@@ -11,6 +12,7 @@ module Timber
11
12
  module Probes
12
13
  def self.insert!
13
14
  ActionControllerLogSubscriber.insert!
15
+ ActionControllerUserContext.insert!
14
16
  ActionDispatchDebugExceptions.insert!
15
17
  ActionViewLogSubscriber.insert!
16
18
  ActiveRecordLogSubscriber.insert!
@@ -1,12 +1,14 @@
1
1
  module Timber
2
- # @private
3
- module Object
2
+ module Util
4
3
  # @private
5
- def self.try(object, method)
6
- if object == nil
7
- nil
8
- else
9
- object.send(method) rescue object
4
+ module Object
5
+ # @private
6
+ def self.try(object, method)
7
+ if object == nil
8
+ nil
9
+ else
10
+ object.send(method) rescue object
11
+ end
10
12
  end
11
13
  end
12
14
  end
@@ -1,3 +1,3 @@
1
1
  module Timber
2
- VERSION = "1.1.3"
2
+ VERSION = "1.1.4"
3
3
  end
@@ -2,7 +2,7 @@ require "rails"
2
2
 
3
3
  # Defualt the rails logger to nothing, each test shoould be
4
4
  # responsible for setting up the logger
5
- logger = ::Logger.new(nil)
5
+ logger = ::Logger.new(STDOUT)
6
6
  Rails.logger = logger
7
7
 
8
8
  class RailsApp < Rails::Application
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Timber::Probes::ActionControllerUserContext do
4
+ describe described_class::AroundFilter do
5
+ let(:time) { Time.utc(2016, 9, 1, 12, 0, 0) }
6
+ let(:io) { StringIO.new }
7
+ let(:logger) do
8
+ logger = Timber::Logger.new(io)
9
+ logger.level = ::Logger::WARN
10
+ logger
11
+ end
12
+
13
+ around(:each) do |example|
14
+ class UserContextController < ActionController::Base
15
+ layout nil
16
+
17
+ def index
18
+ logger.error "test"
19
+ render json: {}
20
+ end
21
+
22
+ def method_for_action(action_name)
23
+ action_name
24
+ end
25
+
26
+ private
27
+ def current_user
28
+ @current_user ||= begin
29
+ user_struct = Struct.new(:id, :name, :email)
30
+ user_struct.new(1, "Ben Johnson", "hi@timber.io")
31
+ end
32
+ end
33
+ end
34
+
35
+ ::RailsApp.routes.draw do
36
+ get 'user_context' => 'user_context#index'
37
+ end
38
+
39
+ old_logger = ::ActionController::Base.logger
40
+ ::ActionController::Base.logger = logger
41
+
42
+ Timecop.freeze(time) { example.run }
43
+
44
+ Object.send(:remove_const, :UserContextController)
45
+ ::ActionController::Base.logger = old_logger
46
+ end
47
+
48
+ describe "#index" do
49
+ it "should capture the user context" do
50
+ dispatch_rails_request("/user_context")
51
+ expect(io.string).to include("\"user\":{\"id\":\"1\",\"name\":\"Ben Johnson\",\"email\":\"hi@timber.io\"}")
52
+ end
53
+ end
54
+ end
55
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timber
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timber Technologies, Inc.
@@ -83,6 +83,7 @@ files:
83
83
  - lib/timber/probes.rb
84
84
  - lib/timber/probes/action_controller_log_subscriber.rb
85
85
  - lib/timber/probes/action_controller_log_subscriber/log_subscriber.rb
86
+ - lib/timber/probes/action_controller_user_context.rb
86
87
  - lib/timber/probes/action_dispatch_debug_exceptions.rb
87
88
  - lib/timber/probes/action_view_log_subscriber.rb
88
89
  - lib/timber/probes/action_view_log_subscriber/log_subscriber.rb
@@ -123,6 +124,7 @@ files:
123
124
  - spec/timber/log_entry_spec.rb
124
125
  - spec/timber/logger_spec.rb
125
126
  - spec/timber/probes/action_controller_log_subscriber_spec.rb
127
+ - spec/timber/probes/action_controller_user_context_spec.rb
126
128
  - spec/timber/probes/action_dispatch_debug_exceptions_spec.rb
127
129
  - spec/timber/probes/action_view_log_subscriber_spec.rb
128
130
  - spec/timber/probes/active_record_log_subscriber_spec.rb
@@ -178,6 +180,7 @@ test_files:
178
180
  - spec/timber/log_entry_spec.rb
179
181
  - spec/timber/logger_spec.rb
180
182
  - spec/timber/probes/action_controller_log_subscriber_spec.rb
183
+ - spec/timber/probes/action_controller_user_context_spec.rb
181
184
  - spec/timber/probes/action_dispatch_debug_exceptions_spec.rb
182
185
  - spec/timber/probes/action_view_log_subscriber_spec.rb
183
186
  - spec/timber/probes/active_record_log_subscriber_spec.rb