log_weasel 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  *.gem
2
2
  .bundle
3
3
  pkg/*
4
+ .idea
data/Gemfile.lock CHANGED
@@ -1,13 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- log_weasel (0.0.2)
4
+ log_weasel (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
9
  activesupport (3.0.4)
10
+ builder (3.0.0)
10
11
  diff-lcs (1.1.2)
12
+ hoptoad_notifier (2.4.5)
13
+ activesupport
14
+ builder
11
15
  json (1.4.6)
12
16
  mocha (0.9.11)
13
17
  rake
@@ -41,6 +45,7 @@ PLATFORMS
41
45
 
42
46
  DEPENDENCIES
43
47
  activesupport (~> 3.0)
48
+ hoptoad_notifier
44
49
  log_weasel!
45
50
  mocha
46
51
  resque (~> 1.0)
data/Rakefile CHANGED
@@ -9,3 +9,5 @@ Bundler::GemHelper.install_tasks
9
9
  Rspec::Core::RakeTask.new(:spec) do |spec|
10
10
  spec.pattern = 'spec/**/*_spec.rb'
11
11
  end
12
+
13
+ task :default => [:spec]
@@ -0,0 +1,15 @@
1
+ require 'active_support/buffered_logger'
2
+
3
+ class LogWeasel::BufferedLogger < ::ActiveSupport::BufferedLogger
4
+ def add(severity, message = nil, progname = nil, &block)
5
+ super(severity, "[#{LogWeasel::Transaction.id}] #$$: #{format_severity(severity)} #{message}", progname, &block)
6
+ end
7
+
8
+ private
9
+ # Severity label for logging. (max 5 char)
10
+ SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
11
+
12
+ def format_severity(severity)
13
+ SEV_LABEL[severity] || 'ANY'
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ module LogWeasel::HoptoadNotifier
2
+ def notify_with_transaction_id(exception, opts = {})
3
+ add_transaction_id(opts) if LogWeasel::Transaction.id
4
+ notify_without_transaction_id exception, opts
5
+ end
6
+
7
+ def notify_or_ignore_with_transaction_id(exception, opts = {})
8
+ add_transaction_id(opts) if LogWeasel::Transaction.id
9
+ notify_or_ignore_without_transaction_id exception, opts
10
+ end
11
+
12
+ def add_transaction_id(opts)
13
+ opts[:parameters] ||= {}
14
+ opts[:parameters]['log_weasel_id'] = LogWeasel::Transaction.id
15
+ end
16
+
17
+ def self.included(base)
18
+ base.send :alias_method, :notify_without_transaction_id, :notify
19
+ base.send :alias_method, :notify, :notify_with_transaction_id
20
+ base.send :alias_method, :notify_or_ignore_without_transaction_id, :notify_or_ignore
21
+ base.send :alias_method, :notify_or_ignore, :notify_or_ignore_with_transaction_id
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ class LogWeasel::Middleware
2
+ def initialize(app, options = {})
3
+ @app = app
4
+ @key = options[:key] || 'RAILS'
5
+ end
6
+
7
+ def call(env)
8
+ LogWeasel::Transaction.create "#{@key}-WEB"
9
+ @app.call(env)
10
+ ensure
11
+ LogWeasel::Transaction.destroy
12
+ end
13
+ end
@@ -0,0 +1,67 @@
1
+ module LogWeasel::Resque
2
+
3
+ def self.initialize!(key)
4
+ ::Resque::Worker.send(:include, LogWeasel::Resque::Worker)
5
+ ::Resque::Job.send(:include, LogWeasel::Resque::Job)
6
+ ::Resque.extend(LogWeasel::Resque::ClassMethods)
7
+
8
+ ::Resque.after_fork do |job|
9
+ LogWeasel::Resque::Callbacks.after_fork job, key
10
+ end
11
+
12
+ ::Resque.before_push do |queue, item|
13
+ LogWeasel::Resque::Callbacks.before_push queue, item, key
14
+ end
15
+ end
16
+
17
+ module Callbacks
18
+ def self.after_fork(job, key)
19
+ if job.context && job.context.has_key?('log_weasel_id')
20
+ LogWeasel::Transaction.id = job.context['log_weasel_id']
21
+ else
22
+ LogWeasel::Transaction.create "#{key}-RESQUE"
23
+ end
24
+ end
25
+
26
+ def self.before_push(queue, item, key)
27
+ item['context'] = {'log_weasel_id' => (LogWeasel::Transaction.id || LogWeasel::Transaction.create("#{key}-RESQUE"))}
28
+ end
29
+ end
30
+
31
+ module ClassMethods
32
+ def before_push(&block)
33
+ block ? (@before_push = block) : @before_push
34
+ end
35
+
36
+ def push(queue, item)
37
+ self.before_push.call(queue, item) if self.before_push
38
+ super
39
+ end
40
+ end
41
+
42
+ module Job
43
+ def context
44
+ @payload['context']
45
+ end
46
+
47
+ def inspect_with_context
48
+ inspect_without_context.gsub /\)$/, " | #{context.inspect})"
49
+ end
50
+
51
+ def self.included(base)
52
+ base.send :alias_method, :inspect_without_context, :inspect
53
+ base.send :alias_method, :inspect, :inspect_with_context
54
+ end
55
+ end
56
+
57
+ module Worker
58
+ def log_with_transaction_id(message)
59
+ log_without_transaction_id "[#{LogWeasel::Transaction.id}] #{message}"
60
+ end
61
+
62
+ def self.included(base)
63
+ base.send :alias_method, :log_without_transaction_id, :log
64
+ base.send :alias_method, :log, :log_with_transaction_id
65
+ end
66
+ end
67
+ end
@@ -2,19 +2,19 @@ module LogWeasel
2
2
  module Transaction
3
3
 
4
4
  def self.create(key = nil)
5
- Thread.current[:manilla_transaction_id] = "#{key ? "#{key}_" : ""}#{SecureRandom.hex(10)}"
5
+ Thread.current[:log_weasel_id] = "#{key ? "#{key}_" : ""}#{SecureRandom.hex(10)}"
6
6
  end
7
7
 
8
8
  def self.destroy
9
- Thread.current[:manilla_transaction_id] = nil
9
+ Thread.current[:log_weasel_id] = nil
10
10
  end
11
11
 
12
12
  def self.id=(id)
13
- Thread.current[:manilla_transaction_id] = id
13
+ Thread.current[:log_weasel_id] = id
14
14
  end
15
15
 
16
16
  def self.id
17
- Thread.current[:manilla_transaction_id]
17
+ Thread.current[:log_weasel_id]
18
18
  end
19
19
  end
20
20
 
@@ -1,5 +1,5 @@
1
1
  module Log
2
2
  module Weasel
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/log_weasel.rb CHANGED
@@ -1 +1,9 @@
1
1
  require 'log_weasel/transaction'
2
+ require 'log_weasel/buffered_logger'
3
+ require 'log_weasel/hoptoad_notifier'
4
+ require 'log_weasel/middleware'
5
+ require 'log_weasel/resque'
6
+
7
+ class << ::HoptoadNotifier
8
+ include LogWeasel::HoptoadNotifier;
9
+ end if defined? ::HoptoadNotifier
data/log_weasel.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.add_development_dependency('mocha')
19
19
  s.add_development_dependency('resque', ['~> 1.0'])
20
20
  s.add_development_dependency('activesupport', ['~> 3.0'])
21
+ s.add_development_dependency('hoptoad_notifier',)
21
22
 
22
23
  s.files = `git ls-files`.split("\n")
23
24
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'log_weasel/buffered_logger'
3
+
4
+ describe LogWeasel::BufferedLogger do
5
+ before do
6
+ @stringio = StringIO.new
7
+ @logger = LogWeasel::BufferedLogger.new @stringio
8
+ LogWeasel::Transaction.stubs(:id).returns('123')
9
+ end
10
+
11
+ it "logs transaction id" do
12
+ @logger.info 'message'
13
+ @stringio.string.should =~ /\[123\]/
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'hoptoad_notifier'
3
+ require 'log_weasel/hoptoad_notifier'
4
+
5
+ describe LogWeasel::HoptoadNotifier do
6
+ before do
7
+ class << ::HoptoadNotifier
8
+ include LogWeasel::HoptoadNotifier;
9
+ end if defined? ::HoptoadNotifier
10
+
11
+ HoptoadNotifier.configure {}
12
+ LogWeasel::Transaction.stubs(:id).returns('123')
13
+ end
14
+
15
+ it "adds transaction id to parameters with no parameters" do
16
+ HoptoadNotifier.expects(:send_notice).with do |notice|
17
+ notice.parameters.should have_key('log_weasel_id')
18
+ end
19
+ HoptoadNotifier.notify(RuntimeError.new('failure'))
20
+ end
21
+
22
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'resque'
3
+ require 'log_weasel/resque'
4
+
5
+ describe LogWeasel::Resque do
6
+
7
+ before do
8
+ LogWeasel::Resque.initialize! 'FOO'
9
+ end
10
+
11
+ after do
12
+ LogWeasel::Transaction.destroy
13
+ end
14
+
15
+ it "pushes with log_weasel_id in context" do
16
+ Resque.stubs(:redis).returns(stub(:sadd => nil, :rpush => nil))
17
+ Resque.expects(:encode).with do |item|
18
+ item['context'].should_not be_nil
19
+ item['context'].should have_key('log_weasel_id')
20
+ item['context']['log_weasel_id'].should =~ /^FOO-RESQUE/
21
+ end
22
+ Resque.push('queue', {'args' => [1]})
23
+ end
24
+
25
+ describe ".after_fork" do
26
+ context "with log_weasel_id" do
27
+ before do
28
+ @job = Resque::Job.new 'queue', {'args' =>[{}], 'context' => {'log_weasel_id' => "123"}}
29
+ end
30
+
31
+ it "sets transaction id from args" do
32
+ LogWeasel::Transaction.expects(:id=).with('123')
33
+ LogWeasel::Resque::Callbacks.after_fork @job, nil
34
+ end
35
+ end
36
+
37
+ context "without log_weasel_id" do
38
+ before do
39
+ @job = Resque::Job.new 'queue', {'args' =>[1]}
40
+ end
41
+
42
+ it "creates a new log_weasel_id" do
43
+ LogWeasel::Transaction.expects(:create)
44
+ LogWeasel::Resque::Callbacks.after_fork @job, nil
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,6 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
  require 'active_support'
3
- require 'active_support/secure_random'
4
3
 
5
4
  describe LogWeasel::Transaction do
6
5
 
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
- require 'log_weasel'
4
+ require 'log_weasel/transaction'
5
5
  require 'rspec'
6
6
 
7
+ require 'active_support/secure_random'
8
+
7
9
  Rspec.configure do |config|
8
10
  config.mock_with :mocha
9
11
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alon Salant
@@ -71,6 +71,19 @@ dependencies:
71
71
  type: :development
72
72
  prerelease: false
73
73
  version_requirements: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ name: hoptoad_notifier
76
+ requirement: &id005 !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ type: :development
85
+ prerelease: false
86
+ version_requirements: *id005
74
87
  description: Instrument Rails and Resque with shared transaction IDs so that you trace execution across instances.
75
88
  email:
76
89
  - alon@salant.org
@@ -89,9 +102,16 @@ files:
89
102
  - README.md
90
103
  - Rakefile
91
104
  - lib/log_weasel.rb
105
+ - lib/log_weasel/buffered_logger.rb
106
+ - lib/log_weasel/hoptoad_notifier.rb
107
+ - lib/log_weasel/middleware.rb
108
+ - lib/log_weasel/resque.rb
92
109
  - lib/log_weasel/transaction.rb
93
110
  - lib/log_weasel/version.rb
94
111
  - log_weasel.gemspec
112
+ - spec/log_weasel/buffered_logger_spec.rb
113
+ - spec/log_weasel/hoptoad_notifier_spec.rb
114
+ - spec/log_weasel/resque_spec.rb
95
115
  - spec/log_weasel/transaction_spec.rb
96
116
  - spec/spec_helper.rb
97
117
  has_rdoc: true
@@ -108,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
128
  requirements:
109
129
  - - ">="
110
130
  - !ruby/object:Gem::Version
111
- hash: -1874627953720402256
131
+ hash: -3932380171388894158
112
132
  segments:
113
133
  - 0
114
134
  version: "0"
@@ -117,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
137
  requirements:
118
138
  - - ">="
119
139
  - !ruby/object:Gem::Version
120
- hash: -1874627953720402256
140
+ hash: -3932380171388894158
121
141
  segments:
122
142
  - 0
123
143
  version: "0"
@@ -127,7 +147,10 @@ rubyforge_project: log_weasel
127
147
  rubygems_version: 1.3.7
128
148
  signing_key:
129
149
  specification_version: 3
130
- summary: log_weasel-0.0.2
150
+ summary: log_weasel-0.0.3
131
151
  test_files:
152
+ - spec/log_weasel/buffered_logger_spec.rb
153
+ - spec/log_weasel/hoptoad_notifier_spec.rb
154
+ - spec/log_weasel/resque_spec.rb
132
155
  - spec/log_weasel/transaction_spec.rb
133
156
  - spec/spec_helper.rb