Advertisement
Advertisement

More Related Content

Advertisement

More from James Titcumb(20)

Recently uploaded(20)

Advertisement

Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)

  1. Low Latency Logging with RabbitMQ BrnoPHP Conference 2014
  2. Who is this guy? James Titcumb www.jamestitcumb.com www.protected.co.uk www.phphants.co.uk @asgrim
  3. Let’s go back to basics...
  4. Errors
  5. source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/
  6. error_reporting(0);
  7. They look rubbish!
  8. @
  9. Log to file only
  10. Ways Around // Will raise E_WARNING fopen($somefile, 'r');
  11. Ways Around // No error! :) if (file_exists($somefile)) { fopen($somefile, 'r'); } else { // nice handling... // maybe throw exception... }
  12. Exceptions
  13. Jargon Buster ● throw Triggering ● try Try to run ● catch Handle it ● finally Run code after try/catch
  14. source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/
  15. C♯/C++
  16. Catchable
  17. Turn into fatal errors if not caught
  18. Classy
  19. Descriptive exceptions
  20. Example (exception class) class DivisionByZeroException extends LogicException { }
  21. Example (throw) class Mathematics { public function divide($a, $b) { if ($b == 0) { $msg = sprintf(“Divide %s by zero”, $a); throw new DivisionByZeroException($msg); } return ($a / $b); } }
  22. Example (catch) $maths = new Mathematics(); try { $result = $maths->divide(5, 0); } catch (DivisionByZeroException $exception) { $logger->warning($exception->getMessage()); }
  23. Throw Descriptively
  24. Logging
  25. What is “logging”?
  26. What is “logging”? Keeping a record of all events...
  27. What is “logging”? Keeping a record of all events... exceptions, errors, warnings, info, debug
  28. Paper Trail
  29. Log like you just don’t care
  30. Log like you just don’t care
  31. Log wherever you like
  32. What about Apache’s error_log? source: http://up-ship.com/blog/?p=20903
  33. Doin’ it rightwrong… // Magic file that makes your entire project work perfectly @ini_set('display_errors', 0); @error_reporting(0); function __globalErrorHandler() { return true; } @set_error_handler('__globalErrorHandler'); @set_exception_handler('__globalErrorHandler'); @register_shutdown_function(function() { if(error_get_last()) echo "Script executed successfully!"; }); https://github.com/webarto/boostrap.php
  34. Fire & Forget
  35. Low Latency
  36. Highly Available
  37. PSR-3
  38. Logging Options ● monolog (PSR-3) ● Drupal - PSR-3 Watchdog ● phpconsole ● log4php ● RavenPHP + Sentry ● FirePHP (dev environment) ● Roll your own
  39. source: http://mirror-sg-wv1.gallery.hd.org/_c/natural-science/cogs-with-meshed-teeth-AJHD.jpg.html
  40. Capturing Messages
  41. Capturing Messages set_error_handler()
  42. Capturing Messages set_exception_handler()
  43. Capturing Messages register_shutdown_function()
  44. Capturing Messages Using the Logger Directly
  45. Adapter / Handler
  46. Typical PSR-3 Compatible Design Capture Method Logger (PSR-3) Handler / Adapter Data Storage
  47. MonologErrorHandler ->handleException() MonologLogger ->log() MonologHandler ->handle() Monolog
  48. Low Latency Logging
  49. Make it easy
  50. Fire & Forget
  51. Minimum Impact
  52. Slow Logging Browser Application Log Server HTTP request Send log message to log server Error! Acknowledge message HTTP response to client
  53. Zero Latency Logging (ideal) Browser Application Log Server HTTP request Send log message to log server Error! HTTP response to client
  54. UDP?
  55. We need a balance.
  56. Low Latency Logging (balance) Browser Application Log Server HTTP request Send log message to log server Error! HTTP response to client
  57. …so how?
  58. Disclaimer...
  59. Say hello to RabbitMQ
  60. RabbitMQ - Basic test_queue 1 2 3 4 5 Producer Consumer source: http://www.rabbitmq.com/tutorials/tutorial-one-php.html
  61. RabbitMQ - Exchanges Exchange Consumer test_queue 1 2 3 4 5 source: http://www.rabbitmq.com/tutorials/tutorial-three-php.html Consumer Producer Producer Producer
  62. Exchanges are flexible
  63. RabbitMQ === Fast!
  64. Low Latency (using AMQP) EdLogHandlerErrorHandler ->handleException() EdLogLogger ->log() EdLogPublisherAmqpPublisher ->publish() RabbitMQ JSON payload Logging Server
  65. Low Latency Logging (with AMQP) Browser Application Log Server Fetch message HTTP request JSON via AMQP Error! HTTP response RabbitMQ
  66. Why bother? ● Scalability RabbitMQ Application A Application B Application C Log Worker Log Worker Log Worker Log Worker
  67. Single Point of Failure... RabbitMQ Node 1 RabbitMQ Node 3 RabbitMQ Node 2 RabbitMQ Node 4 RabbitMQ Node 6 RabbitMQ Node 5
  68. To recap...
  69. Scalable.
  70. Easy.
  71. Fast.
  72. Future proof.
  73. Questions?
  74. Thanks for watching! James Titcumb @asgrim
Advertisement