Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow logging of specified environment variables. #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ For activerecord instantiation counts only:

Rails.application.middleware.use( Oink::Middleware, :instruments => :activerecord )

Environment variables can also be logged by specifying `env_vars`. This is especially handy for displaying request ID or REQUEST_URI from the Rails `env` object.

Rails.application.middleware.use( Oink::Middleware, :env_vars => ['action_dispatch.request_id', 'REQUEST_ID'] )

Note that the previous way of configuring oink, as a set of modules to include into rails controllers, is deprecated.

== Analyzing logs
Expand Down
12 changes: 12 additions & 0 deletions lib/oink/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(app, options = {})
@app = app
@logger = options[:logger] || Hodel3000CompliantLogger.new("log/oink.log")
@instruments = options[:instruments] ? Array(options[:instruments]) : [:memory, :activerecord]
@env_vars = options[:env_vars] ? Array(options[:env_vars]) : []

Oink.extend_active_record! if @instruments.include?(:activerecord)
end
Expand All @@ -17,6 +18,7 @@ def call(env)
status, headers, body = @app.call(env)

log_routing(env)
log_environment(env)
log_memory
log_activerecord
log_completed
Expand All @@ -36,6 +38,16 @@ def log_routing(env)
end
end

def log_environment(env)
return if @env_vars.empty?
env_message = @env_vars.map do |key|
value = env[key]
display_value = value.inspect if value && value.respond_to?(:inspect)
"#{key.inspect}: #{display_value}"
end.join(', ')
@logger.info("Environment: {#{env_message}}")
end

def log_memory
if @instruments.include?(:memory)
memory = Oink::Instrumentation::MemorySnapshot.memory
Expand Down
11 changes: 11 additions & 0 deletions spec/oink/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ def call(env)
end
end

context "include specified environment variables in the oink log" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

let(:app) do
Oink::Middleware.new(SampleApplication.new, :logger => logger, :env_vars => ['action_dispatch.request_id'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.
Line is too long. [113/80]

end

it "includes specified environment variables" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

get "/no_pigs", {}, {"action_dispatch.request_id" => "4cc822f8-0d85-4d80-bcae-d94a4567e06c"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.
Space inside { missing.
Redundant curly braces around a hash parameter.
Line is too long. [98/80]
Space inside } missing.

log_output.string.should include('Environment: {"action_dispatch.request_id": "4cc822f8-0d85-4d80-bcae-d94a4567e06c"}')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [125/80]

end
end

it "reports 0 totals" do
get "/no_pigs"
log_output.string.should include("Instantiation Breakdown: Total: 0")
Expand Down