Module: Sleuth

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

Defined Under Namespace

Classes: Event, Middleware, OutboundHeader, Transaction

Constant Summary collapse

TRANSACTION_HEADER =
"X_SLEUTH_TRANSACTION"

Class Method Summary collapse

Class Method Details

.current_idObject



40
41
42
# File 'lib/sleuth.rb', line 40

def current_id
  ActiveSupport::Notifications.transaction_id
end

.current_transactionObject



44
45
46
# File 'lib/sleuth.rb', line 44

def current_transaction
  Transaction.running[current_id]
end

.httpObject



16
17
18
19
20
# File 'lib/sleuth.rb', line 16

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

.inside_transaction?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/sleuth.rb', line 48

def inside_transaction?
  current_transaction
end

.instrument(payload) ⇒ Object



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

def instrument(payload)
  if inside_transaction?
    ActiveSupport::Notifications.instrument(:sleuth, payload) do
      yield
    end
  else
    yield
  end
end

.subscribeObject



68
69
70
71
72
# File 'lib/sleuth.rb', line 68

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

.thread(name) ⇒ Object



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

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



32
33
34
35
36
37
38
# File 'lib/sleuth.rb', line 32

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

    yield
  end
end

.watch(log_path) ⇒ Object



61
62
63
64
65
66
# File 'lib/sleuth.rb', line 61

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