diff --git a/src/Trace/README.md b/src/Trace/README.md index f1156a265ecc..12499a6f5baa 100644 --- a/src/Trace/README.md +++ b/src/Trace/README.md @@ -21,11 +21,10 @@ $ composer require google/cloud-trace ```php use Google\Cloud\Trace\TraceClient; -use Google\Cloud\Trace\Reporter\SyncReporter; use Google\Cloud\Trace\RequestTracer; $trace = new TraceClient(); -$reporter = new SyncReporter($trace); +$reporter = $trace->reporter(); RequestTracer::start($reporter); ``` @@ -33,8 +32,11 @@ RequestTracer::start($reporter); ### Reporting Traces -The above sample uses the `SyncReporter` to sychronously report trace results to the -Stackdriver servers at the end of the request. +The above sample uses the `AsyncReporter` to report trace results to the Stackdriver servers. +If you are using the experimental +[google-cloud-batch daemon](https://github.com/GoogleCloudPlatform/google-cloud-php-core/blob/master/Batch/BatchDaemon.php) +and have set the `IS_BATCH_DAEMON_RUNNING=true` environment variable, then the reporting of the trace will happen +asynchronously. If not, the reporting will happen at the end of the request and can add some latency to your request. For testing/development, we also provide an `EchoReporter`, `FileReporter` and `LoggerReporter`. diff --git a/src/Trace/TraceClient.php b/src/Trace/TraceClient.php index d0c685d606e7..93c0bc1d88e1 100644 --- a/src/Trace/TraceClient.php +++ b/src/Trace/TraceClient.php @@ -23,6 +23,8 @@ use Google\Cloud\Core\Iterator\PageIterator; use Google\Cloud\Trace\Connection\ConnectionInterface; use Google\Cloud\Trace\Connection\Rest; +use Google\Cloud\Trace\Reporter\AsyncReporter; +use Google\Cloud\Trace\Reporter\ReporterInterface; /** * Google Stackdriver Trace allows you to collect latency data from @@ -51,6 +53,11 @@ class TraceClient */ protected $connection; + /** + * @var array + */ + private $clientConfig; + /** * Create a Trace client. * @@ -79,6 +86,7 @@ class TraceClient */ public function __construct(array $config = []) { + $this->clientConfig = $config; if (!isset($config['scopes'])) { $config['scopes'] = [self::FULL_CONTROL_SCOPE]; } @@ -187,4 +195,18 @@ function (array $trace) { ) ); } + + /** + * Return a Trace reporter that utilizes this client's configuration + * + * @param array $options [optional] Reporter options. + * {@see Google\Cloud\Trace\Reporter\AsyncReporter::__construct()} + * @return ReporterInterface + */ + public function reporter(array $options = []) + { + return new AsyncReporter($options + [ + 'clientConfig' => $this->clientConfig + ]); + } }