timber 1.1.3 → 1.1.4
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 +4 -4
- data/README.md +1 -1
- data/lib/timber/contexts/custom.rb +1 -1
- data/lib/timber/contexts/organization.rb +1 -1
- data/lib/timber/contexts/system.rb +1 -1
- data/lib/timber/contexts/user.rb +3 -2
- data/lib/timber/events/custom.rb +1 -1
- data/lib/timber/probes/action_controller_user_context.rb +52 -0
- data/lib/timber/probes/active_record_log_subscriber/log_subscriber.rb +9 -7
- data/lib/timber/probes.rb +2 -0
- data/lib/timber/util/object.rb +9 -7
- data/lib/timber/version.rb +1 -1
- data/spec/support/rails.rb +1 -1
- data/spec/timber/probes/action_controller_user_context_spec.rb +55 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a530de0193b8608dad9cf57cd4695260fa861eda
|
4
|
+
data.tar.gz: 30d326dde43b0fa6ecaa68dc3361a2379bbc8a54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
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
|
|
data/lib/timber/contexts/user.rb
CHANGED
@@ -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
|
data/lib/timber/events/custom.rb
CHANGED
@@ -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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
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!
|
data/lib/timber/util/object.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module Timber
|
2
|
-
|
3
|
-
module Object
|
2
|
+
module Util
|
4
3
|
# @private
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
data/lib/timber/version.rb
CHANGED
data/spec/support/rails.rb
CHANGED
@@ -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.
|
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
|