Module: Sleuth

Defined in:
lib/sleuth.rb,
lib/sleuth/middleware.rb,
lib/sleuth/transaction.rb,
lib/sleuth/outbound_handler.rb

Defined Under Namespace

Classes: Event, Middleware, OutboundHeader, OutsideTransaction, Transaction

Constant Summary collapse

TRANSACTION_HEADER =
"X_SLEUTH_TRANSACTION"

Class Method Summary collapse

Class Method Details

.current_idObject


42
43
44
# File 'lib/sleuth.rb', line 42

def current_id
  ActiveSupport::Notifications.transaction_id
end

.current_transactionObject


46
47
48
# File 'lib/sleuth.rb', line 46

def current_transaction
  Transaction.running[current_id]
end

.httpObject


18
19
20
21
22
# File 'lib/sleuth.rb', line 18

def http
  @http ||= Rack::Client.new {
    use OutboundHeader
  }
end

.inside_transaction?Boolean

Returns:

  • (Boolean)

50
51
52
# File 'lib/sleuth.rb', line 50

def inside_transaction?
  current_transaction
end

.instrument(payload) ⇒ Object


24
25
26
27
28
29
30
31
32
# File 'lib/sleuth.rb', line 24

def instrument(payload)
  if inside_transaction?
    ActiveSupport::Notifications.instrument(:sleuth, payload) do
      yield
    end
  else
    raise OutsideTransaction, "You are outside of a transaction"
  end
end

.subscribeObject


70
71
72
73
74
# File 'lib/sleuth.rb', line 70

def subscribe
  ActiveSupport::Notifications.subscribe('sleuth') do |*args|
    yield Event.new(*args)
  end
end

.thread(name) ⇒ Object


54
55
56
57
58
59
60
61
# File 'lib/sleuth.rb', line 54

def thread(name)
  parent = current_transaction && current_transaction.full_name
  Thread.new {
    transaction(name, parent) do
      yield
    end
  }
end

.transaction(current_name, parent = nil) ⇒ Object


34
35
36
37
38
39
40
# File 'lib/sleuth.rb', line 34

def transaction(current_name, parent = nil)
  ActiveSupport::Notifications.transaction do
    Transaction.create(current_name, parent)

    yield
  end
end

.watch(log_path) ⇒ Object


63
64
65
66
67
68
# File 'lib/sleuth.rb', line 63

def watch(log_path)
  logger = ActiveSupport::BufferedLogger.new(log_path)
  subscribe do |event|
    logger.debug(Transaction.message_for(event))
  end
end