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

81
Low Latency Logging with RabbitMQ BrnoPHP Conference 2014

description

Logging is an absolute must for any API or web application, but when starting out, questions such as "how can we do it without disrupting everything else" and "what is the easiest way to log" often come up. We’re going to examine a tried and tested method to carry out high-performance, low-latency logging using the power of RabbitMQ to ensure minimal impact to the performance of your runtime application. The talk will show you that a really great logging architecture is a low-cost investment in your application that will definitely pay off in the long run.

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

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

Low Latency Loggingwith RabbitMQ

BrnoPHP Conference 2014

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

James Titcumbwww.jamestitcumb.comwww.protected.co.ukwww.phphants.co.uk@asgrim

Who is this guy?

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

Let’s go back to basics...

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

Errors

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

source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/

Page 6: Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
Page 7: Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
Page 8: Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
Page 9: Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
Page 10: Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)

error_reporting(0);

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

They look rubbish!

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

@

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

Log to file only

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

Ways Around// Will raise E_WARNING

fopen($somefile, 'r');

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

Ways Around// No error! :)

if (file_exists($somefile)) {

fopen($somefile, 'r');

} else {

// nice handling...

// maybe throw exception...

}

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

Exceptions

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

Jargon Buster

● throwTriggering

● tryTry to run

● catchHandle it

● finallyRun code after try/catch

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

source: http://www.dpaddbags.com/blog/episode-119-technology-sucks/

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

C♯/C++

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

Catchable

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

Turn into fatal errorsif not caught

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

Classy

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

Descriptive exceptions

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

Example (exception class)class DivisionByZeroException

extends LogicException

{

}

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

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);

}

}

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

Example (catch)$maths = new Mathematics();

try

{

$result = $maths->divide(5, 0);

}

catch (DivisionByZeroException $exception)

{

$logger->warning($exception->getMessage());

}

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

Throw Descriptively

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

Logging

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

What is “logging”?

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

What is “logging”?Keeping a record of all events...

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

What is “logging”?Keeping a record of all events...

exceptions, errors, warnings, info, debug

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

Paper Trail

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

Log like you just don’t care

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

Log like you just don’t care

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

Log wherever you like

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

What about Apache’s error_log?

source: http://up-ship.com/blog/?p=20903

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

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

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

Fire & Forget

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

Low Latency

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

Highly Available

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

PSR-3

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

● monolog (PSR-3)● Drupal - PSR-3 Watchdog● phpconsole● log4php● RavenPHP + Sentry● FirePHP (dev environment)● Roll your own

Logging Options

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

source: http://mirror-sg-wv1.gallery.hd.org/_c/natural-science/cogs-with-meshed-teeth-AJHD.jpg.html

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

Capturing Messages

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

Capturing Messagesset_error_handler()

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

Capturing Messagesset_exception_handler()

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

Capturing Messagesregister_shutdown_function()

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

Capturing MessagesUsing the Logger Directly

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

Adapter / Handler

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

Capture Method

Logger (PSR-3)

Handler / Adapter

Data Storage

Typical PSR-3 Compatible Design

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

Monolog\ErrorHandler->handleException()

Monolog\Logger->log()

Monolog\Handler->handle()

Monolog

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

Low Latency Logging

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

Make it easy

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

Fire & Forget

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

Minimum Impact

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

Slow Logging

ApplicationBrowser Log Server

HTTP request

Send log message to log server

Error!

Acknowledge message

HTTP response to client

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

Zero Latency Logging (ideal)

ApplicationBrowser Log Server

HTTP request

Send log message to log server

Error!

HTTP response to client

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

UDP?

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

We need a balance.

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

Low Latency Logging (balance)

ApplicationBrowser Log Server

HTTP request

Send log message to log server

Error!

HTTP response to client

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

…so how?

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

Disclaimer...

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

Say hello to RabbitMQ

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

RabbitMQ - Basic

source: http://www.rabbitmq.com/tutorials/tutorial-one-php.html

Producer Consumer

test_queue

1 2 3 4 5

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

RabbitMQ - Exchanges

source: http://www.rabbitmq.com/tutorials/tutorial-three-php.html

Exchange

Consumertest_queue

1 2 3 4 5

Consumer

Producer

Producer

Producer

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

Exchanges are flexible

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

RabbitMQ === Fast!

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

Ed\Log\Handler\ErrorHandler->handleException()

Ed\Log\Logger->log()

Ed\Log\Publisher\AmqpPublisher->publish()

Logging Server

Low Latency (using AMQP)

RabbitMQ JSON payload

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

Fetch message

Low Latency Logging (with AMQP)

ApplicationBrowser Log Server

HTTP request

JSON via AMQP

Error!

HTTP response

RabbitMQ

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

Why bother?● Scalability

RabbitMQ

Application A

Application B

Application C

Log Worker

Log Worker

Log Worker

Log Worker

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

Single Point of Failure...

RabbitMQNode 1

RabbitMQNode 3

RabbitMQNode 2

RabbitMQNode 4

RabbitMQNode 5

RabbitMQNode 6

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

To recap...

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

Scalable.

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

Easy.

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

Fast.

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

Future proof.

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

Questions?

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

James Titcumb@asgrim

Thanks for watching!