项目作者: GlueDev

项目描述 :
Enables logging, tracing and error reporting to Google Stackdriver for Laravel
高级语言: PHP
项目地址: git://github.com/GlueDev/laravel-stackdriver.git
创建时间: 2018-11-23T11:51:59Z
项目社区:https://github.com/GlueDev/laravel-stackdriver

开源协议:MIT License

下载


Laravel Stackdriver

Latest Version on Packagist
Total Downloads

Enables logging, tracing and error reporting to Google Stackdriver for Laravel.
Requires PHP >= 7.1

Screenshots

Tracing Error reporting

Installation

Via Composer

  1. composer require gluedev/laravel-stackdriver

And publish the config file

  1. php artisan vendor:publish --provider="GlueDev\Laravel\Stackdriver\StackdriverServiceProvider"

Usage

First, you will want to open config/stackdriver.php. Here you can see that you have four environment settings available to enable and disable the different features of this package:

  1. STACKDRIVER_ENABLED=false
  2. STACKDRIVER_LOGGING_ENABLED=true
  3. STACKDRIVER_TRACING_ENABLED=true
  4. STACKDRIVER_ERROR_REPORTING_ENABLED=true

The first variable listed has priority over the others.

Authentication

At the time of writing, Google prefers you to authenticate using a service account. It will throw a warning otherwise, which you can (but probably should not) disable by setting SUPPRESS_GCLOUD_CREDS_WARNING=true

Create a service account with the appropriate roles attached to it and add it to your project. Make sure not to commit this file to git, because of security. You can then specify the path to the service account JSON file in the keyFilePath or in the STACKDRIVER_KEY_FILE_PATH environment variable.

Tracing

Tracing requires the OpenCencus module to be installed. As we use docker, this is how we install it:

  1. RUN pecl install opencensus-alpha
  2. RUN docker-php-ext-enable opencensus

Please note: If you run in to an opencensus.so: undefined symbol: ZVAL_DESTRUCTOR error after installing the OpenCencus extension, it is recommended to build the extension yourself, following these instructions.

Logging

Other than changing the values in the config file, logging needs no additional setup.

Error reporting

Error reporting requires you to add the following to the report function in your Exceptions/handler.php

  1. use GlueDev\Laravel\Stackdriver\StackdriverExceptionHandler;
  2. /**
  3. * Report or log an exception.
  4. *
  5. * @param \Exception $exception
  6. * @return void
  7. */
  8. public function report(Exception $exception)
  9. {
  10. StackdriverExceptionHandler::report($exception);
  11. parent::report($exception);
  12. }

Log in to Google Cloud Console and you should start seeing logs, traces and errors appear.

Batch daemon

Google also provides a batch daemon, which is recommended to use. We have seen issues with slow time to first byte on requests when the daemon was not running.
The easiest way to run the daemon is using Supervisor. An example configuration for you to edit:

  1. [program:google-batch-daemon]
  2. command = php -d auto_prepend_file='' -d disable_functions='' /app/vendor/bin/google-cloud-batch daemon
  3. process_name = %(program_name)s
  4. user = application
  5. numprocs = 1
  6. autostart = true
  7. autorestart = true
  8. stdout_logfile = /dev/stdout
  9. stdout_logfile_maxbytes = 0
  10. stderr_logfile = /dev/stderr
  11. stderr_logfile_maxbytes = 0

You also need to tell Google that the daemon is running. This is done by setting the IS_BATCH_DAEMON_RUNNING=true.

And that is it!

Change log

Please see the changelog for more information on what has changed recently.

Credits

License

Please see the license file for more information.