sleuth 0.2.0
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.
- data/lib/sleuth.rb +54 -0
- data/lib/sleuth/middleware.rb +20 -0
- data/lib/sleuth/outbound_handler.rb +15 -0
- data/lib/sleuth/transaction.rb +36 -0
- metadata +77 -0
data/lib/sleuth.rb
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require 'rack/client'
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/module/delegation'
|
|
4
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
|
5
|
+
require 'active_support/notifications'
|
|
6
|
+
require 'active_support/buffered_logger'
|
|
7
|
+
|
|
8
|
+
module Sleuth
|
|
9
|
+
TRANSACTION_HEADER = "X_SLEUTH_TRANSACTION"
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
delegate :head, :get, :post, :put, :delete, :to => :http
|
|
13
|
+
|
|
14
|
+
def http
|
|
15
|
+
@http ||= Rack::Client.new {
|
|
16
|
+
use OutboundHeader
|
|
17
|
+
}
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def instrument(payload)
|
|
21
|
+
ActiveSupport::Notifications.instrument(:sleuth, payload) do
|
|
22
|
+
yield
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def transaction(current_name, parent = nil)
|
|
27
|
+
ActiveSupport::Notifications.transaction do
|
|
28
|
+
Transaction.create(current_name, current_id, parent)
|
|
29
|
+
|
|
30
|
+
yield
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def current_id
|
|
35
|
+
ActiveSupport::Notifications.transaction_id
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def current_transaction
|
|
39
|
+
Transaction.running[current_id]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def watch(log_path)
|
|
43
|
+
logger = ActiveSupport::BufferedLogger.new(log_path)
|
|
44
|
+
ActiveSupport::Notifications.subscribe('sleuth') do |*args|
|
|
45
|
+
logger.debug(Transaction.message_for(*args))
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
current_dir = File.expand_path(File.dirname(__FILE__) + '/sleuth')
|
|
52
|
+
require current_dir + '/middleware'
|
|
53
|
+
require current_dir + '/outbound_handler'
|
|
54
|
+
require current_dir + '/transaction'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Sleuth
|
|
2
|
+
class Middleware
|
|
3
|
+
def initialize(app, name)
|
|
4
|
+
@app, @name = app, name
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def call(env)
|
|
8
|
+
parent = env["HTTP_#{TRANSACTION_HEADER}"]
|
|
9
|
+
|
|
10
|
+
Sleuth.transaction(@name, parent) do
|
|
11
|
+
request = Rack::Request.new(env)
|
|
12
|
+
code, headers, body = Sleuth.instrument("Received #{request.request_method} #{request.url}") do
|
|
13
|
+
@app.call(env)
|
|
14
|
+
end
|
|
15
|
+
headers[TRANSACTION_HEADER] = Sleuth.current_transaction.full_name
|
|
16
|
+
[code, headers, body]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Sleuth
|
|
2
|
+
class OutboundHeader
|
|
3
|
+
def initialize(app)
|
|
4
|
+
@app = app
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def call(env)
|
|
8
|
+
request = Rack::Request.new(env)
|
|
9
|
+
Sleuth.instrument("Sending #{request.request_method} #{request.url}") do
|
|
10
|
+
env["HTTP_#{TRANSACTION_HEADER}"] = Sleuth.current_transaction.full_name
|
|
11
|
+
@app.call(env)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Sleuth
|
|
2
|
+
class Event < ActiveSupport::Notifications::Event
|
|
3
|
+
def formatted_time
|
|
4
|
+
"%s%06d" % [time.utc.iso8601, time.usec]
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def formatted_end
|
|
8
|
+
"%s%06d" % [@end.utc.iso8601, @end.usec]
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class Transaction < Struct.new(:name, :id, :stamp, :pid, :parent)
|
|
13
|
+
mattr_reader :running
|
|
14
|
+
@@running = {}
|
|
15
|
+
|
|
16
|
+
def self.create(name, id, parent)
|
|
17
|
+
running[id] = new(name, id, Time.now.to_i, Process.pid, parent)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.message_for(*args)
|
|
21
|
+
event = Event.new(*args)
|
|
22
|
+
transaction = running[event.transaction_id]
|
|
23
|
+
transaction.message_for(event)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def message_for(event)
|
|
27
|
+
parts = [event.formatted_time, event.formatted_end,
|
|
28
|
+
full_name, parent || '-', event.duration, event.payload.inspect]
|
|
29
|
+
"%s %s %s %s %0.4f -- %s" % parts
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def full_name
|
|
33
|
+
"#{name}-#{id}-#{stamp}-#{pid}"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sleuth
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Tim Carey-Smith
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-11-05 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: halorgium-activesupport
|
|
17
|
+
type: :runtime
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - "="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: 3.0.pre
|
|
24
|
+
version:
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: rack-client
|
|
27
|
+
type: :runtime
|
|
28
|
+
version_requirement:
|
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 0.2.0
|
|
34
|
+
version:
|
|
35
|
+
description:
|
|
36
|
+
email: dev@spork.in
|
|
37
|
+
executables: []
|
|
38
|
+
|
|
39
|
+
extensions: []
|
|
40
|
+
|
|
41
|
+
extra_rdoc_files: []
|
|
42
|
+
|
|
43
|
+
files:
|
|
44
|
+
- lib/sleuth/middleware.rb
|
|
45
|
+
- lib/sleuth/outbound_handler.rb
|
|
46
|
+
- lib/sleuth/transaction.rb
|
|
47
|
+
- lib/sleuth.rb
|
|
48
|
+
has_rdoc: true
|
|
49
|
+
homepage:
|
|
50
|
+
licenses: []
|
|
51
|
+
|
|
52
|
+
post_install_message:
|
|
53
|
+
rdoc_options: []
|
|
54
|
+
|
|
55
|
+
require_paths:
|
|
56
|
+
- lib
|
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: "0"
|
|
62
|
+
version:
|
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: "0"
|
|
68
|
+
version:
|
|
69
|
+
requirements: []
|
|
70
|
+
|
|
71
|
+
rubyforge_project:
|
|
72
|
+
rubygems_version: 1.3.5
|
|
73
|
+
signing_key:
|
|
74
|
+
specification_version: 3
|
|
75
|
+
summary: Transaction logging middleware
|
|
76
|
+
test_files: []
|
|
77
|
+
|