low latency logging with rabbitmq (php london - 4th sep 2014)

61
Low Latency Logging with RabbitMQ PHP London 4th September 2014

Upload: james-titcumb

Post on 13-Jun-2015

458 views

Category:

Software


2 download

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

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

Low Latency Loggingwith RabbitMQ

PHP London4th September 2014

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

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

Who is this guy?

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

Let’s go back to basics...

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

Errors

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

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

Page 6: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Page 7: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Page 8: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Page 9: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Page 10: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)

error_reporting(0);

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

They look rubbish!

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

@

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

Log to file only

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

Ways Around// Will raise E_WARNING

fopen($somefile, 'r');

Page 15: Low Latency Logging with RabbitMQ (PHP London - 4th 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 (PHP London - 4th Sep 2014)

Exceptions

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

Jargon Buster

● throwTriggering

● tryTry to run

● catchHandle it

● finallyRun code after try/catch

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

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

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

C♯/C++

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

Catchable

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

Turn into fatal errorsif not caught

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

Classy

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

Descriptive exceptions

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

Example (exception class)class DivisionByZeroException

extends LogicException

{

}

Page 25: Low Latency Logging with RabbitMQ (PHP London - 4th 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 (PHP London - 4th 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 (PHP London - 4th Sep 2014)

Logging

Page 28: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Page 29: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)

What is “logging”?

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

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

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

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

exceptions, errors, warnings, info, debug

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

Logging is a paper trail

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

Log like you just don’t care

Page 34: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Page 35: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)

Log wherever you like

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

What about Apache’s error_log?

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

Page 37: Low Latency Logging with RabbitMQ (PHP London - 4th 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 38: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)

Requirements (for everyone)

● Fire & forget● Minimum or zero latency● Highly available● Log everything● PSR-3 compatible

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

PSR-3

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

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

Logging Options

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

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

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

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

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

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

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

Capture Method

Logger (PSR-3)

Handler / Adapter

Data Storage

Typical PSR-3 Compatible Design

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

Monolog\ErrorHandler->handleException()

Monolog\Logger->log()

Monolog\Handler->handle()

Monolog

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

Low LatencyHigh Performance

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

Slow Logging

ApplicationBrowser Log Server

HTTP request

Send log message to database

Error!

Acknowledge message

HTTP response to client

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

Zero Latency Logging (ideal)

ApplicationBrowser Log Server

HTTP request

Send log message to database

Error!

HTTP response to client

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

UDP?

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

Low Latency Logging (balance)

ApplicationBrowser Log Server

HTTP request

Send log message to database

Error!

HTTP response to client

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

…so how?

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

Say hello to RabbitMQ

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

RabbitMQ - Basic

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

Producer Consumer

test_queue

1 2 3 4 5

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

RabbitMQ - Exchanges

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

Exchange

Consumertest_queue

1 2 3 4 5

Consumer

Consumer

Consumer

Consumer

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

Using Queues === Fast!Add RabbitMQ to logging architecture...

Page 56: Low Latency Logging with RabbitMQ (PHP London - 4th 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 57: Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)

Fetch message

Low Latency Logging (with AMQP)

ApplicationBrowser Log Server

HTTP request

JSON via AMQP

Error!

HTTP response

RabbitMQ

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

Why bother?● Scalability

RabbitMQ

Application A

Application B

Application C

Log Worker

Log Worker

Log Worker

Log Worker

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

Single Point of Failure...

● RabbitMQ can do HA or clustering

RabbitMQNode 1

RabbitMQNode 3

RabbitMQNode 2

RabbitMQNode 4

RabbitMQNode 5

RabbitMQNode 6

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

Questions?

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

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

Thanks for watching!