Skip to content

Commit

Permalink
Load middleware in Railtie when using Rails 3, configure and load wit…
Browse files Browse the repository at this point in the history
…h LogWeasel.configure
  • Loading branch information
asalant committed Feb 22, 2011
1 parent 8f382da commit 06ed753
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 44 deletions.
1 change: 0 additions & 1 deletion Gemfile.lock
Expand Up @@ -45,7 +45,6 @@ PLATFORMS
ruby

DEPENDENCIES
activesupport (~> 3.0)
hoptoad_notifier
log_weasel!
mocha
Expand Down
36 changes: 21 additions & 15 deletions README.md
Expand Up @@ -2,6 +2,7 @@

Instrument Rails and Resque with shared transaction IDs to trace execution of a unit of work across
applications and application instances.

This particularly handy if you're using a system like <a href="http://www.splunk.com">Splunk</a> to manage your log
files across many applications and application instances.

Expand All @@ -19,39 +20,44 @@ Use bundler to install it:
bundle install
</pre>

## Configure

In your application initialization (e.g. config/initializers/log_weasel.rb) load and configure Log Weasel with:

<pre>
LogWeasel.configure do |config|
config.key = "YOUR_APP"
end
<pre>

<code>key</code> is a string that will be included in your transaction IDs and is particularly
useful in an environment where a unit of work may span multiple applications.

## Rack

Log Weasel provides Rack middleware to create and destroy a transaction ID for every HTTP request. You can use it
in a any web framework that supports Rack (Rails, Sinatra,...)
in a any web framework that supports Rack (Rails, Sinatra,...) by using <code>LogWeasel::Middleware</code> in your middleware
stack.

### Rails 3

To see Log Weasel transaction IDs in your Rails logs, you need to add the Rack middleware and
either use the BufferedLogger provided or customize the formatting of your logger to include
<code>LogWeasel::Transaction.id</code>.
For Rails 3, we provide a Railtie that automatically registers the Rack middleware.

All you need to see Log Weasel transaction IDs in your Rails logs is to either use the BufferedLogger provided or
customize the formatting of your logger to include <code>LogWeasel::Transaction.id</code>.

<pre>
YourApp::Application.configure do
logger = LogWeasel::BufferedLogger.new "#{Rails.root}/log/#{Rails.env}.log"
config.logger = logger
config.action_controller.logger = logger
config.active_record.logger = logger

config.middleware.insert_before Rails::Rack::Logger,
LogWeasel::Middleware, :key => 'YOUR_APP'
end
</pre>

<code>:key</code> is an optional parameter that is useful in an environment where a unit of work may span multiple applications.

## Resque

To see Log Weasel transaction IDs in your Resque logs, you need to need to initialize Log Weasel
when you configure Resque, for example in a Rails initializer.

<pre>
LogWeasel::Resque.initialize! :key => 'YOUR_APP'
</pre>
When you configure Log Weasel as described above, it modifies Resque to include transaction IDs in all worker logs.

Start your Resque worker with <code>VERBOSE=1</code> and you'll see transaction IDs in your Resque logs.

Expand Down
30 changes: 27 additions & 3 deletions lib/log_weasel.rb
Expand Up @@ -3,8 +3,32 @@
require 'log_weasel/hoptoad_notifier'
require 'log_weasel/middleware'
require 'log_weasel/resque'
require 'log_weasel/railtie' if defined? ::Rails::Railtie

class << ::HoptoadNotifier
include LogWeasel::HoptoadNotifier;
end if defined? ::HoptoadNotifier

module LogWeasel
class Config
attr_accessor :key
end

def self.config
@@config ||= Config.new
end

def self.configure
yield self.config

if defined? ::HoptoadNotifier
class << ::HoptoadNotifier
include LogWeasel::HoptoadNotifier;
end
end

if defined? Resque
LogWeasel::Resque.initialize!
end

end
end

#Rails.application.class.to_s.split("::").first
2 changes: 1 addition & 1 deletion lib/log_weasel/middleware.rb
@@ -1,7 +1,7 @@
class LogWeasel::Middleware
def initialize(app, options = {})
@app = app
@key = options[:key] ? "#{options[:key]}-WEB" : "WEB"
@key = LogWeasel.config.key ? "#{LogWeasel.config.key}-WEB" : "WEB"
end

def call(env)
Expand Down
6 changes: 4 additions & 2 deletions lib/log_weasel/railtie.rb
@@ -1,3 +1,5 @@
class Railtie
# To change this template use File | Settings | File Templates.
require 'rails'

class LogWeasel::Railtie < Rails::Railtie
config.app_middleware.insert_before "::Rails::Rack::Logger", "LogWeasel::Middleware"
end
10 changes: 1 addition & 9 deletions lib/log_weasel/resque.rb
@@ -1,13 +1,11 @@
module LogWeasel::Resque

@@initialized = false

def self.initialize!(options = {})
::Resque::Worker.send(:include, LogWeasel::Resque::Worker)
::Resque::Job.send(:include, LogWeasel::Resque::Job)
::Resque.extend(LogWeasel::Resque::ClassMethods)

key = options[:key] ? "#{options[:key]}-RESQUE" : "RESQUE"
key = LogWeasel.config.key ? "#{LogWeasel.config.key}-RESQUE" : "RESQUE"

::Resque.after_fork do |job|
LogWeasel::Resque::Callbacks.after_fork job, key
Expand All @@ -16,12 +14,6 @@ def self.initialize!(options = {})
::Resque.before_push do |queue, item|
LogWeasel::Resque::Callbacks.before_push queue, item, key
end

@@initialized = true
end

def self.initialized?
@@initialized
end

module Callbacks
Expand Down
1 change: 0 additions & 1 deletion spec/log_weasel/buffered_logger_spec.rb
@@ -1,5 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'log_weasel/buffered_logger'

describe LogWeasel::BufferedLogger do
before do
Expand Down
6 changes: 1 addition & 5 deletions spec/log_weasel/hoptoad_notifier_spec.rb
@@ -1,13 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'hoptoad_notifier'
require 'log_weasel/hoptoad_notifier'

describe LogWeasel::HoptoadNotifier do
before do
class << ::HoptoadNotifier
include LogWeasel::HoptoadNotifier;
end if defined? ::HoptoadNotifier

LogWeasel.configure {}
HoptoadNotifier.configure {}
LogWeasel::Transaction.stubs(:id).returns('123')
end
Expand Down
16 changes: 16 additions & 0 deletions spec/log_weasel/log_weasel_spec.rb
@@ -0,0 +1,16 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'resque'
require 'log_weasel'

describe LogWeasel do

describe ".configure" do
it "stores key" do
LogWeasel.configure do |config|
config.key = "KEY"
end
LogWeasel.config.key.should == "KEY"
end
end

end
7 changes: 1 addition & 6 deletions spec/log_weasel/resque_spec.rb
@@ -1,21 +1,16 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'resque'
require 'log_weasel/resque'

describe LogWeasel::Resque do

before do
LogWeasel::Resque.initialize! :key => 'FOO'
LogWeasel.configure { |config| config.key = "FOO" }
end

after do
LogWeasel::Transaction.destroy
end

it "is initialized" do
LogWeasel::Resque.initialized?.should be_true
end

it "pushes with log_weasel_id in context" do
Resque.stubs(:redis).returns(stub(:sadd => nil, :rpush => nil))
Resque.expects(:encode).with do |item|
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,7 +1,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'log_weasel/transaction'
require 'log_weasel'
require 'rspec'

require 'active_support/secure_random'
Expand Down

0 comments on commit 06ed753

Please sign in to comment.