Ruby ãã Sentry ã使ã
前回ã¤ã³ã¹ãã¼ã«ãã Sentry ã« Ruby ããã¤ãã³ããç»é²ãã¦ã¿ã¾ãã
Ruby ãã Sentry ã使ãã«ã¯ãããã¸ã§ã¯ãã®è¨å®ç»é¢ã® Ruby ã®ã¨ããã«ãæ¸ãã¦ããããã« sentry-raven ã¨ãã gem ã使ãã¾ãã
getsentry/raven-ruby
https://github.com/getsentry/raven-ruby
ã¤ã³ã¹ãã¼ã«
gem ããå ¥ããå ´åã¯ä»¥ä¸ã
# gem install sentry-raven --no-rdoc --no-ri
bundler ããå ¥ããå ´å㯠Gemfile ãä½æã
# bundle init # vi Gemfile
以ä¸ã追è¨ãã¾ãã
gem "sentry-raven"
bundle install ãã¾ãã
# bundle install
Ruby ãã使ã
以ä¸ã®ãã㪠Ruby ã³ã¼ããè¨è¿°ãã¾ãã
require 'raven' Raven.configure do |config| config.dsn = 'http://6e874f2d864c4925b2049069f5e8af86:[email protected]/2' end Raven.capture do 1 / 0 end
config.dsn ã«ã¯ Sentry ä¸ã§ [è¨å®]-[APIãã¼] ã®ã¨ããã«è¡¨ç¤ºããã¦ããå¤ã使ç¨ãã¾ãã
å®è¡ãã¦ã¿ã¾ãã
% ruby sentry01.rb I, [2014-03-23T17:08:18.060929 #10369] INFO -- : ** [Raven] Raven 0.7.1 ready to catch errors D, [2014-03-23T17:08:18.062539 #10369] DEBUG -- : ** [Raven] Sending event 18180a944cdd445799d8de694e7691e8 to Sentry D, [2014-03-23T17:08:18.068214 #10369] DEBUG -- : ** [Raven] Raven HTTP Transport connecting to http://192.0.2.1 sentry-ex01.rb:8:in `/': divided by 0 (ZeroDivisionError) from sentry-ex01.rb:8:in `block in <main>' from /home/akishin/.anyenv/envs/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/sentry-raven-0.7.1/lib/raven.rb:89:in `call' from /home/akishin/.anyenv/envs/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/sentry-raven-0.7.1/lib/raven.rb:89:in `capture' from sentry-ex01.rb:7:in `<main>'
ä¾å¤ãçºçãã¾ããã
Raven.capture ã«æå®ãããããã¯ã®å
é¨ã§ä¾å¤ãçºçããã¨èªåçã« Sentry ã«éç¥ããã¾ãã
å®è¡å¾ã« Sentry ä¸ã¹ããªã¼ã ãè¦ãã¨ä»¥ä¸ã®ããã«ã¨ã©ã¼ãéç¥ããã¦ããã¯ãã§ãã
ã¾ããä¾å¤ãèªåã§å¦çããä¸ã§éç¥ãè¡ããããã¨ããå ´åã«ã¯ capture_exception ã使ç¨ã§ãã¾ãã
begin raise ArgumentError.new("test") rescue ArgumentError => e Raven.capture_exception(e) end
ä¾å¤ã§ã¯ãªããä»»æã®ã¤ãã³ããéç¥ãããå ´åã«ã¯ capture_message ã使ç¨ãã¾ãã
Raven.capture_message("test event", { level: 'debug', logger: 'debug', extra: { foo: "123", bar: "abc" }, })
Rails ãã使ã
Rails ãã使ãå ´åã¯ãã¾ãã¯ä½æãã Rails ããã¸ã§ã¯ãã® Gemfile ã« sentry-raven ã追è¨ã㦠bundle install ãã¾ãã
ã¤ã³ã¹ãã¼ã«ããã¨ä»¥ä¸ã® rake ã¿ã¹ã¯ã追å ããã¾ãã
% bundle exec rake -T | grep raven rake raven:test[dsn] # Send a test event to the remote Sentry server
ãã®ã¿ã¹ã¯ã« Sentry ã®ç»é¢ã§ç¢ºèªãã dsn ãæå®ãããã¨ã§ Sentry ã®åä½ç¢ºèªãè¡ãäºãã§ãã¾ãã
å®è¡ãã¦ä»¥ä¸ã®ãããªæã㧠event ID ãåå¾ã§ãã¦ããã° Sentry ãµã¼ãã¨ã®éä¿¡ã¯æåãã¦ãã¾ãã
% bundle exec rake "raven:test[http://6e874f2d864c4925b2049069f5e8af86:[email protected]/2]" Client configuration: -> server: http://192.0.2.1 -> project_id: 2 -> public_key: 6e874f2d864c4925b2049069f5e8af86 -> secret_key: 04756c0af5154d69aee463018cef97c0 Sending a test event: -> event ID: e09165be7fb741a18ebe214139430c39 Done!
Sentry ç»é¢ã®ã¹ããªã¼ã ããããã®ãã¹ãã¤ãã³ãã確èªãããã¨ãåºæ¥ã¾ãã
ãã¹ããåé¡ãªããã°ãRails ãã sentry-raven ã使ãããã®è¨å®ãã¡ã¤ã«ãä½æãã¾ãã
% vi config/initializers/raven.rb
以ä¸ã®ãããªå
容ãè¨è¿°ãã¾ãã
Raven-Ruby ã¯ããã©ã«ãã§ã¯ development ç°å¢ã®æã«ã¤ãã³ãã®éä¿¡ãè¡ããªãã®ã§ãããã§ã¯ç¢ºèªã®ãã config.environments ã§å¯¾è±¡ã«å«ããããè¨å®ãã¦ãã¾ãã
Raven.configure do |config| config.dsn = 'http://6e874f2d864c4925b2049069f5e8af86:[email protected]/2' config.environments = %w[ development ] end
ããã§è¨å®ã¯å®äºã§ãã
Rails ã§ä½¿ãå ´åã¯ããã ãã§å¦çãããªãã£ãä¾å¤ãå
¨ã¦ Sentry ã¸éç¥ãããããã«ãªãã¾ãã
Controller å
ãªã©ã§é©å½ã«ä¾å¤ãæãã¦ã¿ãã° Sentry ã¸éç¥ãããäºã確èªã§ããã¨æãã¾ãã
ã¾ã¨ã
ã¨ã©ã¼ç®¡çãã¼ã«ã«ã¤ãã¦ã¯å®éã«è©¦ãã¦ã¿ãã¾ã§ã¯ããã°è¦ãã°ãããããããªã©ã¨æã£ã¦ãã¾ãããã使ã£ã¦ã¿ãã¨æ¬çªç°å¢ã§ããçºçããªãã£ããããã¾ã«ããçºçããªãã£ãããããããªã¨ã©ã¼ã¾ã§ãã®çºçç®æãçºçé »åº¦ãä¸å 管çãããã¨ãã§ããããã«ãªãããã®æç¨æ§ãç解ã§ãã¾ããã
ã¾ããSentry ã§ã¯ã¨ã©ã¼ã ãã§ãªããä»»æã®ã¤ãã³ãã管çããäºãã§ãããããã©ã®å¦çãã©ããããå¼ã°ãã¦ãããããªã©ã¨ãã£ãäºãç°¡åã«èª¿ã¹ãäºãã§ããã®ã§ããããã¯ãã®å質åä¸ã«ä¸å½¹è²·ã£ã¦ããããã§ãã
ã¡ãªã¿ã«ãä»ã®ãµã¼ãã¹ãªã©ã¨ã®é£æºã«ã¤ãã¦ã¯ãã©ã°ã¤ã³æ¹å¼ãåã£ã¦ããããã§ãã
対å¿ãã¦ãããµã¼ãã¹ã¯ä»¥ä¸ã®ãã¼ã¸ã® [3rd Party Extensions] ã«ã¾ã¨ã¾ã£ã¦ãã¾ãã
Plugins
https://sentry.readthedocs.org/en/latest/plugins/index.html
ãããã®ãµã¼ãã¹ã¨é£æºãããäºã§ããå¹æçã«æ´»ç¨ãããã¨ãã§ãããã§ãã