Git Product home page Git Product logo

laravel-stackdriver's Introduction

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

composer require gluedev/laravel-stackdriver

And publish the config file

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:

STACKDRIVER_ENABLED=false  
STACKDRIVER_LOGGING_ENABLED=true  
STACKDRIVER_TRACING_ENABLED=true
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:

RUN pecl install opencensus-alpha
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

use GlueDev\Laravel\Stackdriver\StackdriverExceptionHandler;

/**  
 * Report or log an exception.
 *
 * @param \Exception  $exception  
 * @return void  
 */
public function report(Exception $exception)  
{  
    StackdriverExceptionHandler::report($exception);  
    parent::report($exception);  
}

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:

[program:google-batch-daemon]
command = php -d auto_prepend_file='' -d disable_functions='' /app/vendor/bin/google-cloud-batch daemon
process_name = %(program_name)s
user = application
numprocs = 1
autostart = true
autorestart = true
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes = 0
stderr_logfile = /dev/stderr
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.

laravel-stackdriver's People

Contributors

diederikvandenb avatar ibrunotome avatar jangidgirish avatar ni-bschmitt avatar sebastianmulders avatar sija avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-stackdriver's Issues

Laravel 5.1 Support

Hello,

I noticed you require Laravel 5.7 or newer. I was wondering if this was done on purpose and if you had any interest in trying to allow for Laravel 5.1. Thank you.

breaking change on laravel 7

Laravel 7 now receives a Throwable instead of Exception in public function report(Throwable $exception)

the public static function report(Exception $exception): void expects the Exception type.

What we can do? bump a new major version for laravel 7 compatibility or remove the type of variable?

Is it possible to "decorate" jsonPayload?

I'd like to add some data to all log messages, like a request id for tracing purposed for example. Any idea how I might accomplish that? I tried adding a processor to all log handlers but it seems like that data doesn't get passed to the app['log']->listen. Thanks!

How to setup this package?

Hello,

I assume this package will free us from having to configure ourselves enabling cloud logging and error logging on GCP.

There are a few questions if you could help me answer them:

  • Can this package be used for apps running on both App Engine and Compute Engine? Or is it only for App Engine?
  • The keyfile path. How to specify that when running on App Engine? For compute engine ,I can download the file locally and put the path. But for App Engine, how does it work? I can put the file in the project root folder but then it gets into git. Is there another way?
  • For the batch daemon, if I am running on App Engine, do i still need to add it to supervisord? In that case, what's the file path that I should specify for the daemon?

Thanks a lot

Question about setup

Hi,

Some help would be greatly appreciated to get this package to work.

  1. Credentials

    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

    What is "it"? Where will any warning be thrown? In the local logs? On Google Console?

  2. Service account

    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.

    What are the appropriate roles for each functionality of this package?

    I tried adding these 3 to get logging on Google:

    • Cloud Trace Admin
    • Logging Admin
    • Error Reporting User
  3. Log channel

    The plugin has a separate configuration where we can enable and disable the Stack Drive logging, but how does it integrate with the Laravel logging channel?
    Our app being doesn't have write permission, therefore we would like to use Stack Driver as the only logging channel. Is it possible? Is enabling Stack Driver supposed to disable other logging channels?

  4. How to make it work?

    After installing the package, and adding a configuration nothing happens:

     STACKDRIVER_ENABLED=true
     STACKDRIVER_LOGGING_ENABLED=true
     STACKDRIVER_TRACING_ENABLED=false
     STACKDRIVER_ERROR_REPORTING_ENABLED=true
     STACKDRIVER_KEY_FILE_PATH=storage/keyfile.json
    

    Nothing appears in the Google Cloud Console, current logging channel is still used, and no error related to this package appears in there. When the key file path is wrong, there are no errors either.

Cannot install package

the composer require is returning the error

./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for google/common-protos (locked at 1.2) -> satisfiable by google/common-protos[1.2].
    - Installation request for gluedev/laravel-stackdriver dev-master -> satisfiable by gluedev/laravel-stackdriver[dev-master].
    - google/cloud-error-reporting v0.12.3 requires google/gax ^0.38.0 -> satisfiable by google/gax[0.38.0, 0.38.1, 0.38.2, 0.38.x-dev].
    - google/cloud-error-reporting v0.12.4 requires google/gax ^0.38.0 -> satisfiable by google/gax[0.38.0, 0.38.1, 0.38.2, 0.38.x-dev].
    - google/cloud-error-reporting v0.12.5 requires google/gax ^0.38.0 -> satisfiable by google/gax[0.38.0, 0.38.1, 0.38.2, 0.38.x-dev].
    - google/gax 0.38.0 requires google/common-protos ^0.1 -> satisfiable by google/common-protos[0.1.0].
    - google/gax 0.38.1 requires google/common-protos ^0.1 -> satisfiable by google/common-protos[0.1.0].
    - google/gax 0.38.2 requires google/common-protos ^0.1 -> satisfiable by google/common-protos[0.1.0].
    - google/gax 0.38.x-dev requires google/common-protos ^0.1 -> satisfiable by google/common-protos[0.1.0].
    - Conclusion: don't install google/common-protos 0.1.0
    - Can only install one of: google/gax[1.3.0, 0.37.0].
    - Can only install one of: google/gax[0.37.0, 1.3.0].
    - Can only install one of: google/gax[0.37.0, 1.3.0].
    - Conclusion: install google/gax 0.37.0|install google/cloud-error-reporting v0.12.3|install google/cloud-error-reporting v0.12.4|install google/cloud-error-reporting v0.12.5
    - Installation request for google/gax (locked at 1.3.0) -> satisfiable by google/gax[1.3.0].


Installation failed, reverting ./composer.json to its original content.

[Fix] Issues with ramsey/uuid when installing?

Hey folks, not a bug, but a compatibility issue downstream from this package.

This is likely a newbie issue more than anything, but if you're using Laravel 7 (in my case 7.13) it requires ramsey/uuid ^3|^4".

However, opencensus/opencensus (a dependency of this package) has its ramsey/uuid dependency set to "~3".

Thus, you need to manually downgrade ramsey/uuid directly in your project PRIOR to requiring gluedev/laravel-stackdriver or you too could wind up in the dependency-hell I just escaped from.

Do this by running composer require ramsey/uuid "^3". Then you can proceed on and run composer require gluedev/laravel-stackdriver.

Again, this dependency tracing may be second nature to others... but it just gobbled up about 90 minutes of my time. Thus, I hope this helps others.

Configure as a separate channel

Hi!

I am probably not configuring the package correctly, but we are already using another channels for logging and when we enable the Stackdriver the other channel stops being called.
How is this driver attached to Laravel base Logger? Shouldn't we need to add this as a separate driver or channel at least?

What about custom fields? How does that work?

Thanks for the help in advance!

Array and string offset access syntax with curly braces is deprecated

Hello,
I encounter the following error when enabling CloudTracing. The Laravel application is run in the official php:7.4-apache docker container.

[2020-03-16 11:05:34] production.ERROR: Array and string offset access syntax with curly braces is deprecated {"exception":"[object] (ErrorException(code: 0): Array and string offset access syntax with curly braces is deprecated at /var/www/html/vendor/opencensus/opencensus/src/Trace/Propagator/CloudTraceFormatter.php:104)
[stacktrace]
#0 /var/www/html/vendor/composer/ClassLoader.php(444): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8192, 'Array and strin...', '/var/www/html/v...', 104, Array)
#1 /var/www/html/vendor/composer/ClassLoader.php(444): include()
#2 /var/www/html/vendor/composer/ClassLoader.php(322): Composer\\Autoload\\includeFile('/var/www/html/v...')
#3 [internal function]: Composer\\Autoload\\ClassLoader->loadClass('OpenCensus\\\\Trac...')
#4 /var/www/html/vendor/opencensus/opencensus/src/Trace/Propagator/HttpHeaderPropagator.php(51): spl_autoload_call('OpenCensus\\\\Trac...')
#5 /var/www/html/vendor/opencensus/opencensus/src/Trace/Tracer.php(129): OpenCensus\\Trace\\Propagator\\HttpHeaderPropagator->__construct()
#6 /var/www/html/vendor/gluedev/laravel-stackdriver/src/Stackdriver.php(77): OpenCensus\\Trace\\Tracer::start(Object(OpenCensus\\Trace\\Exporter\\StackdriverExporter))
#7 /var/www/html/vendor/gluedev/laravel-stackdriver/src/StackdriverServiceProvider.php(23): GlueDev\\Laravel\\Stackdriver\\Stackdriver->startTracing()
#8 [internal function]: GlueDev\\Laravel\\Stackdriver\\StackdriverServiceProvider->boot()
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#13 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(590): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#14 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(856): Illuminate\\Container\\Container->call(Array)
#15 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(839): Illuminate\\Foundation\\Application->bootProvider(Object(GlueDev\\Laravel\\Stackdriver\\StackdriverServiceProvider))
#16 [internal function]: Illuminate\\Foundation\\Application->Illuminate\\Foundation\\{closure}(Object(GlueDev\\Laravel\\Stackdriver\\StackdriverServiceProvider), 17)
#17 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(840): array_walk(Array, Object(Closure))
#18 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php(17): Illuminate\\Foundation\\Application->boot()
#19 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(219): Illuminate\\Foundation\\Bootstrap\\BootProviders->bootstrap(Object(Illuminate\\Foundation\\Application))
#20 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(156): Illuminate\\Foundation\\Application->bootstrapWith(Array)
#21 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(140): Illuminate\\Foundation\\Http\\Kernel->bootstrap()
#22 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#23 /var/www/html/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#24 {main}
"}

Show the exception message

Currently it only shows the exception but not the message, and this is something that can really help figure out what is happening,

Could it be possible to add?
I can try create a PR if you give me a couple of hints on where to add it

Thanks!

_ENABLED vars can end up bring strings and failing strict checks

I spent a while trying to set this up and ended up realizing that I was caught on some strict bool checks for the enabled settings in StackdriverExceptionHandler

        if (config('stackdriver.enabled') === false || config('stackdriver.error_reporting.enabled') === false) {
            return;
        }

I resolve this for me for now by casting these to cool in my settings file.

'enabled' => (bool)env('STACKDRIVER_ERROR_REPORTING_ENABLED', true),

I want to make a PR to make sure no one else falls over this issue but would like to know if I should go for casting the settings to bool or switching to non strict comparisons when the config is checked.

Define level of errors?

Hi there,

WOrks great loving the tracing bit! With error login is there way we can define level? Erorr, notice etc?

thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.