Top Banner
Hiro Asari Senior Software Engineer Red Hat Failure is not an option or: How I learned to read the logs and love the stack trace RubyConf Brasil 2012 Friday, August 31, 12
52

Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Dec 26, 2018

Download

Documents

ngobao
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Hiro AsariSenior Software EngineerRed Hat

Failure is not an optionor: How I learned to read the logs and love the stack trace

RubyConf Brasil 2012

Friday, August 31, 12

Page 2: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Failure is not an optionor: How I learned to read the logs and love the stack trace

Hiro AsariJRuby Support Engineer

RubyConf Brasil 2012

Friday, August 31, 12

Page 3: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Friday, August 31, 12

Page 4: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

If debugging is the process of removing bugs, then programming must be the process of putting them in.

Edsger Dijkstra©2002 Hamilton Richards

Friday, August 31, 12

Page 5: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

All code appearing in this talk are fictitious. Any resemblance

to real code fragments, in production or otherwise, is

purely coincidental.

Friday, August 31, 12

Page 6: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Discovery

Friday, August 31, 12

Page 7: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Know where your logs are

http://www.flickr.com/photos/41449558@N06/6690612703Friday, August 31, 12

Page 8: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

/var/log/*$RAILS_ROOT/log

Friday, August 31, 12

Page 9: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Gather the logs

http://www.flickr.com/photos/30505480@N00/6820686281Friday, August 31, 12

Page 10: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Know your architecture

http://en.wikipedia.org/wiki/File:Ponte_estaiada_Octavio_Frias_-_Sao_Paulo.jpg

Friday, August 31, 12

Page 11: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

http://goo.gl/maps/hBrzmFriday, August 31, 12

Page 12: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

HAProxy

nginx

unicorn

nginx nginx nginx nginx

unicorn unicorn unicorn

request

Web Server

Friday, August 31, 12

Page 13: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

HAProxy

nginx

unicorn

nginx nginx nginx nginx

unicorn unicorn unicorn

request

Friday, August 31, 12

Page 14: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

http://www.flickr.com/photos/usnavy/6871499850/

What if there are no logs?

Friday, August 31, 12

Page 15: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Generating logs

• Debugger

• IRB

• pry

• p / pp / puts

Friday, August 31, 12

Page 16: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Debug!

Friday, August 31, 12

Page 17: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

⋮gems/bundler-1.0.9/lib/bundler/ui.rb:56: uninitialized constant Gem::SilentUI (NameError)⋮

$ bundle install$

Friday, August 31, 12

Page 18: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

know?

Does

Friday, August 31, 12

Page 19: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Answer:

Update to Bundler 1.0.10

or later

Friday, August 31, 12

Page 20: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Read th

e stac

k trac

e

http://www.flickr.com/photos/26699508@N04/2529389660

Friday, August 31, 12

Page 21: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

NoMethodError: undefined method `[]' for nil:NilClass! !initialize at c:/jruby/lib/ruby/gems/1.8/gems/metric_fu-2.1.1/lib/generators/saikuro.rb:232! !get_elements at c:/jruby/lib/ruby/gems/1.8/gems/metric_fu-2.1.1/lib/generators/saikuro.rb:169! !initialize at c:/jruby/lib/ruby/gems/1.8/gems/metric_fu-2.1.1/lib/generators/saikuro.rb:130! !assemble_files at c:/jruby/lib/ruby/gems/1.8/gems/metric_fu-2.1.1/lib/generators/saikuro.rb:111

Example

Friday, August 31, 12

Page 22: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

def initialize(line)!!@line = line!!@element_type = line.match(TYPE_REGEX)[1].strip!!@name = line.match(NAME_REGEX)[1].strip!!@complexity = line.match(COMPLEXITY_REGEX)[1].strip!!@lines = line.match(LINES_REGEX)[1].strip!!@defs = []end

Example

line 232

Friday, August 31, 12

Page 23: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

rake assets:precompile failure

Example

JRuby 1.6.6Rails 3.1.1

can't convert Encoding into String

Friday, August 31, 12

Page 24: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

rake aborted!can't convert Encoding into String (in /opt/jruby/lib/ruby/gems/shared/gems/haml_coffee_assets-0.6.0/vendor/assets/javascripts/hamlcoffee.js.coffee.erb)org/jruby/RubyIO.java:3722:in `popen'/opt/jruby/lib/ruby/gems/shared/gems/execjs-1.2.9/lib/execjs/external_runtime.rb:165:in `sh'/opt/jruby/lib/ruby/gems/shared/gems/execjs-1.2.9/lib/execjs/external_runtime.rb:125:in `exec_runtime'org/jruby/RubyBasicObject.java:1700:in `__send__'org/jruby/RubyKernel.java:2099:in `send'⋮

Example

Friday, August 31, 12

Page 25: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

def sh(command) output, options = nil, {} options[:external_encoding] = @encoding if @encoding options[:internal_encoding] =

Encoding.default_internal || 'UTF-8' IO.popen(command, options) { |f| output = f.read } output end

line 165

jruby -e 'output = nil; IO.popen("ls",{:internal_encoding => Encoding::EucJP}) { |f| output = f.read }; p output'

Minimal Failure

Friday, August 31, 12

Page 26: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

$ jruby -v --1.9 -e 'output = nil; IO.popen("ls",{:internal_encoding => Encoding::EucJP}) { |f| output = f.read }; p output'jruby 1.6.6 (ruby-1.9.2-p312) (2012-01-30 5673572) (Java HotSpot(TM) 64-Bit Server VM 1.7.0_04) [darwin-x86_64-java]TypeError: can't convert Encoding into String popen at org/jruby/RubyIO.java:3722 (root) at -e:1

Example

Friday, August 31, 12

Page 27: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

rake aborted!can't convert Encoding into String (in /opt/jrubylib/ruby/gems/shared/gems/haml_coffee_assets-0.6.0/vendor/assets/javascripts/hamlcoffee.js.coffee.erb)org/jruby/RubyIO.java:3722:in `popen'/opt/jrubylib/ruby/gems/shared/gems/execjs-1.2.9/lib/execjs/external_runtime.rb:165:in `sh'/opt/jrubylib/ruby/gems/shared/gems/execjs-1.2.9/lib/execjs/external_runtime.rb:125:in `exec_runtime'org/jruby/RubyBasicObject.java:1700:in `__send__'org/jruby/RubyKernel.java:2099:in `send'⋮

Example

Friday, August 31, 12

Page 28: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Time to

run debugger!

Friday, August 31, 12

Page 29: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Friday, August 31, 12

Page 30: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Friday, August 31, 12

Page 31: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Friday, August 31, 12

Page 32: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Friday, August 31, 12

Page 33: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Rails footnotes plugin

Friday, August 31, 12

Page 34: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Safeguards

Friday, August 31, 12

Page 35: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Test your code before you ship

Friday, August 31, 12

Page 36: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Set up continuous integration

• Travis http://travis-ci.org

• Jenkins http://jenkins-ci.org

Friday, August 31, 12

Page 37: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Code Coverage

• Shows what you did not test

• Does not show your tests are complete

Friday, August 31, 12

Page 38: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

def foo(arg) begin if arg.nil? raise ArgumentError else raise RegexpError end rescue Exception raise "Play it again, Sam" endend

Rescue exceptions judiciously

Friday, August 31, 12

Page 39: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

def foo(arg) begin if arg.nil? raise ArgumentError else raise RegexpError end rescue Exception => e raise e endend

Rescue exceptions judiciously

Friday, August 31, 12

Page 40: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

unless arg.is_a? Array arg = [arg] end # do stuff with arg arg.each do … endend

Let it faildef foo(arg)

Friday, August 31, 12

Page 41: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

# do stuff with arg arg.each do … endend

Let it fail

unless arg.is_a? Array arg = [arg] end

def foo(arg)

Friday, August 31, 12

Page 42: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

# do stuff with arg arg.each do … endend

Let it faildef foo(arg)

Friday, August 31, 12

Page 43: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

def foo(*args) args.each do … endend

Let it fail

Friday, August 31, 12

Page 44: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Prefer hard-coded* error messages

Ugh!

$ ack -c 'Expected.*but got' .15

$ ruby -S awesome_appExpected 1, but got 2.

Friday, August 31, 12

Page 45: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

irb(main):007:0> obj.foo.foo.foo.foo.fooNoMethodError: undefined method `foo' for nil:NilClass!!!! from (irb):7!!!! from /usr/local/bin/irb2.0:12:in `<main>'

Method chaining

Friday, August 31, 12

Page 46: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

…if you're as clever as you can be when you write [a program], how will you ever debug it?

Brian KernighanFriday, August 31, 12

Page 47: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Test your code before you ship

Friday, August 31, 12

Page 48: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Optimize for humans

…who deal with your present failures

Friday, August 31, 12

Page 49: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

Remember this guy?

It is you!

Friday, August 31, 12

Page 50: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

One More Thing…

Friday, August 31, 12

Page 51: Failure is not an option - static.eventials.com · Failure is not an option or: How I learned to read the logs and love the stack trace Hiro Asari JRuby Support Engineer RubyConf

http://yqg06172.img.jugem.jp/20110824_2225189.jpg

Friday, August 31, 12