sleuth 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sleuth.rb +31 -4
- data/lib/sleuth/transaction.rb +3 -3
- metadata +1 -1
data/lib/sleuth.rb
CHANGED
@@ -5,9 +5,13 @@ require 'active_support/core_ext/class/attribute_accessors'
|
|
5
5
|
require 'active_support/notifications'
|
6
6
|
require 'active_support/buffered_logger'
|
7
7
|
|
8
|
+
require 'time'
|
9
|
+
|
8
10
|
module Sleuth
|
9
11
|
TRANSACTION_HEADER = "X_SLEUTH_TRANSACTION"
|
10
12
|
|
13
|
+
class OutsideTransaction < StandardError; end
|
14
|
+
|
11
15
|
class << self
|
12
16
|
delegate :head, :get, :post, :put, :delete, :to => :http
|
13
17
|
|
@@ -18,14 +22,18 @@ module Sleuth
|
|
18
22
|
end
|
19
23
|
|
20
24
|
def instrument(payload)
|
21
|
-
|
22
|
-
|
25
|
+
if inside_transaction?
|
26
|
+
ActiveSupport::Notifications.instrument(:sleuth, payload) do
|
27
|
+
yield
|
28
|
+
end
|
29
|
+
else
|
30
|
+
raise OutsideTransaction, "You are outside of a transaction"
|
23
31
|
end
|
24
32
|
end
|
25
33
|
|
26
34
|
def transaction(current_name, parent = nil)
|
27
35
|
ActiveSupport::Notifications.transaction do
|
28
|
-
Transaction.create(current_name,
|
36
|
+
Transaction.create(current_name, parent)
|
29
37
|
|
30
38
|
yield
|
31
39
|
end
|
@@ -39,10 +47,29 @@ module Sleuth
|
|
39
47
|
Transaction.running[current_id]
|
40
48
|
end
|
41
49
|
|
50
|
+
def inside_transaction?
|
51
|
+
current_transaction
|
52
|
+
end
|
53
|
+
|
54
|
+
def thread(name)
|
55
|
+
parent = current_transaction && current_transaction.full_name
|
56
|
+
Thread.new {
|
57
|
+
transaction(name, parent) do
|
58
|
+
yield
|
59
|
+
end
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
42
63
|
def watch(log_path)
|
43
64
|
logger = ActiveSupport::BufferedLogger.new(log_path)
|
65
|
+
subscribe do |event|
|
66
|
+
logger.debug(Transaction.message_for(event))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def subscribe
|
44
71
|
ActiveSupport::Notifications.subscribe('sleuth') do |*args|
|
45
|
-
|
72
|
+
yield Event.new(*args)
|
46
73
|
end
|
47
74
|
end
|
48
75
|
end
|
data/lib/sleuth/transaction.rb
CHANGED
@@ -13,12 +13,12 @@ module Sleuth
|
|
13
13
|
mattr_reader :running
|
14
14
|
@@running = {}
|
15
15
|
|
16
|
-
def self.create(name,
|
16
|
+
def self.create(name, parent)
|
17
|
+
id = Sleuth.current_id
|
17
18
|
running[id] = new(name, id, Time.now.to_i, Process.pid, parent)
|
18
19
|
end
|
19
20
|
|
20
|
-
def self.message_for(
|
21
|
-
event = Event.new(*args)
|
21
|
+
def self.message_for(event)
|
22
22
|
transaction = running[event.transaction_id]
|
23
23
|
transaction.message_for(event)
|
24
24
|
end
|