-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trace: Updated Tracing & Added Documentation #25
- Loading branch information
1 parent
7e43690
commit 57bdec5
Showing
7 changed files
with
161 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,125 @@ | ||
## StackDriver Trace Support | ||
# StackDriver Trace Integration | ||
|
||
This package includes built in support for tracing important components to StackDriver Trace. | ||
|
||
By default, this includes: | ||
* Laravel startup | ||
* (currently doesn't render in the tree view properly) | ||
* Laravel internals (including more granular startup). | ||
* Application construct | ||
* Request capture | ||
* Request handle | ||
* Response send | ||
* Request terminate (cleanup) | ||
* Application specific | ||
* Middleware | ||
* (soon) Middleware | ||
* Time in Router | ||
* Controller / Route-Closure Runtime | ||
* Blade view compile/render | ||
* External calls / RPC | ||
* memcached | ||
* redis | ||
* (soon) redis | ||
* MySQL | ||
* PDO | ||
* Eloquent (Laravel) | ||
* Datastore | ||
* Guzzle (HTTP(s)) | ||
* (soon) Datastore | ||
* (soon) Guzzle (HTTP(s)) | ||
|
||
It also allows you to register your own trace providers to be registered as the application boots, via the config file for this package (`trace_providers` in `gaesupport.php`). | ||
|
||
## Architecture | ||
There are two different levels of trace integration: | ||
|
||
* Low Level (to catch Laravel core boot) | ||
* Higher level (via Service Provider) | ||
|
||
All of the trace providers at all levels should implement the interface `OpenCensus\Trace\Integrations\IntegrationInterface`, which providers a static `load` method where the trace hooks are registered. | ||
|
||
### Low level | ||
To allow us to capture the startup of Laravel in more detail and to make sure the core is ready to be traced before any of it runs, we set up the OpenCensus trace library and register some trace hooks before loading Laravel. | ||
|
||
We do this by asking composer to include `src/preload.php` once it has set up the autoloader, the same way helper functions are initialised. | ||
|
||
`src/preload.php` will first include `src/helpers.php` to register our helper functions (which is done because composer can't guarantee load order if we specify an array of files to load in `composer.json` and we need our helper functions before the preload functions run). | ||
|
||
By default, the list of low level providers is provided by calling `A1comms\GaeSupportLaravel\Trace\LowLevelLoader`, but this can be overridden by creating your own `LowLevelLoader` that implements `A1comms\GaeSupportLaravel\Trace\LowLevelLoaderInterface` at `App\Trace\LowLevelLoader`, which we check for before loading the default. | ||
|
||
Example `app/Trace/LowLevelLoader.php` that just loads `Memcached` tracing: | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Trace; | ||
|
||
use A1comms\GaeSupportLaravel\Trace\LowLevelLoaderInterface; | ||
|
||
class LowLevelLoader implements LowLevelLoaderInterface | ||
{ | ||
public static function getList() | ||
{ | ||
return [ | ||
OpenCensus\Trace\Integrations\Memcached::class, | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
### Higher Level | ||
For trace providers that can be registered after Laravel has loaded and during the service provider boot, when other service providers may already be running, we can register them via the `TraceServiceProvider` and have Laravel functionality available if required. | ||
|
||
Firstly, the `TraceServiceProvider` will register an event for `laravel/boostrap`, with a start time set to `LARAVEL_START` as set in the top of `public/index.php`, to document the point in execution when it was run, as an indication of when Laravel finished loading. | ||
|
||
Next, it will look in the application config for our service (`gaesupport.php`) and run the `load` function for any classes listed in the `trace_providers` array, allowing you to add trace hooks for your own application. | ||
|
||
As an example, to trace the `enqueue` function of a model class `CustomQueue`, I'd start by creating the file `app/Trace/CustomQueueModel.php` with the contents: | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Trace; | ||
|
||
use OpenCensus\Trace\Integrations\IntegrationInterface; | ||
use App\CustomQueue; | ||
|
||
class CustomQueueModel implements IntegrationInterface | ||
{ | ||
public static function load() | ||
{ | ||
if (!extension_loaded('opencensus')) { | ||
trigger_error('opencensus extension required to load Laravel integrations.', E_USER_WARNING); | ||
return; | ||
} | ||
|
||
opencensus_trace_method(CustomQueue::class, 'enqueue', [self::class, 'handleEnqueue']); | ||
} | ||
|
||
public static function handleResponseSend($scope, $job) | ||
{ | ||
return [ | ||
'name' => 'model/CustomQueue/enqueue', | ||
'attributes' => [ | ||
'job' => $job, | ||
] | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
To note here is the importance of the parameters passed to the callback function, with the first being the instance of the class as it was called, then all of the parameters to the function as it was called. | ||
|
||
For more information, see the OpenCensus PECL extension documentation. | ||
|
||
https://github.com/census-instrumentation/opencensus-php/blob/master/docs/content/using-the-extension.md | ||
|
||
Next, update your `gaesupport.php` configuration file to include that trace provider into the array: | ||
|
||
```php | ||
'trace_providers' => [ | ||
App\Trace\CustomQueueModel:class, | ||
], | ||
``` | ||
|
||
## Installation | ||
Since the low level trace setup is done as part of the composer autoloader initialisation, most of the installation is taken care of once you've installed the package, although for the higher level & custom trace providers, you'll need to make sure the `GaeSupportServiceProvider` and `TraceServiceProvider` are both loaded into Laravel. | ||
|
||
In Laravel 5.5, service providers are automatically discovered by default, so unless you've disabled this functionality, you shouldn't need to do anything else either. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace A1comms\GaeSupportLaravel\Trace; | ||
|
||
/** | ||
* Class to return the low level trace modules to load. | ||
*/ | ||
class LowLevelLoader implements LowLevelLoaderInterface | ||
{ | ||
/** | ||
* Static method to get the list of trace modules to load. | ||
*/ | ||
public static function getList() | ||
{ | ||
return [ | ||
// OpenCensus provides a basic Laravel trace adapter, | ||
// which covered Eloquent and view compilation. | ||
OpenCensus\Trace\Integrations\Laravel::class, | ||
// Also load our own extended Laravel trace set. | ||
A1comms\GaeSupportLaravel\Trace\Integration\LowLevel\LaravelExtended::class, | ||
// Trace our other basic functions... | ||
OpenCensus\Trace\Integrations\Mysql::class, | ||
OpenCensus\Trace\Integrations\PDO::class, | ||
OpenCensus\Trace\Integrations\Memcached::class, | ||
]; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/A1comms/GaeSupportLaravel/Trace/LowLevelLoaderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace A1comms\GaeSupportLaravel\Trace; | ||
|
||
/** | ||
* Interface to implement for loading your own list | ||
* of low level trace modules. | ||
*/ | ||
interface LowLevelLoaderInterface | ||
{ | ||
/** | ||
* Static method to get the list of trace modules to load. | ||
* | ||
* For an example, see the default at LowLevelLoader. | ||
*/ | ||
public static function getList(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters