Low Latency Logging with RabbitMQ PHP London 4th September 2014
Who is this guy? James Titcumb www.jamestitcumb.com www.protected.co.uk www.phphants.co.uk @asgrim
Let’s go back to basics...
Errors
source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/
error_reporting(0);
They look rubbish!
@
Log to file only
Ways Around // Will raise E_WARNING fopen($somefile, 'r');
Ways Around // No error! :) if (file_exists($somefile)) { fopen($somefile, 'r'); } else { // nice handling... // maybe throw exception... }
Exceptions
Jargon Buster ● throw Triggering ● try Try to run ● catch Handle it ● finally Run code after try/catch
source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/
C♯/C++
Catchable
Turn into fatal errors if not caught
Classy
Descriptive exceptions
Example (exception class) class DivisionByZeroException extends LogicException { }
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); } }
Example (catch) $maths = new Mathematics(); try { $result = $maths->divide(5, 0); } catch (DivisionByZeroException $exception) { $logger->warning($exception->getMessage()); }
Logging
What is “logging”?
What is “logging”? Keeping a record of all events...
What is “logging”? Keeping a record of all events... exceptions, errors, warnings, info, debug
Logging is a paper trail
Log like you just don’t care
Log wherever you like
What about Apache’s error_log? source: http://up-ship.com/blog/?p=20903
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
Requirements (for everyone) ● Fire & forget ● Minimum or zero latency ● Highly available ● Log everything ● PSR-3 compatible
PSR-3
Logging Options ● monolog (PSR-3) ● Drupal - PSR-3 Watchdog ● phpconsole ● log4php ● RavenPHP + Sentry ● FirePHP (dev environment) ● Roll your own
source: http://mirror-sg-wv1.gallery.hd.org/_c/natural-science/cogs-with-meshed-teeth-AJHD.jpg.html
Capturing Logging Use “capture methods”, send to $logger ● set_exception_handler() ○ Handles all uncaught exceptions ● set_error_handler() ○ Handles most errors ● register_shutdown_function() ○ Handles fatal errors
Sending Log Messages ● Handler/Adapter translates ● However you want… ● Monolog has loads: ○ syslog-compatible / error_log ○ Email, HipChat ○ AMQP, Sentry, Zend Monitor, Graylog2 ○ Redis, MongoDB, CouchDB
Typical PSR-3 Compatible Design Capture Method Logger (PSR-3) Handler / Adapter Data Storage
MonologErrorHandler ->handleException() MonologLogger ->log() MonologHandler ->handle() Monolog
Low Latency High Performance
Slow Logging Browser Application Log Server HTTP request Send log message to database Error! Acknowledge message HTTP response to client
Zero Latency Logging (ideal) Browser Application Log Server HTTP request Send log message to database Error! HTTP response to client
UDP?
Low Latency Logging (balance) Browser Application Log Server HTTP request Send log message to database Error! HTTP response to client
…so how?
Say hello to RabbitMQ
RabbitMQ - Basic test_queue 1 2 3 4 5 Producer Consumer source: http://www.rabbitmq.com/tutorials/tutorial-one-php.html
RabbitMQ - Exchanges Exchange Consumer test_queue 1 2 3 4 5 source: http://www.rabbitmq.com/tutorials/tutorial-three-php.html Consumer Consumer Consumer Consumer
Using Queues === Fast! Add RabbitMQ to logging architecture...
Low Latency (using AMQP) EdLogHandlerErrorHandler ->handleException() EdLogLogger ->log() EdLogPublisherAmqpPublisher ->publish() RabbitMQ JSON payload Logging Server
Low Latency Logging (with AMQP) Browser Application Log Server Fetch message HTTP request JSON via AMQP Error! HTTP response RabbitMQ
Why bother? ● Scalability RabbitMQ Application A Application B Application C Log Worker Log Worker Log Worker Log Worker
Single Point of Failure... ● RabbitMQ can do HA or clustering RabbitMQ Node 1 RabbitMQ Node 3 RabbitMQ Node 2 RabbitMQ Node 4 RabbitMQ Node 6 RabbitMQ Node 5
Questions?
Thanks for watching! James Titcumb www.jamestitcumb.com www.protected.co.uk www.phphants.co.uk @asgrim

Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)