low latency logging (brightonphp - 18th nov 2013)

52
Low Latency Logging at BrightonPHP

Upload: james-titcumb

Post on 11-May-2015

3.158 views

Category:

Technology


4 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 out. I'm going to explore a couple of infrastructure ideas to carry out what I call "high-performance, low-latency" logging to ensure minimal impact to the performance of the 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 (BrightonPHP - 18th Nov 2013)

Low Latency Loggingat BrightonPHP

Page 2: Low latency Logging (BrightonPHP - 18th Nov 2013)

James Titcumb

@asgrimgithub.com/asgrim

Page 3: Low latency Logging (BrightonPHP - 18th Nov 2013)

@asgrimgithub.com/asgrim

● Started on Amiga 500● PHP for 11 years● ZCE PHP 5.3

● Run PHP Hampshire user group● Lead dev on browscap.ini● First open source GoDeploy● Development Manager at Protected.co.uk● Mostly a ZF2 developer

Page 4: Low latency Logging (BrightonPHP - 18th Nov 2013)

Hello! :)

@asgrimgithub.com/asgrim

Page 5: Low latency Logging (BrightonPHP - 18th Nov 2013)

Errors

Page 6: Low latency Logging (BrightonPHP - 18th Nov 2013)

● Something broke… :)● e.g.

○ Can’t connect to MySQL (mysqli_connect)○ No such file or directory (fopen)

● Usually PHP core● Sometimes “fatal”

What are errors?

Page 7: Low latency Logging (BrightonPHP - 18th Nov 2013)

Problems?

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

Page 8: Low latency Logging (BrightonPHP - 18th Nov 2013)

Problems.

● error_reporting setting● Errors look ugly● “@”● Limited options● Not very “OO”

Page 9: Low latency Logging (BrightonPHP - 18th Nov 2013)

Ways Around

// Will raise E_NOTICEfopen($somefile, 'r');

Page 10: Low latency Logging (BrightonPHP - 18th Nov 2013)

Ways Around

// No error! :)if (file_exists($somefile)) { fopen($somefile, 'r');} else { // nice handling... // maybe throw exception...}

Page 11: Low latency Logging (BrightonPHP - 18th Nov 2013)

Exceptions

Page 12: Low latency Logging (BrightonPHP - 18th Nov 2013)

Jargon Buster

● throwTriggering

● tryTry to run

● catchHandle it

● finallyRun code after try/catch

Page 13: Low latency Logging (BrightonPHP - 18th Nov 2013)

● Something still broke● OO● Catchable● Fatal errors● They are classes

What are exceptions?

Page 14: Low latency Logging (BrightonPHP - 18th Nov 2013)

● Built in to PHP ● More descriptive than just “Exception”, e.g.:

○ InvalidArgumentException○ LogicException○ OutOfBoundsException○ RuntimeException○ see PHP manual for more

SPL Exceptions

Page 15: Low latency Logging (BrightonPHP - 18th Nov 2013)

Example (exception class)

class DivisionByZeroException

extends LogicException

{

}

Page 16: Low latency Logging (BrightonPHP - 18th Nov 2013)

Example (throw)class Mathematics

{ public function divide($a, $b)

{

if ($b == 0) {

$msg = sprintf(‘Tried to divide %s by zero”, $a);

throw new DivisionByZeroException($msg);

}

return ($a / $b);

}

}

Page 17: Low latency Logging (BrightonPHP - 18th Nov 2013)

Example (catch)

$maths = new Mathematics();

try

{

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

}

catch (DivisionByZeroException $exception)

{

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

}

Page 18: Low latency Logging (BrightonPHP - 18th Nov 2013)

Logging

Page 19: Low latency Logging (BrightonPHP - 18th Nov 2013)
Page 20: Low latency Logging (BrightonPHP - 18th Nov 2013)

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

exceptions, errors, warnings, info, debug

Page 21: Low latency Logging (BrightonPHP - 18th Nov 2013)

Why use logging?

● Easier to find problems● More detail● “paper trail” for code● Log where you want

Page 22: Low latency Logging (BrightonPHP - 18th Nov 2013)

What about Apache’s error_log?

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

Page 23: Low latency Logging (BrightonPHP - 18th Nov 2013)

Why?

● error_log is too basic● Reading / parsing● error_reporting (again)

Page 24: Low latency Logging (BrightonPHP - 18th Nov 2013)

Doin’ it right wrong… /************************************************************\

* 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 25: Low latency Logging (BrightonPHP - 18th Nov 2013)

Requirements (for everyone)

● Fire & forget● Minimum or zero latency● Highly available● Log everything:

○ Exceptions○ Errors○ Fatal Errors○ Debug & info

● PSR-3 compatible

Page 26: Low latency Logging (BrightonPHP - 18th Nov 2013)

PSR-3

● Common logging interface→ LoggerInterface

● RFC-5424 Levels(debug, info, notice, warning, error, critical, alert, emergency)

● Interoperability● Reusability

Page 27: Low latency Logging (BrightonPHP - 18th Nov 2013)

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

Logging Options

Page 28: Low latency Logging (BrightonPHP - 18th Nov 2013)

How they work...

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

Page 29: Low latency Logging (BrightonPHP - 18th Nov 2013)

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 30: Low latency Logging (BrightonPHP - 18th Nov 2013)

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 31: Low latency Logging (BrightonPHP - 18th Nov 2013)

Capture Method

Logger (PSR-3)

Handler / Adapter

Data Storage

Typical PSR-3 Compatible Design

Page 32: Low latency Logging (BrightonPHP - 18th Nov 2013)

Monolog\ErrorHandler->handleException()

Monolog\Logger->log()

Monolog\Handler->handle()

Monolog

Page 33: Low latency Logging (BrightonPHP - 18th Nov 2013)

Low LatencyHigh Performance

Page 34: Low latency Logging (BrightonPHP - 18th Nov 2013)

● Easy to add● Fire & Forget● Minimum impact to request

What do I mean?

Page 35: Low latency Logging (BrightonPHP - 18th Nov 2013)

Slow Logging

ApplicationBrowser Log Server

HTTP request

Send log message to database

Error!

Acknowledge message

HTTP response to client

Page 36: Low latency Logging (BrightonPHP - 18th Nov 2013)

Zero Latency Logging (ideal)

ApplicationBrowser Log Server

HTTP request

Send log message to database

Error!

HTTP response to client

Page 37: Low latency Logging (BrightonPHP - 18th Nov 2013)

What about UDP?

● Yes… Zero latency, but….

● Do you even care about your logs?(UDP means log messages may get lost...)

● TCP means guaranteed network delivery

● Any non-blocking fails

Page 38: Low latency Logging (BrightonPHP - 18th Nov 2013)

Low Latency Logging (balance)

ApplicationBrowser Log Server

HTTP request

Send log message to database

Error!

HTTP response to client

Page 39: Low latency Logging (BrightonPHP - 18th Nov 2013)

So how?

Page 40: Low latency Logging (BrightonPHP - 18th Nov 2013)

Say hello to RabbitMQ

Page 41: Low latency Logging (BrightonPHP - 18th Nov 2013)

● Robust messaging for applications● Easy to use● Runs on all major operating systems● Supports a huge number of developer

platforms● Open source and commercially supported

www.rabbitmq.com

What is RabbitMQ?

Page 42: Low latency Logging (BrightonPHP - 18th Nov 2013)

RabbitMQ - Basic

“Publisher” “Consumer”

queue

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

Page 43: Low latency Logging (BrightonPHP - 18th Nov 2013)

RabbitMQ - Exchanges

“Consumer 1”

“Consumer 2”

queue 1

queue 2 “Publisher” “Exchange”

“Publisher”

“Publisher”

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

“Publisher”

“Publisher”

Page 44: Low latency Logging (BrightonPHP - 18th Nov 2013)

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

Page 45: Low latency Logging (BrightonPHP - 18th Nov 2013)

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

Ed\Log\Logger->log()

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

Logging Server

Low Latency (using AMQP)

RabbitMQ JSON payload

Page 46: Low latency Logging (BrightonPHP - 18th Nov 2013)

Fetch message

Low Latency Logging (with AMQP)

ApplicationBrowser Log Server

HTTP request

JSON via AMQP

Error!

HTTP response

RabbitMQ

Page 47: Low latency Logging (BrightonPHP - 18th Nov 2013)

Why bother?● Scalability

RabbitMQ

Application A

Application B

Application C

Log Worker

Log Worker

Log Worker

Log Worker

Page 48: Low latency Logging (BrightonPHP - 18th Nov 2013)

Single Point of Failure...

● RabbitMQ can do HA

RabbitMQNode 1

RabbitMQNode 3

RabbitMQNode 2

RabbitMQNode 4

RabbitMQNode 5

RabbitMQNode 6

Page 49: Low latency Logging (BrightonPHP - 18th Nov 2013)

Live demo(pre-recorded… meh)

Page 50: Low latency Logging (BrightonPHP - 18th Nov 2013)

Questions?

Page 51: Low latency Logging (BrightonPHP - 18th Nov 2013)

Feedback please...https://joind.in/9928

Page 52: Low latency Logging (BrightonPHP - 18th Nov 2013)

Thanks!

@asgrimgithub.com/asgrim

https://joind.in/9928