Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Measuring and Logging Everything in Real Time

Measuring and Logging Everything in Real Time

Do you know what is happening in your web application right now, this second, how many requests, logins, exceptions you have, how many and what data your users are generating and what the click through rate of each of your registration step is? Or do you know exactly which exception with which stack trace caused the error your users are seeing? If not, this talk will show how you can easily measure and log everything that happens in your application with the help of open source tools, such as Graphite, StatsD, Elasticsearch and Logstash, without influencing your production environment.

Bastian Hofmann

September 27, 2014
Tweet

More Decks by Bastian Hofmann

Other Decks in Programming

Transcript

  1. bool error_log ( string $message [, int $message_type = 0

    [, string $destination [, string $extra_headers ]]] )
  2. public function handle($errno, $msg, $file, $line) {! $e = new

    \Exception();! $error = [! 'type' => $errno,! 'message' => $msg,! 'file' => $file,! 'line' => $line,! 'trace' => $e->getTrace(),! ];! ! error_log(json_encode($error));! ! return true;! }
  3. LogFormat "%h %l %u %t \"%r\" %>s %b" custom! !

    CustomLog /var/logs/apache/access.log custom http://httpd.apache.org/docs/2.2/mod/ mod_log_config.html#logformat
  4. <?php! ! use Monolog\Logger;! use Monolog\Handler\StreamHandler;! ! // create a

    log channel! $log = new Logger('name');! $log->pushHandler(! new StreamHandler(! 'path/to/your.log', Logger::WARNING! )! );! ! // add records to the log! $log->addWarning('Foo');! $log->addError('Bar');!
  5. <?php! ! use Monolog\Logger;! use Monolog\Handler\StreamHandler;! use Monolog\Formatter\JsonFormatter;! ! $log

    = new Logger('name');! $handler = new StreamHandler(! 'path/to/your.log', Logger::WARNING! );! $handler->setFormatter(new JsonFormatter());! $log->pushHandler($handler);! ! !
  6. web server http service http service http service http service

    create unique trace_id for request user request trace_id trace_id trace_id trace_id log log log log log
  7. web server http service http service http service http service

    create unique trace_id for request user request trace_id trace_id trace_id trace_id log log log log log
  8. input {! file {! type => "error"! path => !

    [ "/var/logs/php/*.log" ]! add_field => ! [ "severity", "error" ]! }! file {! type => "access"! path => ! [ "/var/logs/apache/*_access.log" ]! add_field => ! [ "severity", "info" ]! }!
  9. filter{! ! grok {! match => ["@source", ! "\/%{USERNAME:facility}\.log$"]! }!

    ! grok {! type => "access"! pattern => "^%{IP:OriginalIp} \s[a-zA-Z0-9_-]+\s[a-zA-Z0-9_-]+\s\[.*? \]\s\"%{DATA:Request}..."! }! }
  10. input {
 rabbitmq {
 queue => "logs"
 host => "amqp.host"


    exchange => "ls_exchange"
 exclusive => false
 }
 }
  11. filter {! grok {
 match => [
 'Type','EntityNotFoundException| TimeoutException'
 ]


    break_on_match => true
 add_tag => ['not_serious']
 tag_on_failure => ['serious']
 }! ! }
  12. output {
 elasticsearch {
 embedded => false
 bind_host => "localhost"


    bind_port => "9305"
 host => "localhost"
 cluster => "logs"
 }
 }
  13. browser JS: boomerang logstash trackingServer access log requests tracking image

    with timing information as query parameters graphite statsd